Install Varnish with cPanel and CentOS
This guide covers installing Varnish 4 as a caching layer in front of Apache on a cPanel/WHM CentOS server. Apache moves to port 8080 and Varnish listens on port 80.
Prerequisites
- Root access to a CentOS 6/7 server running cPanel/WHM
- Apache already serving your sites
Steps
1. Move Apache to port 8080
In WHM navigate to Home → Server Configuration → Tweak Settings,
set Apache non-SSL IP/port to 8080, and save. Alternatively edit
/etc/apache2/conf/httpd.conf directly.
2. Install Varnish
rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
yum install varnish
3. Set Varnish to listen on port 80
Edit /etc/sysconfig/varnish and set:
VARNISH_LISTEN_PORT=80
4. Configure the VCL backend
Edit /etc/varnish/default.vcl and replace the backend block with your
server IP. Adjust the IP to match your server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
return(hash);
}
}
# Strip cookies before caching static assets
sub vcl_backend_response {
if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") {
unset beresp.http.set-cookie;
}
}
5. Enable and start Varnish
chkconfig varnish on
service varnish start
Verify
Check that Varnish is listening on port 80 and proxying correctly:
varnishstat
curl -I http://127.0.0.1/
Notes
- Test any VCL changes before restarting:
varnishd -C -f /etc/varnish/default.vcl - Use
varnishlogto inspect live request/response pairs. - Restart order: Apache first, then Varnish.