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 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

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.

Search for Alternative 2, you should see something like this

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


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

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”



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

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

Replace all <VirtualHost *:80> block to <VirtualHost>


Finally, restart varnish and apache2 services

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


and you should see XVarnish in the respond header output

You can see varnish stats with this command

sample output


  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


    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 = “”;
    .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