How To Install Varnish To Run With Apache On CentOS

How to install Varnish on CentOS

Step 1 – Install the latest version of Varnish on CentOS using the following command:

1
sudo yum install varnish

How to Configure Varnish to work with Apache

Step 1 – Now setup the basic configuration to make Varnish work with Apache. In CentOS and Fedora, Varnish configuration file is located in “/etc/sysconfig/varnish”. You can use either ‘vi’ or ‘nano’ editor to edit the file:

1
sudo nano /etc/sysconfig/varnish

Step 2 – Down the page and you’ll notice 4 alternative configurations available in the file and the currently active one is “Alternative 3″. Add # symbol to all lines under “Alternative 3″ section which will disable this section. It should look like this:

Step 3 – Now add following code in “Alternative 4″ config.

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

It should look like this:

Once you are done with editing, press Ctrl+O to save, then Ctrl+X to exit.

Step 4 – Now configure Varnish default VCL file located in “/etc/varnish/”. The file holds the configuration to tell varnish where to look for the webserver content (in this case, it is to fetch from Apache in port 8080). Use vi or Nano editor to edit the file:

1
sudo nano /etc/varnish/default.vcl

In that file you can set the backend server, I mean the server that Varnish will look for the content.

Change the following default value:

change the default port to 8080.

1
2
3
4
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

It should look like this:

Once you are done with editing, press Ctrl+O to save, then Ctrl+X to exit.

How to configure Apache to work with Varnish

Step 1 – Open the Apache default configuration file:

1
sudo nano /etc/httpd/conf/httpd.conf

Step 2 – Then look for the line “Listen 80″ and change it to “127.0.0.1:8080″:

so it will look like:

Step 3 – Also look for the line “NameVirtualHost *:80″ and change it to “NameVirtualHost 127.0.0.1:8080″:

so it will look like:

Step 4 – Now scroll down to the bottom of  the page and check for the VirtualHost entry for your website. Change it to “127.0.0.1:8080″:

so it will look like:

Once you are done with editing, press Ctrl+O to save, then Ctrl+X to exit.

Step 5 – Finally restart Apache and Varnish service.

Command to restart Apache:

1
sudo service httpd restart

and command to restart Varnish:

1
sudo service varnish restart

Have something to Add?

Loading Facebook Comments ...
Loading Disqus Comments ...