{"id":4909,"date":"2014-04-13T23:37:49","date_gmt":"2014-04-13T15:37:49","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=4909"},"modified":"2023-07-10T13:52:27","modified_gmt":"2023-07-10T13:52:27","slug":"how-to-install-varnish-cache-with-apache-on-centos-6-5","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-install-varnish-cache-with-apache-on-centos-6-5\/","title":{"rendered":"How to Install Varnish Cache With Apache On CentOS 6.5"},"content":{"rendered":"<p>Are you running a content-heavy dynamic website and looking for ways to enhance your web server&#8217;s performance? If so, <a href=\"https:\/\/webhostinggeeks.com\/blog\/varnish-mechanism-features-capabilities-advantages\/\">Varnish Cache<\/a> could be the solution you need.<\/p>\n<p>Varnish Cache, an open-source proxy server, can significantly reduce server load and increase response times by serving saved copies of pages instead of re-requesting the same revisited page from the web server.<\/p>\n<p>This tutorial will guide you through the process of installing and configuring Varnish Cache with Apache on CentOS 6.5, a popular choice for those running a VPS or a dedicated server.<\/p>\n<h2>Step-by-Step Guide to Install Varnish Cache with Apache on CentOS 6.5<\/h2>\n<p>We will start by setting up the Varnish repository, followed by the installation of Varnish and Apache. Then we configure both programs to start at boot, set Apache to listen to port 8080, and configure Varnish Cache. After that we will conclude with a performance comparison of Apache with and without Varnish Cache, demonstrating the significant improvement in response times when using Varnish Cache.<\/p>\n<h3>Setup Varnish repo:<\/h3>\n<pre>\r\n[root@centos6 ~]# wget http:\/\/repo.varnish-cache.org\/redhat\/varnish-3.0\/el6\/noarch\/varnish-release\/varnish-release-3.0-1.el6.noarch.rpm\r\n[root@centos6 ~]# rpm --nosignature -i varnish-release-3.0-1.el6.noarch.rpm\r\n<\/pre>\n<h3>Install Varnish and Apache:<\/h3>\n<pre>[root@centos6 ~]# yum install varnish -y\r\n[root@centos6 ~]# yum install httpd -y\r\n<\/pre>\n<h3>Start both at boot:<\/h3>\n<pre>[root@centos6 ~]# chkconfig --level 345 varnish on\r\n[root@centos6 ~]# chkconfig --level 345 httpd on\r\n<\/pre>\n<h3>Configure Apache to listen to port 8080:<\/h3>\n<pre>[root@centos6 ~]# vi \/etc\/httpd\/conf\/httpd.conf\r\n<\/pre>\n<p>Modify:<\/p>\n<pre>Listen 8080\r\n<\/pre>\n<h3>Configure Varnish Cache:<\/h3>\n<pre>\r\n[root@centos6 ~]# vim \/etc\/sysconfig\/varnish\r\n<\/pre>\n<pre>\r\n..\r\n..\r\n# # Main configuration file. You probably want to change it :)\r\nVARNISH_VCL_CONF=\/etc\/varnish\/default.vcl\r\n#\r\n# # Default address and port to bind to\r\n# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify\r\n# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.\r\n# VARNISH_LISTEN_ADDRESS=\r\n#VARNISH_LISTEN_PORT=6081\r\nVARNISH_LISTEN_PORT=80\r\n#\r\n# # Telnet admin interface listen address and port\r\nVARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1\r\nVARNISH_ADMIN_LISTEN_PORT=6082\r\n#\r\n# # Shared secret file for admin interface\r\nVARNISH_SECRET_FILE=\/etc\/varnish\/secret\r\n#\r\n# # The minimum number of worker threads to start\r\nVARNISH_MIN_THREADS=50\r\n#\r\n# # The Maximum number of worker threads to start\r\nVARNISH_MAX_THREADS=1000\r\n#\r\n# # Idle timeout for worker threads\r\nVARNISH_THREAD_TIMEOUT=120\r\n#\r\n# # Cache file location\r\nVARNISH_STORAGE_FILE=\/var\/lib\/varnish\/varnish_storage.bin\r\n#\r\n# # Cache file size: in bytes, optionally using k \/ M \/ G \/ T suffix,\r\n# # or in percentage of available disk space using the % suffix.\r\nVARNISH_STORAGE_SIZE=1G\r\n#\r\n# # Backend storage specification\r\nVARNISH_STORAGE=\"file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}\"\r\n#\r\n# # Default TTL used when the backend does not specify one\r\nVARNISH_TTL=120\r\n#\r\n# # DAEMON_OPTS is used by the init script.  If you add or remove options, make\r\n# # sure you update this section, too.\r\nDAEMON_OPTS=\"-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \\\r\n             -f ${VARNISH_VCL_CONF} \\\r\n             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \\\r\n             -t ${VARNISH_TTL} \\\r\n             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \\\r\n             -u varnish -g varnish \\\r\n             -S ${VARNISH_SECRET_FILE} \\\r\n             -s ${VARNISH_STORAGE}\"\r\n#\r\n..\r\n..\r\n<\/pre>\n<p>Add the following in \/etc\/varnish\/default.vcl :<\/p>\n<pre>\r\n[root@centos6 ~]# vim \/etc\/varnish\/default.vcl\r\n<\/pre>\n<pre>backend default {\r\n  .host = \"127.0.0.1\";\r\n  .port = \"8080\";\r\n}\r\n<\/pre>\n<h3>Start Varnish and Apache:<\/h3>\n<pre>[root@centos6 ~]# service varnish start\r\nStarting Varnish Cache:                                    [  OK  ]\r\n[root@centos6 ~]# service httpd start\r\nStarting httpd:                                            [  OK  ]\r\n<\/pre>\n<h3>Verify Varnish and Apache running on the correct ports:<\/h3>\n<pre>\r\n[root@centos6 ~]# netstat -plunt\r\n<\/pre>\n<pre>\r\nActive Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address               Foreign Address             State       PID\/Program name\r\ntcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1042\/rpcbind\r\ntcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      21729\/varnishd\r\ntcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1075\/sshd\r\ntcp        0      0 127.0.0.1:6082              0.0.0.0:*                   LISTEN      21728\/varnishd\r\ntcp        0      0 :::111                      :::*                        LISTEN      1042\/rpcbind\r\ntcp        0      0 :::80                       :::*                        LISTEN      21729\/varnishd\r\ntcp        0      0 :::8080                     :::*                        LISTEN      1182\/httpd\r\ntcp        0      0 :::22                       :::*                        LISTEN      1075\/sshd\r\nudp        0      0 0.0.0.0:111                 0.0.0.0:*                               1042\/rpcbind\r\nudp        0      0 0.0.0.0:793                 0.0.0.0:*                               1042\/rpcbind\r\nudp        0      0 :::111                      :::*                                    1042\/rpcbind\r\nudp        0      0 :::793                      :::*                                    1042\/rpcbind\r\n\r\n<\/pre>\n<h3>Verify Varnish running using the following command:<\/h3>\n<pre>\r\n[root@centos6 ~]# curl -I http:\/\/www.webhostinggeeks.local\r\nHTTP\/1.1 200 OK\r\nServer: Apache\/2.2.15 (CentOS)\r\nX-Powered-By: PHP\/5.3.3\r\nX-Pingback: http:\/\/www.webhostinggeeks.local\/xmlrpc.php\r\nContent-Type: text\/html; charset=UTF-8\r\nContent-Length: 7990\r\nAccept-Ranges: bytes\r\nDate: Sun, 13 Apr 2014 15:41:41 GMT\r\nX-Varnish: 1889373153\r\nAge: 0\r\nVia: 1.1 varnish\r\nConnection: keep-alive\r\n<\/pre>\n<h3>ApacheBench performance test without Varnish Cache<\/h3>\n<pre>\r\n[root@centos6 ~]# ab -k -n 1000 -c 50 http:\/\/www.webhostinggeeks.local:8080\/\r\nThis is ApacheBench, Version 2.3 &lt; $Revision: 655654 $&gt;\r\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http:\/\/www.zeustech.net\/\r\nLicensed to The Apache Software Foundation, http:\/\/www.apache.org\/\r\n\r\nBenchmarking www.webhostinggeeks.local (be patient)\r\nCompleted 100 requests\r\nCompleted 200 requests\r\nCompleted 300 requests\r\nCompleted 400 requests\r\nCompleted 500 requests\r\nCompleted 600 requests\r\nCompleted 700 requests\r\nCompleted 800 requests\r\nCompleted 900 requests\r\nCompleted 1000 requests\r\nFinished 1000 requests\r\n\r\n\r\nServer Software:        Apache\/2.2.15\r\nServer Hostname:        www.webhostinggeeks.local\r\nServer Port:            8080\r\n\r\nDocument Path:          \/\r\nDocument Length:        0 bytes\r\n\r\nConcurrency Level:      50\r\nTime taken for tests:   217.545 seconds\r\nComplete requests:      1000\r\nFailed requests:        81\r\n   (Connect: 0, Receive: 0, Length: 81, Exceptions: 0)\r\nWrite errors:           0\r\nNon-2xx responses:      1000\r\nKeep-Alive requests:    0\r\nTotal transferred:      318518 bytes\r\nHTML transferred:       20331 bytes\r\nRequests per second:    4.60 [#\/sec] (mean)\r\nTime per request:       10877.237 [ms] (mean)\r\nTime per request:       217.545 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          1.43 [Kbytes\/sec] received\r\n\r\nConnection Times (ms)\r\n              min  mean[+\/-sd] median   max\r\nConnect:        0    3  12.1      0      65\r\nProcessing:   219 10822 17202.8   3275   61139\r\nWaiting:      218 10802 17210.6   3228   61138\r\nTotal:        239 10825 17201.5   3275   61139\r\n\r\nPercentage of the requests served within a certain time (ms)\r\n  50%   3275\r\n  66%   4513\r\n  75%   5570\r\n  80%  12099\r\n  90%  40539\r\n  95%  60069\r\n  98%  60103\r\n  99%  60200\r\n 100%  61139 (longest request)\r\n<\/pre>\n<h3>ApacheBench performance test with Varnish Cache<\/h3>\n<pre>\r\n[root@centos6 ~]# ab -k -n 1000 -c 50 http:\/\/www.webhostinggeeks.local\/\r\nThis is ApacheBench, Version 2.3 &lt; $Revision: 655654 $&gt;\r\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http:\/\/www.zeustech.net\/\r\nLicensed to The Apache Software Foundation, http:\/\/www.apache.org\/\r\n\r\nBenchmarking www.webhostinggeeks.local (be patient)\r\nCompleted 100 requests\r\nCompleted 200 requests\r\nCompleted 300 requests\r\nCompleted 400 requests\r\nCompleted 500 requests\r\nCompleted 600 requests\r\nCompleted 700 requests\r\nCompleted 800 requests\r\nCompleted 900 requests\r\nCompleted 1000 requests\r\nFinished 1000 requests\r\n\r\n\r\nServer Software:        Apache\/2.2.15\r\nServer Hostname:        www.webhostinggeeks.local\r\nServer Port:            80\r\n\r\nDocument Path:          \/\r\nDocument Length:        7990 bytes\r\n\r\nConcurrency Level:      50\r\nTime taken for tests:   0.227 seconds\r\nComplete requests:      1000\r\nFailed requests:        0\r\nWrite errors:           0\r\nKeep-Alive requests:    1000\r\nTotal transferred:      8565396 bytes\r\nHTML transferred:       8221710 bytes\r\nRequests per second:    4410.08 [#\/sec] (mean)\r\nTime per request:       11.338 [ms] (mean)\r\nTime per request:       0.227 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          36888.79 [Kbytes\/sec] received\r\n\r\nConnection Times (ms)\r\n              min  mean[+\/-sd] median   max\r\nConnect:        0    1   3.2      0      19\r\nProcessing:     7   10   1.7     10      18\r\nWaiting:        1    4   2.3      4      15\r\nTotal:          7   11   4.1     10      31\r\n\r\nPercentage of the requests served within a certain time (ms)\r\n  50%     10\r\n  66%     10\r\n  75%     11\r\n  80%     11\r\n  90%     12\r\n  95%     13\r\n  98%     27\r\n  99%     31\r\n 100%     31 (longest request)\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>This tutorial provides a comprehensive guide on how to install and configure Varnish Cache with Apache on CentOS 6.5. By following these steps, you can significantly enhance your web server&#8217;s performance by reducing server load and increasing response times.<\/p>\n<p>Whether you&#8217;re running a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS<\/a> or a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated web server<\/a>, implementing Varnish Cache can provide noticeable improvements in your website&#8217;s performance.<\/p>\n<p>Did you find this guide helpful? Feel free to leave a comment below. We&#8217;d love to hear your thoughts and experiences with Varnish Cache and Apache on CentOS 6.5.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you running a content-heavy dynamic website and looking for ways to enhance your web server&#8217;s performance? If so, Varnish Cache could be the solution you need. Varnish Cache, an&#8230;<\/p>\n","protected":false},"author":6,"featured_media":5019,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1103,2055,1078],"tags":[2110,1199,2076,1712],"class_list":["post-4909","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache","category-centos","category-varnish-cache","tag-acceleration","tag-apache","tag-cache","tag-reverse-proxy"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=4909"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4909\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/5019"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=4909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=4909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=4909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}