How to Setup Varnish HTTP Cache on an Ubuntu 18.04

Date:

How to Setup Varnish HTTP Cache on an Ubuntu 18.04 – Slow loading website can affect the overall performance of your business and even 1 second delay also can affect your online sale

How to Setup Varnish HTTP Cache on an Ubuntu 18.04

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.

Please refer Varnish Cache Documentation Page for more information

Step 1: Install Apache

Before you begin, update the package list information to ensure we are installing the new versions of software applications:

$ sudo apt-get update

Install Apache web server using the command below:

$ sudo apt-get install apache2

Press Y and hit Enter when prompted to confirm the installation.

Step 2: Test Apache Web Server

Once the Apache installation is complete, enter your Ubuntu 18.04 server IP address in a browser:

http://127.0.0.1

You should see the below default Apache web page:

Apache Landing Page
Apache Landing Page

The above page confirms that Apache is working properly.

Step 3: Install Varnish HTTP Cache

We can install Varnish HTTP Cache using the command below since Apache is up and running

$ sudo apt-get install varnish

Press Y and hit Enter when prompted to confirm the installation.

Step 4: Change Apache and Varnish HTTP Cache ports

By default Apache port is 80 and we need to change the default port to configure Apache with Varnish. Let us change the default Apache port to 8080

First, let’s assign port 8080 to Apache web server. To do this, edit the file ‘/etc/apache2/ports.conf’ file using the editor.

$ sudo nano /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Press CTRL + Xand hit Enter to save the file once you make the changes.

Please change Apache default configuration file to listen to the port 8080.

$ sudo nano /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:8080>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Press CTRL + X, Y and Enter to save the file

Restart Apache service effect changes

$ sudo service apache2 restart

Please check Apache service using your server IP address, including a new port to make sure that the Web server is working.

http: //127.0.0.1:8080

Step 5: Configure Varnish to listen on port 80

Next we will configure Varnish to listen on port 80 and forward all requests to our Apache web server.

We can do this by editing Varnish configuration file /etc/default/varnish

$ sudo nano  /etc/default/varnish

Please Change DAEMON_OPTS to port 80

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

Then, press CTRL + Xand hit Enter to save the file.

We need to edit the port on the varnish service configuration file ‘/lib/systemd/system/varnish.service’

$ sudo nano /lib/systemd/system/varnish.service

By default, it’s running under ports ‘6081’ for public address and ‘6082’ for localhost address. Check it using the netstat command below.

Please change 6081 to 80

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd
[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f$
ExecReload=/usr/share/varnish/varnishreload
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
[Install]
WantedBy=multi-user.target

Step 6: Restart Services

Please restart services using below command.

$ sudo systemctl restart apache2
$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish

Test your setup using a browser

http://public_ip_adress

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Subscribe

Popular

More like this
Related

How to Protect WordPress with Cloudflare

Cloudflare, Inc. is an American content delivery network and...

Facebook Outage

Mike Schroepfer - CTO @ Facebook. *Sincere* apologies to everyone...

Magento 2 One Page Checkout (One Step Checkout)

Magento 2 One Page Checkout - One Page checkout...