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 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
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
Why install varnish with wordpress
It will reduce server load , by reducing CPU work
It will load your load your WordPress website very fast, because vanish cache stored in RAM.
Your wordpress site SEO will increase, Because your site will load so fast
Response time will reduce by 1000x
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.
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.
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
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
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.
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.
You will get response as shown in following image.
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
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
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.
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.
My name is Purab Kharat, Here is my Personal Blog: wpapi.com. Work as an Senior Technical Lead at Smartsourcing Global Pvt Ltd in Princeton, NJ, USA. I contributor of many wordpress plugins and Drupal Modules. I am core developer of Drupal. Connect to on LinkedIn or Follow @purab on Twitter or Follow me on Google+ or Write Mail to firstname.lastname@example.org or Chat with me on Skype : purabdk