Limited Time

Cheap Resseller Hosting starting at just $5.95/mo

Installing Varnish on cPanel

installing-varnish-on-cpanel

Following up on our last post on how to optimize your VPS for wordpress performance, in this post we will go over installing and setting up Varnish on your cPanel server.

A quick reminder of what varnish is.

Varnish is a web cache that runs on linux, it listens on port 80 (the usual HTTP port) and connects to an web server such as Apache running on al alternate port on the server, such as 8080. Varnish stores / caches the copies of your pages and when requested by a visitor it serves those pre-built pages without having to process them over and over again.

Read more: https://host4geeks.com/optimizing-your-server-for-wordpress/

Since Varnish listens on port 80 (HTTP), we will first need to change the port for Apache running on our server. We will be changing the Apache port to 8080. To do this, login to your WHM as root, click on Tweak Settings from the sidebar, head over to the System tab and change the Apache Non-SSL port to 8080.

138.128.172.66

Now, we can proceed with the installation of Varnish. You need to be logged in as root via SSH. Type:

yum install varnish

Once the installation completes, we will configure Varnish to listen on port 80.

Edit the file /etc/sysconfig/varnish using your preferred editor.

nano /etc/sysconfig/varnish

VARNISH_LISTEN_PORT=80

Varnish is not a web server, it is merely a proxy sitting in front of a web server such as Apache, this implies that you would still need to run Apache on an alternate port on your server (such as 8080) and varnish in turn connects to Apache on the backend and listens on port 80 in the front end.

We now need to configure Varnish to connect to Apache on port 8080. Type in the following:


cd /etc/varnish/
mv default.vcl default_bak.vcl
touch default.vcl #creating a new default config file
nano default.vcl

Paste the following code.

backend default {
.host = "<your server's IP address>";
.port = "8080";
}

sub vcl_recv {
if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
return(lookup);
}
}

# strip the cookie before the image is inserted into cache.

sub vcl_fetch {
if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
unset beresp.http.set-cookie;
}
}

Make sure you replace the <your server’s IP address> with the IP address of your server. Finally, enter:

chkconfig varnish on

service varnish start

That is it,  you now have varnish running on your server.

12 Comments

  1. Huy N Reply January 22, 2015 - 11:51 pm

    That’s great that you guys post these tutorials! I do appreciate them a lot!

    I just have a question: I’m currently have APC, nginx as cache and Apache for dynamic files. Should I still install Varnish? Is Varnish kinda like APC? Which one should I go for?

    • host4geeks Reply January 23, 2015 - 4:30 pm

      Thanks, we are glad that you like these tutorials, if there is any specific you want us to cover, please let us know.

      You should still have Varnish for the best performance, APC is mostly for caching PHP scripts compilations, thus making the pages load faster, a combination of Varnish+APC is a very good solution.

      You cannot run Nginx and Varnish together an a reverse proxy, however, you can run Nginx as a webserver and put varnish in front of it. But do note that Apache .htaccess directives will not work with Nginx.

      • Huy N Reply January 23, 2015 - 11:31 pm

        Does cPanel play along nicely with nginx by any chance though? I depend so much on cPanel, but Apache is… well, slow as a snail.

        You guys should make a tutorial on how to be such a great web hosting with attentive customer supports like that lol. I admire and appreciate your business a lot 🙂

        • host4geeks Reply January 23, 2015 - 11:46 pm

          It is not supported by cPanel officially, however there are a couple of 3rd party plugins. Although, in our experience Varnish + Apache does the job.

          It is a pleasure to be able to serve clients as yourself.

  2. JyothisKumar Sathyanesh Reply February 17, 2015 - 6:32 pm

    Hi Kushal,
    Nice info and tutorial. Can it be done it on any specific account under shared server or the server should be compile with varnish in default?

  3. Ljubisa Reply June 2, 2015 - 1:16 pm

    What happens with the Apache access log, AwStats and Webalizer since Varnish wont log user requests by default and only requests that are pulled from the backend are logged on Apache. That will break analytic plugins in cPanel as well as bandwidth usage. Do you have idea how to solve this issue?

  4. ZILZAL Reply June 25, 2015 - 11:59 am

    In centos 6.6 , when trying to install Varnish it says No package available , what is the latest version of varnish and what is the best source to get it ?

  5. Dave Reply March 22, 2016 - 3:03 am

    do we need to change anything in the httpd.conf (virtual servers)? or does changing the tweak settings rebuild it?

  6. Shaker Reply June 14, 2016 - 12:01 pm

    That’s great , but I’ve a question when I add a new user from Cpanel I should add virtual host for it on /etc/varnish/default.vcl

  7. Dedicated Proxies Reply May 31, 2017 - 2:43 pm

    Hi i am installing Varnish with W3total cache. But it seems like nothing changed in my wordpress blog. I do not know what exactly i missing in setting up varnish. But i found here something new i will try it at my blog. and hope it works for me. by the way this is nice information and problem solving blog. Such type of things or tutorial are gets rid from long time facing problem, as i facing setting up varnish for my blog. Keep updating and keep helping.



Leave a Reply