Apache is a popular web server used by most web hosting companies. Varnish cache is an HTTP accelerator and reverse proxy. We can use Varnish with any HTTP server. In this example, we will be using Apache 2.
As a web server, Apache can use a considerable amount of server resources to serve pages. If you are running a high-traffic website, then you might need an HTTP accelerator to boost server performance. Varnish will help you with that.
Install the Apache server and activate it by using the following commands:
sudo apt-get update
sudo apt-get install apache2-mpm-eventYou can test Apache’s server status with this command:
sudo service apache2 statusIf the service is running, apache2 is running will be printed to your terminal. Otherwise, you can start the service with this command:
sudo service apache2 startInstall the latest stable version of Varnish 4. This version is not available in the Ubuntu Repository by default, so you need to run the following commands to install it.
sudo apt-get install apt-transport-https
sudo curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
sudo apt-get update
sudo apt-get install varnishConfigure Varnish Cache. Here, we are going to change the Varnish Server port to 80. Run the following command:
sudo nano /etc/default/varnishNow look for DAEMON_OPTS=” under Alternative 2, Configuration with VCL. Change the DAEMON_OPTS=” section to match the following lines. This is only a port update.
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"Press CTRL + X, then Y, to save the file.
Configure Apache. We need to change the listening port of Apache from 80 to 8080.
Edit the ports file by running the following command:
sudo nano /etc/apache2/ports.confChange the Listen 80 to Listen 8080.
Next, update the virtual host file…
sudo nano /etc/apache2/sites-available/000-default.conf… change <VirtualHost :80> to <VirtualHost :8080>.
If you have other virtual host files, then they should be updated as well.
Restart both services.
sudo service apache2 restart
sudo service varnish restartYou’re all set. See the following sections for advanced setup tips.
Run the following command to show Varnish stats:
varnishstatYou can edit the default.vcl file for various features.
To enable browser caching for media files, you vcl_backend_response should match the following configuration.
sub vcl_backend_response {
if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") {
unset beresp.http.set-cookie;
set beresp.http.cache-control = "max-age = 2592000";
}
}This will improve your site speed and SEO ranking.
To clear the Varnish’s cache, you can change vcl_recv to match the following configuration:
sub vcl_recv {
if (req.method == "PURGE") {
return (purge);
}
}After making this change, you can send a curl request in your SSH session with the following format:
curl <domain_name.com> -XPURGEHere, -XPURGE will send the purge request to the Varnish server.