0217 | How to install Nginx on CentOS + DA

หลังจากเคยแนะนำวิธี ติดตั้ง varnish กันไปแล้ว ซึ่ง varnish นั้นเหมาะกับเครื่องที่แรมเยอะๆหน่อย วันนี้ขอมาแนะนำการติดตั้ง nginx กันบ้างครับ ซึ่งวิธีก็ดันแปลงนิดหน่อยมาจากการติดตั้ง varnish
1. ติดตั้ง nginx
[code]
wget http://nginx.org/download/nginx-1.0.10.tar.gz
tar xvfz nginx-1.0.10.tar.gz
cd nginx-1.0.10
./configure –with-http_stub_status_module –with-http_gzip_static_module –with-http_realip_module –with-http_ssl_module
make
make install
[/code]

2. แก้ไข config ของ nginx
ไฟล์ config สามารถเอาด้านล่างนี้ไปใช้ได้เลยครับ โดยต้นฉบับของไฟล์ config นี้ เอามาจากคุณ 360 ในเว็บ thaihosttalk.com ครับ

แก้ไขที่ /usr/local/nginx/conf/nginx.conf
[code]
user apache apache;

worker_processes 4; # Set it according to what your CPU have. 4 Cores = 4
worker_rlimit_nofile 8192;

pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main ‘$remote_addr – $remote_user [$time_local] ‘
‘"$request" $status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"’;

server_tokens off;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error_log debug;

server_names_hash_bucket_size 64;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 30;
gzip on;
gzip_comp_level 9;
gzip_proxied any;

proxy_buffering on;
proxy_cache_path /usr/local/nginx/proxy_temp levels=1:2 keys_zone=one:15m inactive=7d max_size=1000m;
proxy_buffer_size 16k;
proxy_buffers 100 8k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;

server {
listen ip เครื่องของคุณ:85 default rcvbuf=8192 sndbuf=16384 backlog=32000; # Real IP here
server_name domain.name _ ; # "_" is for handle all hosts that are not described by server_name
charset off;
access_log /var/log/nginx/nginx_host_general.access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1;
client_max_body_size 16m;
client_body_buffer_size 128k;
proxy_buffering on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 120;
proxy_buffer_size 16k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

location ~* ^/(phpmyadmin|webmail|squirrelmail|uebimiau|roundcube)/.+\.(jpg|jpeg|gif|png|ico|css|zip|tar|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|wmv|avi|cur|swf|mp3|wma|htc|cur)$ {
root /var/www/html/;
expires 30d;
access_log off;
}

location ~* ^/(stats)/.+\.(jpg|jpeg|gif|png|html|htm)$ {
root /var/www/html/;
access_log off;
}

location ~* ^/(mrtg|imrtg)/.+\.(jpg|jpeg|gif|png|html|htm)$ {
root /var/www/html/;
access_log off;
}

location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}

}

include /usr/local/nginx/etc/*.conf;
}
[/code]
ให้แก้ไขบรรทัดที่ 42 ด้วยนะครับ เป็น ip เครื่อง server ของคุณ
จากนั้นสร้าง dir สำหรับเก็บ log ครับ
[code]
mkdir /var/log/nginx
[/code]
และสร้างไฟล์ขึ้นมา
[code]
touch /var/log/nginx/access.log
touch /var/log/nginx/error_log
touch /var/log/nginx/nginx_host_general.access.log
[/code]
3.สร้างไฟล์ start|stop|restart nginx service + pid
[code]
touch /var/run/nginx.pid
touch /etc/init.d/nginx
[/code]
จากนั้นแก้ไขไฟล์ /etc/init.d/nginx
[code]
nano /etc/init.d/nginx
[/code]
และนำ code ด้านล่างนี้ไปวาง
[code]
#!/bin/sh
ulimit -n 65535
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`
options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
for opt in $options; do
if [ `echo $opt | grep ‘.*-temp-path’` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
#/usr/bin/killall -q -w nginx
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[/code]
แก้ permission และกำหนดให้ nginx ทำงานเมื่อ boot ระบบ
[code]
chmod 755 /etc/init.d/nginx
chkconfig –add nginx
chkconfig nginx on
[/code]

ถึงตอนนี้ ให้ลองทดสอบด้วยการเข้าทาง port 85 ครับ เช่น http://www.domain.com:85 จะต้องเข้าเว็บได้ปกติเหมือน port 80 และ mod_rewrite ต้องทำงานได้ปกติ และเช็ค sub domain ด้วยครับ

4.แก้ไข apache ให้ไปใช้ port 8080
แก้ไขไฟล์ /etc/httpd/conf/httpd.conf
จาก
[code]
Listen 80
[/code]
เป็น
[code]
Listen 8080
[/code]

ไฟล์ /etc/httpd/conf/extra/httpd-vhosts.conf
จาก
[code]
Include /etc/httpd/conf/ips.conf

#
#
<VirtualHost 123.123.123.123:80>
[/code]
เป็น
[code]
###Include /etc/httpd/conf/ips.conf
LogFormat "%O \"%r\"" homedir
NameVirtualHost 127.0.0.1:8080
NameVirtualHost 123.123.123.123:8080
NameVirtualHost 123.123.123.123:443

#
#
<VirtualHost 127.0.0.1:8080 123.123.123.123:8080>
[/code]

คัดลอกไฟล์ template มาไว้ใน custom เพื่อแก้ไข (ไฟล์ใน custom จะไม่ถูกทับเมื่อ update DirectAdmin)
[code]
cp -p /usr/local/directadmin/data/templates/virtual_host2.conf /usr/local/directadmin/data/templates/custom/virtual_host2.conf
cp -p /usr/local/directadmin/data/templates/virtual_host2_sub.conf /usr/local/directadmin/data/templates/custom/virtual_host2_sub.conf
cp -p /usr/local/directadmin/data/templates/redirect_virtual_host.conf /usr/local/directadmin/data/templates/custom/redirect_virtual_host.conf
[/code]

หลังจากนั้น ให้เข้าไปแก้ไขไฟล์ทั้ง 3 ไฟล์นี้
/usr/local/directadmin/data/templates/custom/virtual_host2.conf
/usr/local/directadmin/data/templates/custom/virtual_host2_sub.conf
/usr/local/directadmin/data/templates/custom/redirect_virtual_host.conf

แก้ไขทั้ง 3 ไฟล์เหมือนกัน จาก
[code]
<VirtualHost |IP|:|PORT_80| |MULTI_IP|>
[/code]
เป็น
[code]
<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>
[/code]

ติดตั้ง mod_rpaf เพื่อแก้ไข ip ของ remote host จาก 127.0.0.1 ที่เรียกมาจาก nginx ให้เป็น client ip ที่เรียกมาจริง
[code]
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
tar xzf mod_rpaf-0.6.tar.gz
apxs -cia mod_rpaf-2.0.c
[/code]
แก้ไขไฟล์ /etc/httpd/conf/extra/httpd-includes.conf เพิ่มด้านล่างนี้ลงไป

[code]
<IfModule mod_rpaf-2.0.c>
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1
RPAFheader X-Forwarded-For
</IfModule>
[/code]

หลังจากนั้นกลับไปแก้ไขไฟล์ config ของ nginx บรรทัดที่ 42 และ 50
จาก listen ip เครื่องของคุณ:85 เป็น listen ip เครื่องของคุณ:80
และ proxy_pass http://127.0.0.1; เป็น proxy_pass http://127.0.0.1:8080;

5.ทำการ rewrite config vhosts ของ apache ใหม่ตามไฟล์ template ที่ได้แก้ไขไป
[code]
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
[/code]
รอสัก 1 นาที ให้ cron ทำงาน แล้วค่อยไปสั่ง restart apache และ nginx อีกสักครั้งครับ
แค่นี้ก็เสร็จแล้วครับ

ต้องขอบคุณ คุณ 360 และพี่แมน KKE ครับ
แหล่งที่มาจาก
http://www.thaihosttalk.com/topic/32792-%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-config-nginx-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B8%95%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%A2%E0%B8%B8%E0%B9%88%E0%B8%87%E0%B8%AD/
http://www.thaihosttalk.com/topic/32748-how-to-install-varnishd-on-centosda/

เอา 2 อย่างในกระทู้มาประยุกต์เอาครับ คิดว่า ถ้าไม่ต้องให้ firewall ทำการ redirect การทำงานน่าจะดีกว่าครับ

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *