Varnish as a frontend for a remote WordPress install – Part 1

This tutorial assumes two sites, varnish.chris.example.com and web.chris.example.com. Both of my test sites will be created on DigitalOcean but on separate instances. Both will be running basic Ubuntu 14.04.

On web.chris.example.com

Install Nginx

  1. Add the dev repo so that we can get to 1.7.x:
    sudo apt-add-repository ppa:nginx/development
    sudo apt-get update
  2. Verify that we’ll get 1.7.x (check the version number returned, currently 1.7.5 as of this posting. If you see 1.4.6 you probably didn’t run apt-get update above)
    sudo apt-get -s install nginx
    
  3. Install Nginx
    sudo apt-get install nginx
    
  4. Verify that it was installed
    nginx -v

Install and configure PHP-FPM

  1. Install PHP-FPM
    sudo apt-get install php5-fpm
  2. Tweak PHP-FMP for security:
    sudo vi /etc/php5/fpm/php.ini
  3. Look for ;cgi.fix_pathinfo=1 and change it to cgi.fix_pathinfo=0
  4. Restart PHP-FPM
    sudo service php5-fpm restart
  5. Create a directory for our web files:
    sudo mkdir /var/www

Configure Nginx

  1. Edit the default site for Nginx (or whatever site you want to work on)
    sudo vi /etc/nginx/sites-available/default
  2. Erase and change to
    server {
        listen 80 default_server;
    
        root /var/www;
        index index.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    
    }
  3. Test Nginx:
    sudo nginx -t
  4. Restart Nginx
    sudo service nginx restart
  5. Create a quick test file
    echo "<?php phpinfo();" | sudo tee /var/www/index.php
    
  6. Use a web browser to confirm that PHP is running
  7. Remove the test file
    sudo rm /var/www/index.php

Install and configure mysql

  1. Install
    sudo apt-get install mysql-server php5-mysql
  2. Configure
    sudo mysql_install_db
    sudo mysql_secure_installation
    

Install exim for email (optional)

  1. sudo apt-get install exim4
    sudo dpkg-reconfigure exim4-config

Install WP-CLI

    1. Install PHP CLI
      sudo apt-get install php5-cli
    2. Download
      cd ~
      curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  • Test
    php wp-cli.phar --info
  • Make it executable
    chmod +x wp-cli.phar
  • Make it available everywhere as just the command wp
    sudo mv wp-cli.phar /usr/local/bin/wp
    Confirm it works
    wp --info

Install WordPress via WP-CLI (optional, you can do it any way you want)

  1. Download WordPress
    cd /var/www
    wp core download
  2. Create a MySql database and user, replace the testXYZ parts as needed
    mysql -uroot -p -e “CREATE DATABASE testdb; GRANT ALL PRIVILEGES ON testdb.* TO testuser@localhost IDENTIFIED BY ‘testpassword’; FLUSH PRIVILEGES;”
  3. Create a config file (replace variable below obviously)
    wp core config --dbname=testdb --dbuser=testuser --dbpass=testpassword

Generate some sample content

  1.  wp post generate –count=10000

 

Notes

  1. Nginx runs as user and group www-data by default. For most of my installs I usually add my non-root account to the www-data group and also make that my default group
    sudo usermod -a -G www-data myaccounthere
    sudo usermod -g www-data myaccounthere