How to Install Varnish and Apache2 on Ubuntu 16

Люба х...ня

Varnish is a flexible tool for speeding up a server. I’m not going to write a lot about it, you’ll find enough information about Varnish on the Internet.

In the article, I’ll describe its installation, problems that may occur, and solutions to them.

Installing Apache 2

sudo apt-get update
sudo apt-get install apache2

Follow the link http://localhost. Go on if you see a standard parked page.

Installing Varnish

sudo apt-get install varnish

Configuring Varnish

Here’s an algorithm how to set up a connection between Apache2 and Varnish: Varnish listens on port 80, but it is connected to Apache2 through port 81.
Open up the config file:

nano /etc/default/varnish

Change the code

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

into

DAEMON_OPTS="-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -S /etc/varnish/secret \
    -t 120 \
    -s malloc,256m"

Now, we need to change the configuration for Varnish VCL.

nano /etc/varnish/default.vcl
backend default {
    .host = "127.0.0.1";
    .port = "81";
    }

Basic configuration for Varnish is done.

Apache2 configuration

Apache2 has to listen on port 81.

sudo nano /etc/apache2/ports.conf

By default, it listens on port 80, so we replace 80 with 81

NameVirtualHost 127.0.0.1:81
Listen 127.0.0.1:81

The last thing is to change the virtual host. For this article, I’ve created a new DigitalOcean droplet, so this is the default host (000-default.conf). If you have other hosts, don’t forget to change settings for them.

nano /etc/apache2/sites-enabled/000-default.conf

Change the port


 ... 

Now, restart Apache2 and Varnish to get the config files working.

sudo service apache2 restart
sudo service varnish restart

And check the result.

Don’t worry if you get ERR_CONNECTION_REFUSED. First, let’s see what port are used by Varnish.

ps aux | grep vcache
vcache 15569 0.0 0.7 125044 7816 ? Ss 08:20 0:00 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

        vcache 15581 0.0 9.3 272012 94900 ? Sl 08:20 0:00 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

As you can see, port 6081 is still in use. The problem has been detected in this file:

nano /lib/systemd/system/varnish.service

Replace:

[...]
    ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
[...]

with

[...]
    ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
[...]

Restart services again.

systemctl daemon-reload
systemctl restart apache2.service
systemctl restart varnish.service

To review the statistics, run the command:

varnishstat

And the final test:

curl -I http://sever_ip 

 

HTTP/1.1 200 OK
    [...]
    Content-Type: text/html
    X-Varnish: 32771
    Age: 0
    Via: 1.1 varnish-v4
    [...]
Let’s fill the brief, shall we?