In this tutorial we can check how to Configure and Install Varnish on CentOS/Ubuntu server.
Varnish is the most powerful open source HTTP engine/reverse HTTP proxy that has the capability to increase the site speed, especially when there is high traffic on the site. Varnish Cache visits your page only once for caching the page and all the future request for that page will be served by varnish cache
Install varnish
Before installing varnish, we need to add varnish repositories to the server.
For Ubuntu Users
For CentOS Users
Configuring Varnish on both Ubuntu and CentOS
After installation, we need to configure varnish to reduce the load on the server.
For Ubuntu Users
Search and uncomment the lines under “DAEMON_OPTS” on the varnish configuration file /etc/default/varnish and edit the lines similar to the following.
After saving this file open varnish default file which tells varnish to look for server content. Edit this file to tell varnish to look for the content on port 8080. The code will be similar to the below example.
For CentOS users.
Edit varnish configuration file to change the varnish port.
Save the file and open the default file of varnish to look the content on port 8080. The code should be similar to the following example.
Configure apache to work with varnish
By default, apache is listening to port 80. We have to change the port for apache to 8080 to run it behind varnish.
For Ubuntu Users.
Edit the port.conf file to change apache port to 8080. The file should be similar to the example below.
Also, edit the apache configuration file to change the port.
Save this file and restart both services to enable the changes.
For CentOS users
Edit apache configuration file to change the port to 8080.
Restart both services to reflect the changes.
After the installation, you can verify whether the vanish is working or not by using the following command.
Then the output should be something similar to the following:
Varnish is the most powerful open source HTTP engine/reverse HTTP proxy that has the capability to increase the site speed, especially when there is high traffic on the site. Varnish Cache visits your page only once for caching the page and all the future request for that page will be served by varnish cache
Install varnish
Before installing varnish, we need to add varnish repositories to the server.
For Ubuntu Users
# curl [URL]http://repo.varnish-cache.org/debian/GPG-key.txt[/URL] | sudo apt-key add –
# apt-get update
# apt-get install varnish
For CentOS Users
# yum install -y epel-release
# yum update
# yum install -y varnish
Configuring Varnish on both Ubuntu and CentOS
After installation, we need to configure varnish to reduce the load on the server.
For Ubuntu Users
Search and uncomment the lines under “DAEMON_OPTS” on the varnish configuration file /etc/default/varnish and edit the lines similar to the following.
# vi /etc/default/varnish
DAEMON_OPTS=”-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m”
# vi /etc/varnish/default.vcl
backend default {
.host = “127.0.0.1”;
.port = “8080”;
}
For CentOS users.
Edit varnish configuration file to change the varnish port.
# vi /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80
# vi /etc/varnish/default.vcl
backend default {
.host = “127.0.0.1”;
.port = “8080”;
}
Configure apache to work with varnish
By default, apache is listening to port 80. We have to change the port for apache to 8080 to run it behind varnish.
For Ubuntu Users.
Edit the port.conf file to change apache port to 8080. The file should be similar to the example below.
# vi /etc/apache2/ports.conf
NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080
# vi /etc/apache2/sites-available/default
<VirtualHost 127.0.0.1:8080>
# service apache2 restart
# service varnish restart
For CentOS users
Edit apache configuration file to change the port to 8080.
# vi /etc/httpd/conf/httpd.conf
Listen 8080
# systemctl restart varnish
# systemctl restart httpd
# curl -I http://localhost
HTTP/1.1 200 OK
Date: Wed, 04 Nov 2015 10:21:07 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 02 Oct 2015 10:36:53 GMT
ETag: “6c-5211cdbf61c14”
Content-Length: 108
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish-v4
Connection: keep-aliveHTTP/1.1 200 OK
Date: Wed, 04 Nov 2015 10:21:07 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 02 Oct 2015 10:36:53 GMT
ETag: “6c-5211cdbf61c14”
Content-Length: 108
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive[/INDENT]