เขาว่ากันว่า Varnish cache แรงดี เลยเอาวิธีลงมาแป๊ะไว้หน่อย วิธีต่อไปนี้ สำหรับ CentOS ที่ลง DirectAdmin ไว้นะครับ
1.เตรียม dependencies ที่ varnish ต้องการ
yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
2.download varnish and compile
ก่อนโหลด ให้โหลดเวอร์ชั่นล่าสุด ที่ stable ดูเวอร์ชั่นล่าสุดได้ที่นี่ครับ https://www.varnish-cache.org/releases
wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz tar xzf varnish-3.0.0.tar.gz cd varnish-3.0.0 sh autogen.sh sh configure make make install ldconfig
3.ทดสอบ varnish ที่ port 8080
ถ้ามี Firewall ให้เปิด port 8080 ก่อนครับ และแก้ไขไฟล์ /usr/local/etc/varnish/default.vcl โดยเอาเครื่องหมาย # ที่อยู่ด้านหน้าออก โดยที่ 123.123.123.123 ให้แก้ไขเป็น ip ของ server ของคุณ
backend default { .host = "123.123.123.123"; .port = "80"; }
start varnish ด้วยคำสั่ง
varnishd -f /usr/local/etc/varnish/default.vcl -s malloc,512M -T 127.0.0.1:2000 -a 123.123.123.123:8080
หากสามารถใช้งานได้เหมือนเปิดเว็บปกติที่ port 80 เช่น https://www.igolf.in.th:8080 ต้องแสดงเหมือนกับ https://www.igolf.in.th แสดงว่าทำงานได้ปกติ
4.stop varnish
หยุดการทำงานของ varnish ก่อนเพื่อที่เราจะทำการสลับ port ของ varnish ให้เป็น 80 และเปลี่ยนของ apache ให้เป็น 8080 ด้วยคำสั่ง
killall varnishd
5.แก้ไขไฟล์ config ต่างๆเพื่อเปลี่ยน port apache ไปใช้งาน 8080
ไฟล์ที่ต้องแก้ไขทั้งหมดคือ
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/extra/httpd-vhosts.conf
/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
แก้ไข /etc/httpd/conf/httpd.conf
จาก
Listen 80
เป็น
Listen 8080
แก้ไขไฟล์ /etc/httpd/conf/extra/httpd-vhosts.conf
จาก
Include /etc/httpd/conf/ips.conf # # <VirtualHost 123.123.123.123:80>
เป็น
#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>
คัดลอกไฟล์ template มาไว้ใน custom เพื่อแก้ไข (ไฟล์ใน custom จะไม่ถูกทับเมื่อ update DirectAdmin)
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
และแก้ไขไฟล์ต่อไปนี้ ที่เพิ่งคัดลอกไป โดยแก้เหมือนกันทั้ง 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
จาก
<VirtualHost |IP|:80 |MULTI_IP|>
เป็น
<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>
6.ติดตั้ง mod_rpaf เพื่อแก้ไข ip ของ remote host จาก 127.0.0.1 ที่เรียกมาจาก varnish ให้เป็น client ip ที่เรียกมาจริง
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz tar xzf mod_rpaf-0.6.tar.gz cd mod_rpaf-0.6 apxs -cia mod_rpaf-2.0.c
แก้ไขไฟล์ /etc/httpd/conf/extra/httpd-includes.conf เพิ่มข้อความ
<IfModule mod_rpaf-2.0.c> RPAFenable On RPAFsethostname On RPAFproxy_ips 127.0.0.1 RPAFheader X-Forwarded-For </IfModule>
7.สร้างไฟล์ start/stop varnishd
nano /etc/init.d/varnishd
เพิ่มข้อความด้านล่างนี้
#!/bin/sh # # Startup script for varnishd # # chkconfig: 2345 80 20 # description: Varnish Cache is an open source, state of the art web application accelerator. \ # You install it on your web server and it makes your website fly. # processname: varnishd # config: /usr/local/etc/varnish/default.vcf # # By: Korakot E. <korakot [at] pinkkeyhost.com> # $Id: varnishd.init.d,v 1.0 2011/07/27 23:23:23 kke $ # Source function library. . /etc/rc.d/init.d/functions MALLOC="512M" BINDADDR="0.0.0.0:80" TELPORT="2000" CONFIG="/usr/local/etc/varnish/default.vcl" OPTIONS="-f $CONFIG -s malloc,$MALLOC -T 127.0.0.1:$TELPORT -a $BINDADDR" if [ -f /etc/sysconfig/varnishd ]; then . /etc/sysconfig/varnishd OPTIONS="-f $CONFIG -s malloc,$MALLOC -T 127.0.0.1:$TELPORT -a $BINDADDR" fi PATH="$PATH:/usr/local/sbin" # See how we were called. case "$1" in start) echo -n "Starting varnishd: " daemon varnishd $OPTIONS echo touch /var/lock/subsys/varnishd ;; stop) echo -n "Shutting down varnishd: " killproc varnishd echo rm -f /var/lock/subsys/varnishd ;; status) status varnishd ;; restart) $0 stop $0 start ;; *) echo -n "Usage: $0 {start|stop|restart|status}" exit 1 esac if [ $# -gt 1 ]; then shift $0 $* fi exit 0
แก้ permission และกำหนดให้ varnishd ทำงานเมื่อ boot ระบบ
chmod 755 /etc/init.d/varnishd chkconfig --add varnishd chkconfig varnishd on
8.กำหนดค่า config ของ varnishd
แก้ไขไฟล์ /usr/local/etc/varnish/default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; }
สร้าง/แก้ไขไฟล์ /etc/sysconfig/varnishd กำหนดค่า MALLOC ตามต้องการ (หรือจะแก้ไปในไฟล์ init.d ตรงๆเลยก็ได้ ก็ไม่ต้องสร้างไฟล์นี้)
MALLOC="1G" BINDADDR="127.0.0.1:80,123.123.123.123:80" TELPORT="2000" CONFIG="/usr/local/etc/varnish/default.vcl"
9.ทำการ rewrite config vhosts ของ apache ใหม่ตามไฟล์ template ที่ได้แก้ไขไป
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
รอประมาณ 1 นาที ให้ cron ทำงาน
/etc/init.d/httpd restart /etc/init.d/varnishd start
เป็นอันเสร็จครับ ทดสอบเข้าเว็บ โดยเข้าทาง port 80 ธรรมดา
โดยสามารถไปดูบทความต้นฉบับได้นี่ครับ
Install Varnishd on CentOS+DA
โดยพี่แมน KKE
you are the best
hi, could you translate this post into English?
many thanks