nativeInstallerwordpress

Configure Varnish for WordPress

Before running the commands shown on this page, you should load the Bitnami stack environment by executing the installdir/use_APPNAME script (Linux and Mac OS X) or by clicking the shortcut in the Start Menu under “Start -> Bitnami APPNAME Stack -> Application console” (Windows). Learn more.

NOTE: The steps below assume that WordPress is already running at the root URL of the server, such as at http://wordpress.example.com/, and that Varnish is enabled. Varnish is disabled by default, but you can enable it by following the instructions in our Varnish guide.

Download the different files used in the example:

Apache

Follow these steps:

  • Log in to the server console using SSH.
  • Backup the Varnish configuration file:

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

    • For Varnish < 4.0:

      $ sudo cp wordpress.vcl installdir/varnish/etc/varnish/default.vcl
      $ sudo sed -i 's/port\s*=\s*"[^"]*"/port = "80"/g' installdir/varnish/etc/varnish/default.vcl
      
    • For Varnish >= 4.0:

      $ sudo cp wordpress.v4 installdir/varnish/etc/varnish/default.vcl
      $ sudo sed -i 's/port\s*=\s*"[^"]*"/port = "80"/g' installdir/varnish/etc/varnish/default.vcl
      
  • Restart Varnish:

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

Nginx

Follow these steps:

  • Log in to the server console using SSH.
  • Backup the Varnish configuration file:

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

    $ sudo cp wordpress.v4 installdir/varnish/etc/varnish/default.vcl
    $ sudo sed -i 's/\.port = "8080"/\.port = "80"/g' installdir/varnish/etc/varnish/default.vcl
    
  • Restart Varnish:

    $ sudo installdir/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 Nginx and Varnish ports and activate Varnish on port 80:

    $ sudo sed -i 's/listen .*80;$/listen 81;/g' installdir/nginx/conf/bitnami/bitnami.conf
    $ sudo sed -i 's/=80/=81/g' installdir/properties.ini
    $ sudo sed -i 's/port\s*=\s*"[^"]*"/port = "81"/g' installdir/varnish/etc/varnish/default.vcl
    $ sudo sed -i 's/VARNISH_PORT=.*$/VARNISH_PORT=80/g' installdir/varnish/scripts/ctl.sh
    $ sudo installdir/ctlscript.sh restart
    
  • Remove the firewall rule for port 81.

Last modification December 21, 2018