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

# apt-get update; apt-get install curl nano
# curl | sudo apt-key add -
# echo "deb 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

Alternative 2, Configuration with VCL
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.
DAEMON_OPTS="-a :6081 \
 -T localhost:6082 \
 -f /etc/varnish/default.vcl \
 -S /etc/varnish/secret \
 -s malloc,256m"

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

DAEMON_OPTS="-a :6081 \


DAEMON_OPTS="-a :80 \

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

Alternative 2, Configuration with VCL
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.
DAEMON_OPTS="-a :80 \
 -T localhost:6082 \
 -f /etc/varnish/default.vcl \
 -S /etc/varnish/secret \
 -s malloc,1G"

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

backend default {
 .host = "";
 .port = "8080";

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

backend domain1 {
 .host = "";
 .port = "8080";
backend domain2 {
 .host = "";
 .port = "8080";

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”

Listen 80
Listen 8080

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

# nano /etc/apache2/sites-available/default.conf

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

<VirtualHost *:80>



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

# curl -I

and you should see XVarnish in the respond header output

HTTP/1.1 200 OK
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 24 Apr 2014 02:25:15 GMT
ETag: "2cf6-4f7c08f3158c0-gzip"
Vary: Accept-Encoding
Content-Type: text/html
Date: Sun, 25 May 2014 01:12:28 GMT
X-Varnish: 75280918
Age: 0
Via: 1.1 varnish
Connection: keep-alive

You can see varnish stats with this command

sample output
Hitrate ratio: 1 1 1
Hitrate avg: 0.0000 0.0000 0.0000
2 0.00 0.00 client_conn - Client connections accepted
 3 0.00 0.01 client_req - Client requests received
 1 0.00 0.00 cache_miss - Cache misses
 2 0.00 0.00 backend_conn - Backend conn. success
 1 0.00 0.00 backend_reuse - Backend conn. reuses
 1 0.00 0.00 backend_toolate - Backend conn. was close
 3 0.00 0.01 backend_recycle - Backend conn. recycles
 2 0.00 0.00 fetch_length - Fetch with Length
 1 0.00 0.00 fetch_304 - Fetch no body (304)
 5 . . n_sess_mem - N struct sess_mem
 1 . . n_waitinglist - N struct waitinglist
 1 . . n_vbc - N struct vbc
 10 . . n_wrk - N worker threads
 10 0.00 0.02 n_wrk_create - N worker threads created
 1 . . n_backend - N backends
 1 . . n_expired - N expired objects
 2 0.00 0.00 s_sess - Total Sessions
 3 0.00 0.01 s_req - Total Requests
 2 0.00 0.00 s_pass - Total pass
 3 0.00 0.01 s_fetch - Total fetch


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments