Install Varnish 3 To Run With Apache 2 On Ubuntu Server
|Complete guide how to install Varnish Cache 3 HTTP accelerator and configure it to run with Apache 2 on Ubuntu server. Using Apache itself as a web / HTTP server is considered not enough to run and host a production website in that server. Even for WordPress users, using caching plugin like W3 Total Cache, Hyper Cache, or WP Super Cache can sometimes not enough to reduce server load when the site getting its traffic peak. Most users simply ditch Apache and start learning and using Nginx because it is also free, faster, and lightweight. But since Nginx do not support .htaccess, many users think twice before using Nginx. So another alternative comes up, meet Varnish Cache, a free HTTP accelerator specially designed for dynamic website with heavy content and massive traffics beyond what a normal Apache web server is capable to serve.
What’s the difference between Nginx and Varnish 3? While Nginx is primarily an origin web server (like Apache), Varnish is simply an HTTP accelerator so it can also run with Nginx and Apache. You can read more about Varnish in Wikipedia.
Why use Varnish?
- To reduce server load, especially CPU works.
- To make a website load faster, because cache stored in RAM.
- To gain more visitors.
As I said, Varnish can be installed and run along with either Nginx or Apache but this time I will show you how to install and setup Varnish on Apache-based Ubuntu server. Remember that each time I say “server” it refers to both VPS or Dedicated server.
Prerequisite
- This tutorial works on Ubuntu. In this example I use My Playground server runs Ubuntu 12.04.
- You may also need to follow most common practices for initial Ubuntu server setup.
- Install Apache on your server or read my previous guide to setup Apache2 and PHP5 on your server.
Setup and Configure Varnish with Apache
Step 1 – Login to your server via SSH. You can use Terminal in Mac or Linux but as I’m using Windows, Putty is the perfect tool. You can download Putty and read how to use it. You have to login with root or sudo privilege. If you disabled root login (following my previous guide) then once logged in simply type “su” (without quotes), hit Enter then type the password of your root.
Step 2 – This is how to install Varnish 3. But first you have to add official Varnish repo url. Ubuntu by default comes with Varnish already but it might be outdated. Issue following command:
curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
It will then look similar like this:
Now edit “source/list” file then add the repo url there. Use this command to edit the file:
nano /etc/apt/sources.list
then add this line there:
deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0
It looks like this:
Once done hit Control+O to save then Control+X to exit Nano editor.
Now it is time to install Varnish, issue commands below:
apt-get update
then,..
apt-get install varnish
Wait few seconds for the installation process to finish with output something like this:
Step 3 – This is how to setup Varnish. So you’ve installed Varnish on your server and it runs already but it is not running properly, yet. You have to follow other few steps to configure Varnish so your server will be faster than before it your visitors will feel how fast it loads. What are you gonna do? All you have to do is making Varnish to run on port 80 while Apache on port 8080. First configuration file you need to edit is located at “/etc/default/varnish”. Use Nano to edit it:
nano /etc/default/varnish
Now look for these lines there under “Alternative 2”:
DAEMON_OPTS="-a :6081 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
Found it? Now edit those lines to match following to make it works in port 80:
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
It looks like this:
Once done hit Control+O to save then Control+X to exit.
Step 4 – As you will use Varnish Configuration Alternative 2 (with VCL), so you have to also the default.vcl file which tells varnish where to look for the webserver content (in this case is to fetch from Apache in port 8080). This step is also called as to set the backend server. Use Nano again to edit it:
nano /etc/varnish/default.vcl
Now you’ll see some lines like below. If not, edit it to match as following:
backend default { .host = "127.0.0.1"; .port = "8080"; }
The config above says that Apache as the backend server is available on localhost at port 8080 and Varnish will run in front of it listening on port 80 that previously used by Apache.
Now you can exit Nano editor screen.
Step 5 – Now you’ve setup Varnish configuration to work on port 80. Next step, you have to also make Apache to work on port 8080. Without doing this step, Apache will still running on port 80 and so Varnish. Now edit Apache port setting located at “/etc/apache2/ports.conf”
nano /etc/apache2/ports.conf
Once Nano editor screen appears, find “NameVirtualHost” and “Listen” lines and replace 80 with 8080. Find:
NameVirtualHost 127.0.0.1:80 Listen 127.0.0.1:80
edit it to:
NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
screenshot:
Once done hit Control+O to save then Control+X to exit.
Step 6 – Now edit the virtual hosts file for your site’s domain name (in case if you host more than one website in your server). I assume you knew already what is Virtual Hosts file and how to set it up. If not, read my previous guide about how to add new site in Apache server. Alternatively you can edit default Apache hosts file at first:
nano /etc/apache2/sites-available/default
then edit this line:
<VirtualHost *:80>
and make it like this (replace 80 with 8080):
<VirtualHost *:8080>
or you can also make it like this (alternatively):
<VirtualHost 127.0.0.1:8080>
Once done hit Control+O to save then Control+X to exit.
Step 7 – That’s it. Final step, restart Apache and Varnish service:
service apache2 restart
then
service varnish restart
It will look similar like mine below:
Done.
Now you can test your server and it should work fine now. Using steps explained above your site will load faster and your server will be lighter in load. Also, by default as configured above in step 3, Vanish will use 256mb of your allocated RAM to store all cache files generated by the service. Please edit it to match your server specs. Edit that value to lower number If you are on a VPS with low amount of RAM. But you may also allocate x gb of your RAM if you are on Dedicated server.
Q: How to know / detect a server is running Varnish? Now once you access your website in browser it should be served through Varnish. You can use Firebug to test response header or simply use Centralops domain scanner service and check mark the “service scan” option. You’ll see message saying: Via: 1.1 varnish.
output (in the bottom):
Q: I want to see my website not via Varnish, how to access through Apache directly?
Simply add port “8080” at the end of your domain / ip address. Example:
http://servermom.wpengine.com:8080/
Q: How to know Varnish performance?
You can know how Varnish performance by seeing its real time stats. You can see its Hit rate ration, Hit rate average, number of client connections accepted, and so on. Issue this command syntax:
varnishstat
Example below taken from my playground server (not a production server running live website) so there is only me accessing the site.
That’s it. Enjoy your blazing speed server. Also catch my complete guide how to build and configure Ubuntu server from zero.
p.s: You can simply install Nginx or Apache + Varnish cache easily using Tuxlite. Read my previous guide about Tuxlite.
Thanks for sharing. Really nice.
Dear thank you for the tutorial I did all of this config but I always got error : varnish Error 503 Service Unavailable I changed timeout and the same , I just want to install varnish only , I dnt need apache or Ngnix
please if you can help me and if you have working config file send me it
thank you
I need install and configure varnish 1 pc and other PC apache httpd
any idea ?
Hello,
I am loving your web, i am a baby and just learning about building a website and your tutorial are really awesome.. i mean absolutely awesome, it help me a lot, so i want to say thank you bu samiyati..
oiya, i just install varnish on my web, as i follow step by step, i encounter one error which is Apacher Error, No Virtual Host error. I google it for about an hour, and found the problem are conflict virtual host name between apache on ubuntu and my zpanel apache config..
the solution i found are this http://forums.zpanelcp.com/Thread-NameVirtualHost-80-has-no-VirtualHosts
i just need to place # before virtualnamehost on /etc/apache2/ports.conf and /etc/apache2/httpd.conf
i don’t know exactly which one makes the conflict but it solve the issue.. 🙂
oops typo, i mean bu Sawiyati 🙂
Hi…
I want to know where to install the cache server in the network?Is it between the router and the AAA Management Server or on the LAN side of AAA Server?
On the same subnet as your webserver(s).
Thanks… read through numerous guides and this one was by far the most straight forward with all the right steps.
Thanks for the great article!
Installed just fine with the same setup. Just wondering how to get around the Age = 0 problem, even though Varnish is running it looks like it’s not actually storing files in the cache.
Man.. this is really cool… i loved it… thanks alot