How to install and configure Varnish with Apache on Ubuntu 14.04

I have shown you how to install and configure LAMP server Linux, Apache, MySQL, PHP on Ubuntu 14.04 LTS. If you want your to speed up your LAMP setup, varnish is your answer. Varnish is a reverse proxy web accelerator, which can cache both static and dynamic content and quicky serves them to your website’s visitors. By caching your website’s content and saves them to virtual memory, Varnish can respone a request quickly without accessing the web serve, at the end, Varnish can handle thousands of requests per second and speed up your website up to 300x times.

varnish-logo-namhuy

Varnish benefits on Apache Web Server

  • Serving content from memory cache, no need to wait for MySQL queries or PHP execution
  • Reduce CPU load on the server
  • More flexible caching than other alternatives
  • Your website will load faster

Install Varnish on Ubuntu 14.04 Apache2 Web Server

We are going to install varnish package through their repository
# apt-get update; apt-get install curl nano# curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -# echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list# apt-get update; apt-get install varnish

In Ubuntu 14.04 Varnish configuration file will be locate at “/etc/default/varnish”. You need to setup some basic basic configurations after you installed Varnish in order to make Varnish serves contents on port 80 and gets data from Apache on port 8080.
# nano /etc/default/varnish

Search for Alternative 2, you should see something like this

Replace -a :6081 to port 80 to make varnish stay in front of Apache

to

Also at -s malloc,256m line, the 256m is the default varnish cache size, you can increase or decrease Varnish cache size here. Normally I give varnish 50% to 70% of free RAM. Depends on how much free RAM you have in your system, adjust Varnish cache size here. For example I have 2 GB ram in total, and I have 1.5G free ram, I want Varnish cache to have 1G of RAM to do it’s caching stuff. I will replace 256m value to 1G. Varnish stores data on disk and then pulls it into RAM to accelerate requests. The final /etc/default/varnish after adjust Varnish listening port and caching size should look like

Next, we will configure Varnish default file which is located at /etc/varnish/default.vcl . default.vcl file tells varnish where to get data from Apache web server. In Centos Varnish have default backend at port 80. You want to change that to 8080 and later we will change Apache listen port 8080 to fetch data to varnish. Check default.vcl file to make sure your default backend port is set to 8080
# nano /etc/varnish/default.vcl
and you should have .port = “8080”;

If you host multiple websites/domains on your server, you should have several separate backend (this is or multiple domains configs)

Configure Apache to work with Varnish on Ubuntu 14.04

We have set varnish to get data from Apache on port 8080. As you know Apache default listen port is 80. We have to modify Apache to listen on port 8080 to fetch data to Varnish. We are going to edit Apache port setting located at “/etc/apache2/ports.conf”
# nano /etc/apache2/ports.conf

Replace

To

You will need to change the settings in the default virtual host file also in /etc/apache2/sites-available/ All Virtual Host and IP should set to 127.0.0.1:8080

You must do this step for every single .conf file in /etc/apache2/sites-available/ . I assume you have default.conf file in /etc/apache2/sites-available/ so I will use default.conf as an example
# nano /etc/apache2/sites-available/default.conf

Replace all <VirtualHost *:80> block to <VirtualHost 127.0.0.1:8080>
From
<VirtualHost *:80>

to
<VirtualHost 127.0.0.1:8080>

Finally, restart varnish and apache2 services
# service varnish restart# service apache2 restart

If you follow all my steps correctly, after you restart varnish and apache2, you should not get any error. To check Varnish is acting up and caching files in front of apache2, first load your website on web browser like firefox or chrome, you should be able to load your website as normal. Then use curl command to confirm varnish is running

Apache2_ubuntu_14_04_namhuy
# curl -I http://domain.com
and you should see X-Varnish in the respond header output

You can see varnish stats with this command
# varnishstat

sample output

Comments

  1. Hai says

    I have two vcl file: magento.vcl, wordpress.vcl. How to apply two file vcl for two website wordpress and magento??

    • namhuy says

      first, combine the rules in those two .vcl files into one. Then in /etc/default/varnish file, you can set the location of your own .vcl file in Alternative 2 section.

  2. says

    root@serverwise:~/git/logs# curl -I domain.local
    HTTP/1.1 200 OK
    Set-Cookie: frontend=tr7v14g1fr24dacj5ts7cmo2k1; expires=Wed, 16-Jul-2014 05:52:06 GMT; path=/; domain=domain.local; HttpOnly
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Age: 3387
    Vary: Accept-Encoding
    Content-Type: text/html; charset=UTF-8
    Date: Wed, 16 Jul 2014 03:52:06 GMT
    Age: 3387
    Connection: keep-alive

    ===================

    I not see Via: 1.1 varnish ??

  3. says

    Hello,

    I have one comment when editing “/etc/varnish/default.vcl”: you don’t have to make a single configuration for each domain, having the “backend default” configuration (below) only is enough:

    backend default {
    .host = “127.0.0.1”;
    .port = “8080”;
    }

    Nice tutorial, thank you for sharing this.

    • namhuy says

      thanks for your input, I’m sort of want to seperate things out, easy to control for each domain if thing goes wrong

Leave a Reply