Setup Varnish 4.x for CentOS 6.x
03-30-2016, 09:25 AM
(This post was last modified: 03-31-2016, 06:18 AM by reverendspam.)
This tutorial is strictly for Varnish 4.x, Sentora 1.0.x, and CentOS 6.x.
Installation
Log into your server via ssh gaining root access.
IMPORTANT - Make a backup of of your vhosts file.
Get the latest version of Varnish from their repositories:
Then install Varnish
Configure Varnish
Using your favorite editor open /etc/sysconfig/varnish and change "VARNISH_LISTEN_PORT" to
Using your favorite editor open /etc/varnish/default.vcl and make sure line 16-18 states
Configure Sentora
set your apache port
set your sentora port
Go to your sentora administration panel to admin --> Module Admin --> Apache Config and set "Apache Port" to "8080". Tick the "Force Update" box and click on "Save Changes"
restart the apache server
start / restart Varnish
If all goes well both should restart with no issue and Varnish is running in the front of your apache server.
If for some reason the varnish service is not starting try to start it again. If it still does not start and you think there my be a syntax error somewhere you can run the varnish debug command to see where your error is.
To verify Varnish is working correctly
Going back to you root login via ssh type in the command below replacing 127.0.0.1 with one of your websites.
You should get back a response that looks similar to this:
HTTP/1.1 200 OK
Date: Tue, 29 Mar 2016 22:43:18 GMT
Server: Apache
Vary: Accept-Encoding,Cookie
Cache-Control: max-age=3, must-revalidate
WP-Super-Cache: Served supercache file from PHP
Content-Type: text/html; charset=UTF-8
X-Varnish: 65560
Age: 3
Via: 1.1 varnish-v4
Connection: keep-alive
Two flags to that let you know Varnish is working correctly:
X-Varnish: 16
Via: 1.1 varnish-v4
In case something goes wrong
Remove Varnish completely
Restore your vhosts file
Set your apache port back to 80
set your sentora port back to 80
Restart the apache server
Installation
Log into your server via ssh gaining root access.
IMPORTANT - Make a backup of of your vhosts file.
Code:
cp /etc/zpanel/configs/apache/httpd-vhosts.conf /etc/zpanel/configs/apache/httpd-vhosts.bak.conf
Get the latest version of Varnish from their repositories:
Code:
cat << EOF >> /etc/yum.repos.d/varnish.repo
[varnish]
name=Varnish for Enterprise Linux 6
baseurl=https://repo.varnish-cache.org/redhat/varnish-4.0/el6/
enabled=1
gpgkey=https://repo.varnish-cache.org/GPG-key.txt
gpgcheck=1
EOF
Then install Varnish
Code:
yum install -y varnish
Configure Varnish
Using your favorite editor open /etc/sysconfig/varnish and change "VARNISH_LISTEN_PORT" to
Code:
VARNISH_LISTEN_PORT=80
Using your favorite editor open /etc/varnish/default.vcl and make sure line 16-18 states
Code:
backend default {
.host = "127.0.0.1";
.port = "8080";
Configure Sentora
set your apache port
Code:
setso --set apache_port 8080
set your sentora port
Code:
setso --set sentora_port 8080
Go to your sentora administration panel to admin --> Module Admin --> Apache Config and set "Apache Port" to "8080". Tick the "Force Update" box and click on "Save Changes"
restart the apache server
Code:
service httpd restart
start / restart Varnish
Code:
service varnish restart
If all goes well both should restart with no issue and Varnish is running in the front of your apache server.
If for some reason the varnish service is not starting try to start it again. If it still does not start and you think there my be a syntax error somewhere you can run the varnish debug command to see where your error is.
Code:
varnishd -C -f /etc/varnish/default.vcl
To verify Varnish is working correctly
Going back to you root login via ssh type in the command below replacing 127.0.0.1 with one of your websites.
Code:
curl -I http://127.0.0.1/
You should get back a response that looks similar to this:
HTTP/1.1 200 OK
Date: Tue, 29 Mar 2016 22:43:18 GMT
Server: Apache
Vary: Accept-Encoding,Cookie
Cache-Control: max-age=3, must-revalidate
WP-Super-Cache: Served supercache file from PHP
Content-Type: text/html; charset=UTF-8
X-Varnish: 65560
Age: 3
Via: 1.1 varnish-v4
Connection: keep-alive
Two flags to that let you know Varnish is working correctly:
X-Varnish: 16
Via: 1.1 varnish-v4
In case something goes wrong
Remove Varnish completely
Code:
yum erase varnish
Restore your vhosts file
Code:
cp /etc/zpanel/configs/apache/httpd-vhosts.bak.conf /etc/zpanel/configs/apache/httpd-vhosts.conf
Set your apache port back to 80
Code:
setso --set apache_port 80
set your sentora port back to 80
Code:
setso --set sentora_port 80
Restart the apache server
Code:
service httpd restart