Install Varnish 3 To Run With Nginx On Ubuntu Server

Wednesday, August 17th 2016. | how to

#howto ,#tutorial #VPS – Install Varnish 3 To Run With Nginx On Ubuntu Server – This tutorial explains step by step guide (with pics) about Install Varnish 3 To Run With Nginx On Ubuntu Server

Complete guide how to install Varnish Cache 3 HTTP accelerator and configure it to run with Nginx on Ubuntu server. But before you read much further, if I may, I suggest you to use Nginx itself without Varnish. Why? Because without the help of Varnish, Nginx is already pretty good at handling static files. But however the decision is up to you because some users still prefer to put Varnish in front of Nginx as they believe it can increase server performance.

Few Words About Varnish

Both Nginx and Varnish Cache are having purpose to boost a server performance as well as speed the web load time. But however both are different to each other. Nginx is a web server just like Apache but it is faster while Varnish is a HTTP accelerator so it can also run side by side with either Nginx or Apache. I’ve posted the step by step tutorial about how to install, setup and configure Varnish in front of Apache on Ubuntu server. Remember that each time I say “server” it refers to both VPS or Dedicated server.

Why use Varnish?

  1. To reduce server load, especially CPU works.
  2. To make a website load faster, because cached and stored in RAM.
  3. To gain more visitors to your website.

Read more about Varnish at WikiPedia to gain more information and to understand how exactly Varnish works in boosting your 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 Nginx on your server or read my previous guide to setup Nginx and PHP5 on your server.

Setup and Configure Varnish with Nginx

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 – Ubuntu is coming with Varnish package already but it might be outdated. The solution is to add official Varnish repository url to your server to fetch newer version. Follow the steps below:

Type this command syntax:

curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

Screenshot taken from my server:

varnish repo

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:

varnish add repo

Once done hit Control+O to save then Control+X to exit Nano editor. Now issue command syntax below to update then install Varnish:

apt-get update

screenshot:

apt get update

Then issue this, the main command syntax to install Varnsih:

apt-get install varnish

It you’ve been asked to continue, simply type Y then hit Enter:

apt-get install varnish

Wait few seconds for the installation process to finish with output something like this:

installed varnish

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 Nginx 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” section:

AEMON_OPTS="-a :6081 
             -T localhost:6082 
             -f /etc/varnish/default.vcl 
             -S /etc/varnish/secret 
             -s malloc,256m"

edit varnish config

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:

edit varnish port 80

Once done hit Control+O to save then Control+X to exit.

That configuration tells Varnish to 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.

Step 4 – Now you’ll also need to specify the backend server or in other words to tell Varnish where to look for the webserver content (in this case is to fetch from Nginx on port 8080 which you’ll also configure it later in next step below). Simply edit the “default.vcl” file:

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";
}

# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

The config above says that Nginx 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 Nginx. Pay attention to the “# Drop any cookies sent to WordPress” and “# Drop any cookies WordPress tries to send back to the client” section. Those two lines tell Varnish to drop all the cookies that don’t relate to the admin side of the WordPress site and to remove the cookies that make WordPress very difficult to cache. However if you don’t use WordPress so you can simply ditch those two sections and use this only:

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

varnish setup file

Now you can exit Nano editor screen.

Step 5Configure Nginx to work and listen on port 8080. Without doing this step, Apache will still run on port 80 and so Varnish. So you have to also make Nginx to work on port 8080 by editing Virtual Hosts file of corresponding domain.

nano /etc/nginx/sites-available/domain.com

Obviously you have to change “domain.com” to your own domain name.

Now find following line:

listen  127.0.0.1:80;

then change it to:

listen  127.0.0.1:8080;

Or if you don’t see that line simply add it there so the updated line should look like this:

server {
        listen  127.0.0.1:8080;

Screenshot taken from my server:

virtual hosts file

Once done save it by hitting Control+O on your keyboard then Control+X to exit Nano editor screen.

Step 6 – Now delete default Virtual Hosts file of Nginx located at “/etc/nginx/sites-enabled/default”. Issue this command:

rm /etc/nginx/sites-enabled/default

is it safe? Yes it is safe enough to remove the default enabled virtual host because the configuration template remains in the sites-available directory.

Step 7 – Now test Nginx config and restart the service using this command:

nginx -t

Should look like this:

test nginx config

this command to restart:

service nginx restart

it gives you output like this:

restart nginx

Step 8 – Now restart Varnish as well:

service varnish restart

It should give you the OK message:

restart varnish

Done.

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.

test server response

output (in the bottom):

test varnish is running

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

It looks like this:

varnish stats

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.

Also do not forget to follow me on Twitter to get faster update.

This post Install Varnish 3 To Run With Nginx On Ubuntu Server is part of ServerMom.

Source link