• How to Install and Configure Varnish on CentOS?

    What is Varnish?

    Varnish is a HTTP proxy cache or an HTTP accelerator designed to cache http requests to make dynamic websites a lot faster

    Steps to install Varnish on CentOS

    1. Execute the following command to get the rpm for varnish

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

    2. Then install varnish using this command

    yum install varnish

    3. Modify the VARNISH_LISTEN_PORT=6081 line to port 80

    VARNISH_LISTEN_PORT=80

    4. Save the file and edit varnish default configuration file using the following command

    vi /etc/varnish/default.vcl

    5. Modify the default backend default port to port 81 instead, so the backend section should look like the following

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

    6. Edit the apache httpd.conf to configure apache to listen on port 81 instead

    To edit the file execute the following command

    vi /etc/httpd/conf/httpd.conf

    Modify the Listen line to look the the following

    Listen 81

    7. Modify the iptables firewall to allow connections on port 81

    Edit the iptables configuration file using the command

    vi /etc/sysconfig/iptables

    Add the following 2 lines before the COMMIT

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
    
    -A OUTPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT

    Save the file then restart iptables

    service iptables restart

    8. Configure SELinux to allow connecting to apache on port 81 then restart apache

    vi /etc/selinux/config

    Modify SELINUX=enforcing to

    SELINUX=permissive

    Save the file then restart apache using the command

    service httpd restart

    9. Configure Varnish to run on startup

    chkconfig varnish on

    Then start Varnish

    service varnish start

    Try visiting your site and refresh the page multiple times to make sure it was cached, then using firebug go to the Net tab you should see under the fetched resources that it was served by varnish

    I hope you've enjoyed this tutorial and stay tuned for more to come

    Share this post