Skip to main content

Installing Varnish to increase the speed of the page load.

Install varnish using the command below

yum install varnish

Your response should look something like

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.realcompute.io
 * epel: epel.mirror.digitalpacific.com.au
 * extras: mirror.realcompute.io
 * updates: centos.mirror.digitalpacific.com.au
Resolving Dependencies
--> Running transaction check
---> Package varnish.x86_64 0:4.0.5-3.el7 will be installed
--> Processing Dependency: varnish-libs(x86-64) = 4.0.5-3.el7 for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: jemalloc for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: redhat-rpm-config for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: gcc for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.0)(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.1)(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.2)(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.3)(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnish.so()(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1()(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvarnishcompat.so()(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvcc.so()(64bit) for package: varnish-4.0.5-3.el7.x86_64
--> Processing Dependency: libvgz.so()(64bit) for package: varnish-4.0.5-3.el7.x86_64

Note: the above output is showing the initial part of the response and not the complete response.

 

Start and Enable Varnish

Now Varnish will be successfully installation and we need to ensure that it is up and running.  

Start Varnish - run the command
systemctl start varnish

Enable Varnish - on boot run

systemctl enable varnish

 

Confirm Varnish is running

To confirm that Varnish is running, execute the command

systemctl status varnish

Response

varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-04-01 12:20:57 AEDT; 15s ago
 Main PID: 14987 (varnishd)
   CGroup: /system.slice/varnish.service
           ├─14987 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :6081 -T 12...
           └─14989 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :6081 -T 12...

 

You can view more information such as the release, version and install data using the command

rpm -qi

 

Configure Varnish

After installing Varnish, you'll need to configure some varnishd runtime parameters.

Systemd configuration - The varnishd process is managed by Systemd and has its unit file in /usr/lib/systemd/system/varnish.service. By way of example:

[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
After=network.target

[Service]
# If you want to make changes to this file, please copy it to
# /etc/systemd/system/varnish.service and make your changes there.
# This will override the file kept at /lib/systemd/system/varnish.service
#
# Enviroment variables may be found in /etc/varnish/varnish.params
#

# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
LimitMEMLOCK=82000

# On systemd >= 228 enable this to avoid "fork failed" on reload.
#TasksMax=infinity

# Maximum size of the corefile.
LimitCORE=infinity

EnvironmentFile=/etc/varnish/varnish.params

Type=forking
PIDFile=/var/run/varnish.pid
PrivateTmp=true
ExecStartPre=/usr/sbin/varnish_pre
ExecStart=/usr/sbin/varnishd \
        -P /var/run/varnish.pid \
        -f $VARNISH_VCL_CONF \
        -a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
        -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
        -S $VARNISH_SECRET_FILE \
        -u $VARNISH_USER -g $VARNISH_GROUP \
        -s $VARNISH_STORAGE \
        $DAEMON_OPTS

ExecStartPost=/bin/sleep 0.5
ExecReload=/usr/sbin/varnish_reload_vcl

[Install]
WantedBy=multi-user.target

 

In the above output, you will see that the parameters are not set in the code.  For instance, something similar to

ExecStart=/usr/sbin/varnishd \
      -a :6000 \
      -a localhost:8443,PROXY \
      -p feature=+http2 \
      -f /etc/varnish/default.vcl \
      -s malloc,256m

If you have the same situation, you will most likely find the parameters for Varnish in the varnish.params file.

/etc/varnish/varnish.params

While i had a sense that variables might exist here, I confirmed by running a find command:

grep -iRl "VARNISH_LISTEN_PORT" /etc/

The response of the find command was

/etc/systemd/system/varnish.service
/etc/systemd/system/multi-user.target.wants/varnish.service
/etc/varnish/varnish.params

 

Nginx or Apache

Apache

If you’re using Apache, you will need to replace the listen port value in /etc/httpd/conf/httpd.conf from Listen 80 to Listen 8080. You also need to replace <VirtualHost *:80> with <VirtualHost *:8080> in all virtual host files.

Using the following command it will action this for all .conf files in the /etc/httpd folder, including its subfolders:

sudo find /etc/httpd -name '*.conf' -exec sed -r -i 's/\bListen 80\b/Listen 8080/g; s/<VirtualHost ([^:]+):80>/<VirtualHost \1:8080>/g' {} ';'

Nginx

If you’re using Nginx, change the listen port values using the following command:

sudo find /etc/nginx -name '*.conf' -exec sed -r -i 's/\blisten ([^:]+:)?80\b([^;]*);/listen \18080\2;/g' {} ';'

This command will replace listen 80; with listen 8080; in all .conf files in the /etc/nginx/ folder and all of its subfolders.

 

Configure Varnish as a reverse proxy for Nginx

As Varnish sits in front of the web server to serve HTTP requests, you'll need to change the default Nginx port to port 8080 and then configure Varnish to listen on port 80.  To do this, open the Nginx configuration file:

vim /etc/nginx/nginx.conf

Locate the Server block and make the changes as shown

.....
server {
        listen       8080 default_server;
        listen  [::] 8080 default_server;
        ....
}

Save and exit the text editor.  For these changes to kick in you will need restart Nginx.  Execute the command

systemctl restart nginx

Now Nginx should run on port 8080

Next, go to the Varnish configuration file

vim /etc/varnish/default.vcl

Locate the backend default block and ensure it resembles the lines shown below

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

.host = backend web server IP address.
.port = backend webserver port it is running on.

Save and exit the text editor.

We need to configure Varnish to listen to port 80.  This will achieved through editing

vim /etc/varnish/varnish.params

Change the value of VARNISH_LISTEN_PORT line to HTTP port 80.

VARNISH_LISTEN_PORT=80

 

Restart the services

As changes have been made to various configuration files we now need to action a command so these take effect.  THis is achieved by restarting Varnish and your web server.

Apache

Run the following command if your web server is running Apache:

sudo systemctl restart httpd varnish
Nginx

Run the following command if you’re using Nginx instead of Apache:

sudo systemctl restart nginx varnish

 

Test time

So you have made a suite of changes to your site.  Now you need to test how these changes have kicked in.

Related articles

Andrew Fletcher17 Aug 2022
How To Install Apache Solr 8.11 on CentOS/RHEL 7
Looking to install Solr on your server?&nbsp; Not sure if it worth the effort?&nbsp; Well, Apache Solr is the open-source, popular, super fast open source enterprise search platform from the Apache Lucene project.&nbsp; Written in Java, Solr is highly scalable, providing fault-tolerant distributed...
Andrew Fletcher07 Apr 2022
Discovering Drupal caches
The cache system in Drupal 9 delivers the API with the elements required for creation, storage and invalidation of cached data. &nbsp;Drupal is structured so initially data is stored in the database. &nbsp;Whereas, files are stored in the following...
Andrew Fletcher13 Mar 2022
Mac: how to clear cache
Clear DNS Cache The DNS (Domain Name Service) cache on your Mac helps browsers locate and connect to web addresses quickly. If your browser Safari or Chrome still fails to load websites, this can be the result of an obsolete DNS cache may be causing the issue. In this instance you want to focus...