CentOSSoftware

How to install and configure Varnish with Apache on Centos 6

Varnish (HTTP accelerator) is an open source reverse HTTP proxy that you put in front of Apache web server to speed up your Apache web server 10x to 300x depends on your server setup. WordPress comes with many PHP based caching plugins like W3 Total Cache, WP Super Cache, WP Fast Cache, WP Fastest Cache and more to reduce server load, but Varnish will increase your Apache server performance significantly compare to PHP based caching plugins. So you want to speed up your website? I will show you how to install and configure Varnish with Apache on Centos 6

Varnish benefits on Apache Web Server

  • reduce CPU load on the server
  • web browser will load your website faster since cache is stored in RAM
  • load balancing supported
  • google loves fast loading websites

If you don’t have or did not install a LAMP server, you can follow my article: How to setup a LAMP server Linux, Apache, MySQL, PHP on Centos 6

Install Varnish on Centos 6 Apache Web Server

Before you isntall Varnish, you will have to install Varnish’s dependencies

$ su -
# yum update
# yum install gcc make automake autoconf libtool ncurses-devel libxslt groff pcre-devel pckgconfig libedit libedit-devel

Now you are ready to install Varnish on your Centos 6, for RHEL 6 or Centos 6, you can use this command to install Varnish

# rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm
# yum install varnish

The varnish GPG (GNU Privacy Guard) key is not available for the yum keyring yet at this time, so don’t worry about –no-signature since it’s only for initial installation.

If you get not found 404 error when adding the rpm, you can go to https://www.varnish-cache.org/installation/redhatfor the lastest Varnish package for Centos/RHEL.

To run Varnish automatically by default when the system boot/reboot

# chkconfig --level 345 varnish on

or

# /sbin/chkconfig --levels 235 varnish on

To start Varnish on your Centos 6

# service varnish start

To restart Varnish on your Centos 6

# service varnish restart

To stop Varnish on your Centos 6

# service varnish stop

Configure Varnish on Centos 6 Apache Web Server

Varnish configuration file will be locate at “/etc/sysconfig/varnish” in Centos and Fedora. You need to setup some basic basic configurations after you installed Varnish in order to make Varnish works in front of Apache on Centos.

# nano /etc/sysconfig/varnish

You will see 4 alternatives in Varnish configuration file. The default config after install Varnish will be “Alternative 3″, but we are going to use “Alternative 2″ so we are going to disable “Alternative 3″ by adding # symbol to all lines for “Alternative 3″ section.

Disabled Alternative 3 configuration should look like this with # symbol in front all every line.

## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # Main configuration file. You probably want to change it :)
#VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
#VARNISH_LISTEN_PORT=6081
#
# # Telnet admin interface listen address and port
#VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
#VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
#VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
#VARNISH_MIN_THREADS=1
#
# # The Maximum number of worker threads to start
#VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
#VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
#VARNISH_STORAGE_SIZE=1G
#
# # Backend storage specification
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
#VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
#3DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
#             -f ${VARNISH_VCL_CONF} \
#             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
#             -t ${VARNISH_TTL} \
#             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
#             -u varnish -g varnish \
#             -S ${VARNISH_SECRET_FILE} \
#             -s ${VARNISH_STORAGE}"
#

Now back to Alternative 2 which we are going to use, remove all the # symbol from DAEMON_OPTS just like mine. Also change

-a :6081 \

to

-a :80 \

Default varnish cache size is 1 GB, you should change varnish cache size according to your server / VPS (virtual private server) ram/memory size. Varnish cache size = amount of free ram/memory of your server. Note: Varnish cache can’t be more than 2 GB on 32bit system. If you want to set varnish cache more than 2 GB, you need 64bit system.

-s file,/var/lib/varnish/varnish_storage.bin,1G

to

-s file,/var/lib/varnish/varnish_storage.bin,256m

From:

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on  request.  Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -f /etc/varnish/default.vcl \
#             -u varnish -g varnish \
#             -S /etc/varnish/secret \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"

To:

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -u varnish -g varnish \
             -S /etc/varnish/secret \
             -s file,/var/lib/varnish/varnish_storage.bin,256m"

Save /etc/sysconfig/varnish Varnish configuration after you done. Different Varnish release may have different default setup enabled. You can choose to use whatever Alternative 1,2,3, or 4 but Alternative 2 is the easiest one to setup and config.

You can save Varnish cache on Disk or Ram/Memory, but Varnish cache on Ram/Memory will be much faster. You can also store Varnish cache on SSD disk or SSD cached disk, but doing that will effect your disk IO.

Next thing we are going to do is to configure /etc/varnish/default.vcl Varnish default VCL file.

# nano /etc/varnish/default.vcl

Change the default port from 80 to 8080, because Varnish will run in front of Apache which uses port 80 (Apache/web server itself runs on port 80 as default).

backend default {
  .host = "127.0.0.1";
  .port = "80";

to

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

Configure Apache to work with Varnish on Centos 6

We have installed Varnish, configured Varnish, now we need to configure Apache to work with Varnish. We changed Varnish default port to 80 to take over Apache default port to run in front of Apache, now we are going to make Apache to run behind Varnish caching by changing Apache default port to 8080.

# nano /etc/httpd/conf/httpd.conf

Search for “Listen 80″, with nano, you can use “Ctrl + w” to search. You should see something like this

#Listen 12.34.56.78:80
Listen 80

Now change port 80 to 8080 with 127.0.0.1 as your localhost IP

#Listen 12.34.56.78:80
Listen 127.0.0.1:8080

Also look and change NameVirtualHost port and VirtualHost port

Change

NameVirtualHost *:80

To

NameVirtualHost *:8080

Change (if you have more than 1 domain/sub domain or VirtualHost, don’t forget to change all those VirtualHost port to 8080)

VirtualHost *:80

To

VirtualHost *:8080

We are almost done, make sure you changed all apache virtualhost and namevirtualhost in httpd.conf to port 8080, then save httpd.conf file. We have configured Varnish and Apache, next we are going to restart both services.

To restart Apache on Centos

# service httpd restart

To restart Varnish on Centos

# service varnish restart

Check if Varnish and Apache are working

Now you should have varnish and apache running together, to make sure Varnish is on and working, you can use curl command to view http header. Note: you view your varnish/apache http header from a remote computer, not from your varnish/apache web server. If on your home / remote system does not have curl, you can install it with this commands

For Ubuntu/Linux Mint/Debian based

# apt-get update && apt-get install curl

For Fedora/Centos/Redhat based

# yum install curl

To view http header

$ curl -I http://namhuy.net

The out put

HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Wed, 26 Feb 2014 12:17:42 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Set-Cookie: __cfduid=d5e2c582998a70b15dc8654e627b9c3f51393417062854; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.namhuy.net; HttpOnly
Vary: Accept-Encoding,Cookie
Last-Modified: Wed, 26 Feb 2014 11:31:34 GMT
ETag: "1d1a-4f34d8c0b9902"
Cache-Control: max-age=3511, public
Expires: Wed, 26 Feb 2014 12:31:34 GMT
Pragma: public
X-Varnish: 1551603352 1551602628
Age: 2680
Via: 1.1 varnish
CF-RAY: 102c92e2d3ea037d-LAX

If you see something like this, means your varnish/apache web server is working right.

X-Varnish: 1551603352 1551602628
Age: 2680
Via: 1.1 varnish

Related Articles

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button