bchmagento

Configure Varnish for Magento

NOTE: The steps below assume that Magento is already running at the root URL of the server, such as at http://magento.bitnamiapp.com/.

Follow these steps:

  • Log in to the server console using SSH.

  • Enable and start Varnish. Skip this step if you selected “Varnish” as part of the “Development Options” when creating the server. The Varnish service will start on port 81 by default.

  • Backup the Varnish configuration file:

    $ sudo cp /opt/bitnami/varnish/etc/varnish/default.vcl /opt/bitnami/varnish/etc/varnish/default.vcl.backup
    
  • Add the Magento-specific Varnish configuration by executing the following commands:

    $ bash < <(curl -s https://raw.githubusercontent.com/colinmollenhour/modman/master/modman-installer)
    $ source ~/.profile
    $ cd /opt/bitnami/apps/magento/htdocs
    $ modman init
    $ modman clone magneto-varnish https://github.com/madalinoprea/magneto-varnish.git
    
    • For Varnish < 4.0:

      $ sudo cp magento-varnish3.vcl /opt/bitnami/varnish/etc/varnish/default.vcl
      $ sudo sed -i 's/port = "8008"/port = "80"/g' /opt/bitnami/varnish/etc/varnish/default.vcl
      
    • For Varnish >= 4.0:

      $ sudo cp magento.v4 /opt/bitnami/varnish/etc/varnish/default.vcl
      $ sudo sed -i 's/port = "8008"/port = "80"/g' /opt/bitnami/varnish/etc/varnish/default.vcl
      
  • Review other settings in the file /opt/bitnami/varnish/etc/varnish/default.vcl.

  • Enable “Varnish” in the Magento administration panel under the “System -> Cache Management” menu.

  • Flush the Magento cache:

    $ sudo rm -rf /opt/bitnami/apps/magento/htdocs/var/cache/*
    $ sudo rm -rf /opt/bitnami/apps/magento/htdocs/var/sessions/*
    
  • Restart Varnish:

    $ cd /opt/bitnami
    $ sudo /opt/bitnami/ctlscript.sh restart varnish
    
  • Open port 81 in the server firewall.

  • Check if your website is being served properly by Varnish on port 81 by adding :81 to the domain name in your browser - for example, http://xyz.bitnamiapp.com:81/.

  • If Varnish is working correctly, switch the Apache and Varnish ports and activate Varnish on port 80:

    $ sudo sed -i 's/^\s*Listen .*$/Listen 81/g' /opt/bitnami/apache2/conf/httpd.conf
    $ sudo sed -i 's/\:80/\:81/g' /opt/bitnami/apache2/conf/bitnami/bitnami.conf
    $ sudo sed -i 's/at port.*"/at port 81"/g' /opt/bitnami/apache2/scripts/ctl.sh
    $ sudo sed -i 's/\:80/\:81/g' /opt/bitnami/apps/*/conf/httpd-vhosts.conf
    $ sudo sed -i 's/=80/=81/g' properties.ini
    $ sudo sed -i 's/port\s*=\s*"[^"]*"/port = "81"/g' varnish/etc/varnish/default.vcl
    $ sudo sed -i 's/VARNISH_PORT=.*$/VARNISH_PORT=80/g' varnish/scripts/ctl.sh
    $ sudo /opt/bitnami/ctlscript.sh restart
    
  • Remove the firewall rule for port 81.

Download the different files used in the example as a reference:

Last modification September 10, 2018