how to install varnish with wordpress and apache

There are many caching solution available in open source. But Varnish is best in among. In this article, We given full steps to use and install varnish with wordpress and apache server . varnish setup with your wordpress site. Many website webmasters installed Varnish on their production servers to handle millions of requests without delay in response. There are many caching plugin like W3 Total Cache, WP Super Cache, Hyper Cache and so many..

Varnish is open source software

Varnish is free open source software licensed under a two-clause BSD licence aimed to really super boost server performance. In short, it will work as web accelerator between any server that speaks with HTTP (Apache or any other).

how to install varnish with wordpress and apache

how to install varnish with wordpress and apache

Why Apache or Nginx is Enough to Handle Huge Traffic
Many system administrator do many tricks with Apache because it performs really bad when the server is getting very high requests. Some people started using other servers like Ngnix and LiteSpeed. WordPress itself are using Ngnix server for many years for production environment. But still performance and response time of website is not that much fast with these plugins. So Best solution is Varnich Cache.

Varnish Explained Here

Vanish server started working on top of Apache server. When HTTP request comes to server first it will go the varnish server, If Vanish server has the response in memory cache then it will give response immediately, If Varnish server does not has cache for that request then it will go to apache server and get the response and keep in cache. Next request will delivered by Varinish cache, which will be so fast. Using varnish cache you can increase your server performance and response time by 300 to 900x. I personally checked response time difference. Before installing varnish cache my server response time was 3.46 second. After installing Varnish Cache, my server response time was 0.3, Means My server response increased by 1000x.

In following diagram, we explained, how varnish server process request.

varnish processing request compressed, how to install varnish with wordpress and apache

varnish processing request compressed, how to install varnish with wordpress and apache

Why install varnish with wordpress

  1. It will reduce server load , by reducing CPU work
  2. It will load your load your WordPress website very fast, because vanish cache stored in RAM.
  3. Your wordpress site SEO will increase, Because your site will load so fast
  4. Response time will reduce by 1000x
  5. Google page speed response score improves by at-least 20%

how to install varnish with wordpress and apache

Note: You can use this tutorial for setup varnish with any apache website. If you used HTML, ROR, JAVA, PHP for your website than you can set up varinish as we shown in this article. Here, we are assuming you already installed wordpress with apache server on linux server.

We are using dedicated Linux (centos) server for hosting our sites. So here we given steps can be applicable for Red Hat, Centos, Fedora, ubuntu server. But for ubuntu server you need use “apt-get install” instead of yum install.

So now Just follow my steps to install the varnish server. For latest version of Linux, you don’t need to install repository. But for older linux version you need to install the Varnish server repository. For that use following command to install varnish repository.

rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.noarch.rpm

Than you will be ready for install varnish server. Use following command for installing varnish server. You need to run following command as root. So first became root.

yum install varnish

Configure Varnish to work on port 80

usually Apache run on port 80 and your all sites will run on port 80. So as I told you earlier in this article. (check Varnish Explained section) Varnish work on top of apache server So you need to set up varnish on port 80. In CentOS, RedHat, Fedora, and Ubuntu server, Varnish configuration file located in “/etc/sysconfig/varnish”. You can edit file using vim or vi editor or you can download file to your PC and edit it then upload to server.

I am using vim here.


vim /etc/sysconfig/varnish

Please change files first part code as per I shown in image. OR you can download setting file from here:

Download Link is Hidden! It's Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.
  • Change Default Varnish Config file

Got to “Alternative 2” section and change setting as I shown in image. Un-comment all the lines after “Alternative 2” as I shown.

By default -s setting will be pointed to filesystem, change that to memory. This is important, because we want, varnish give response from memory.

you need to change “-a : 6081 \” to “-a : 80 \” as we shown here in image. You can change “256m” to higher number, which is depends on your server configuration. If you have higher memory server then you can 512mb or 1GB for Varnish server. Many websites are using 4gb for varinsh servers. But they have a huge amount of traffic.

varnish configuration, install varnish with wordpress and apache

varnish configuration, install varnish with wordpress and apache

 Second Part of configuration – go to end of file, using “shit+g” key. Change configuration as per I shown.

Got to “Alternative 3” section and change setting as I shown in image. Comment all the lines after “Alternative 3”

varnish default config , install varnish with wordpress and apache

varnish default config , install varnish with wordpress and apache

  •  Change server config of Varnish

Like Apache server, Varnish server VCL file found in “/etc/varnish/default.vcl” location. Varnish VCL file is configuration for looking for apache(server) response data(content). You can edit this file using vim editor as per shown in following image OR you can download file from following location.

Download Link is Hidden! It's Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.

 

varnish server config, install varnish with wordpress and apache

varnish server config, install varnish with wordpress and apache

Change the port 80 to 8080 port as We shown in image. Means Varnish server will generate cache from port 8080 so you need configure your apache server on port 8080.


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

Now Varnish configuration is complete. Now we need to configure apache server to send apache in back of varnish server.

You need to change following setting of your apache server. By default apache config file is htttpd.conf or apache.conf file. You will find this file in “/etc/httpd/conf/httpd.conf” location. For ubuntu that will be other location. Open your apache config file.

You can download apache configuration file from here. You can refer only “custom.conf” file. it has all changed configuration.

https://github.com/purab/varnish/blob/master/custom.conf
https://github.com/purab/varnish/blob/master/httpd.conf

We created custom.conf file and added under “/etc/httpd/conf.d/” folder. So that file will be automatically loaded in apache config.

You can add following code in your apache configuration file as we shown in following image. You need to change the NameVirtualHost 80 to 8080. So your apache server will run on port 8080.

apache server config with varnish,  install varnish with wordpress and apache

apache server config with varnish, install varnish with wordpress and apache

You need to change VirtualHost entries for each of your domain. This is very important, If you do not change this properly then your apache server will through error while restarting.


NameVirtualHost *:8080

#Browser cache code
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip
BrowserMatch \bMSIE\s7 !no-gzip !gzip-only-text/html
BrowserMatch \bOpera !no-gzip
Header append Vary User-Agent

#wpapi.com
<VirtualHost *:8080>
ServerAdmin purabdk@gmail.com
DocumentRoot /home/siwan/wpapi.com
ServerName wpapi.com
ServerAlias www.wpapi.com
ErrorLog logs/wordpressapi-error_log
CustomLog logs/wordpressapi-access_log common
</VirtualHost>

 

  •  Now your Varnish and Apache configuration are done. So time to restart both servers.

First Stop the Apache server and start the Varnish server. After starting Varnish server. Again start the apache server. You can use following commands step by step. Use commands in following order.


service httpd stop

service varnish start

service httpd start

Note: If you try to start varnish server without stopping apache server then it will never start because it will try to start using port 80.

  • Test Website performance and response time

How can you test your site pages speed and response time. Go to following “Google pagespeed” URL to test the website response time.

PageSpeed Insights 

Check Varnish service is running for your site. go to following URL and add your website URL in inputbox and select “service scan” checkbox and click on go button.

http://centralops.net/co/domaindossier.aspx

You will get response as shown in following image.

varnish-detection-response

As per explained in this article. After varnish configuration, your website will perform much faster then earlier and server CPU will not be consumed by apache server. We are using small dedicated server so we used only 256mb memory. If you have 1gb RAM of our server then use 256mb means one-fourth of memory for varnish server. When you have all code and database of same server.

Varnish Useful commands

# varnishlog : Provides detailed information on requests.
# varnishtop : It reads varnishd shared memory logs and presents a most commonly occurring log entries.
# varnishadm : Command-line varnish administration used to reload vcl and purge urls.
# varnishstat : Provides all the info you need to spot cache misses and errors.
# varnishhist : Provides a histogram view of cache hits/misses.

By “varnishstat” command you can check real-time statistics of varnish server. You can see following items

  • Client connection
  • Hit rate ration
  • Hit rate average
  • Number of client connections accepted

Following is screenshot of my production site. There are a lot of useful data provided by varnish stats

varnish varnishstat, install varnish with wordpress and apache

varnish varnishstat, install varnish with wordpress and apache

 

Create Varnish Log File

Using following command you can create varnish request log file as per apache log file. In this file you will get similar information like apache access log.


varnishncsa -c -a -d -w /var/log/varnish/varnish.log

For more varnish commands you can go to varnish site using following URL.

https://www.varnish-software.com/static/book/Getting_started.html#the-management-interface

Now you are set with varnish server and wordpress site. Above article can be used for any linux distribution like, Fedora, Ubuntu, Centos, RedHat and Suse. Commands will be same and configuration will be same.

In wordpress configuration you do not need to change anything. With Varnish server .htaccess file will not break because still main webserver is  apache.

If you still need assistance to install Varnish server and configure production server then contact to me.

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *