DIY Mercury installation for super fast WordPress performance

I’ve just released a new service to set up, migrate and configure your WordPress site for you on Digital Ocean using the latest and greatest Mercury Install. BUT, maybe you want to set this up yourself? If so, read on for the full guide.

This includes:

  • Nginx – web server (far faster than Apache)
  • Memcached and APC – caching engine which works with W3 Total Cache
  • Varnish – HTTP accelerator
  • HHVM with fallback to PHP-FPM – compiled PHP for much faster pages
  • Percona DB – faster than MySQL (100% compatibility)

Installing just Nginx will make your site a helluva lot faster and able to support many many more simultaneous users, but add in all these other features and you’ll be the envy of all your WordPress friends.

If you want to see some charts and stuff, check out the High Performance WordPress Installation Service page.

DIY Mercury Installation Steps

You need to know SSH and some Linux commands in order to build this yourself. Here’s the steps:

  1. Get yourself a Digital Ocean droplet (or any Ubuntu 14.04+ VPS but Digital Ocean use Raid SSD so they’re a great choice) (this affiliate link gets you $10 off)
  2. If you’re migrating, point new.yourdomain.com at your Digital Ocean IP address. If this is for a new site, point yourdomain.com at your Digital Ocean IP address
  3. Follow the instructions using this Ansible Playlist to the letter
  4. Install cURL, mcrypt and set up your firewall –
    sudo apt-get install php5-curl
    sudo apt-get install php5-mcrypt
    sudo ufw allow ssh
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw allow 25/tcp
    sudo ufw enable
  5. Set up PHP7 for your faster fallback plan:
    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    sudo apt-get install -y tmux curl wget nginx php7.0-fpm php7.0-cli php7.0-curl php7.0-gd php7.0-intl 
    sudo apt-get install php7.0-mysql php-memcached php7.0-mbstring php7.0-zip php7.0-xml
    vi /etc/nginx/conf.d/upstream.conf
    (Change php socket to new php7 socket)
    
    upstream php {
     server unix:/var/run/php/php7.0-fpm.sock;
    }
    
    
  6. Set up fail2ban to protect your server from attack (edit the jail.local file once you’ve created it, find the nginx section and change enabled to true):
    sudo apt-get install fail2ban
    cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    
    vi /etc/fail2ban/filter.d/wordpress.conf (add following lines)
    [Definition]
    failregex = ^<HOST>.*POST.*(wp-login\.php|xmlrpc\.php).* (403|499)
    Then vi /etc/fail2ban/jail.conf
    Change nginx enabled=true (instead of false)
    Change bantime at top to 86400 (1 day instead of 10 minutes)
    Add the following after nginx section:
    [wordpress]
    enabled = true
    port = http,https
    filter = wordpress
    logpath = /var/log/nginx/*access.log
    maxretry = 3
    
    
  7. Set up sendmail (not needed if you use an external service like Mandrill or Mailgun – note: more work needed to set up SPF records and make email get past Junk folders – use external service if in doubt)
    sudo apt-get install sendmail
    sudo sendmailconfig (choose Y and Y for answers to the questions)
  8. Set up unattended-upgrades (add “kernel”; to the blacklist section because if you are on Digital Ocean, you should update the kernel through the console to ensure snapshots and other things continue to work):
    sudo apt-get install unattended-upgrades
    vi /etc/apt/apt.conf.d/50unattended-upgrades
  9. Run
    update-rc.d xxx disable (where xxx is the service you wish to switch off)

    for any services you do not wish to run. Check services with service –status-all. Note: If you have 1GB RAM or less on your server, switch off Varnish and HHVM and use PHP7 – you will need to change your /etc/nginx/sites-available/domain.com file – change three things: 1) Change port to 80 instead of 8080, 2) Change primary method from hhvm to php, 3) Remove the fallback line in the primary method

  10. Set up a proper cron job – locate your wp-config.php file (/var/www/html/yourdomain/wp-config.php) and add:
    define('DISABLE_WP_CRON', 'true');
    Then run crontab -e and add:
    */1 * * * * wget -q -O – http://YOURWEBSITE.COM/wp-cron.php?doing_wp_cron
  11. Change your timezone:
    sudo dpkg-reconfigure tzdata
  12. Optionally configure longer timeouts and debug – useful if you’re going to be running large imports and such stuff
    Edit wp-config.php:

    define ('WP_DEBUG', true);
    define('WP_DEBUG_DISPLAY', false);
    define('WP_DEBUG_LOG', true);
    Edit /etc/nginx/sites-available/domain.com:
    add fastcgi_read_timeout 120; to your location block
    Edit /etc/php/7.0/fpm/php.ini:
    Change max_execution_time to 120
    Change upload_max_filesize to something better than 2M
  13. Increase the number of child processes that get spawned:
    Edit /etc/php/7.0/fpm/pool.d/www.conf and change:

    pm.max_children 25
    pm.start_servers 5
    pm.max_spare_servers 5

    (you can play with above settings depending on the RAM on your droplet)

  14. Increase the number of connections available to nginx – edit /etc/nginx/nginx.conf – something like this at the beginning:
    events {
     worker_connections 20000;
     multi_accept on;
     use epoll;
    }
    worker_rlimit_nofile 42000;
  15. Configure MySQL – vi /etc/mysql/my.cnf [mysqld section] (depends on memory available, engine used, and what you’re doing with wordpress)
    key_buffer = 512M
    max_allowed_packet = 512M
    thread_stack = 192K
    thread_cache_size = 8
    query_cache_limit = 512M
    query_cache_size = 4192M (or 0 can improve performance, it depends)
    innodb_buffer_pool_size = 20000M (according to your memory - as a guide, use 60% of your memory here)
    innodb_buffer_pool_instances = 16 (according to how many cores you have)
    innodb_io_capacity = 5000 (because Digital Ocean use fast SSDs so we don't want InnoDB to be limited)

Restart your key services (nginx, varnish, hhvm, php7.0-fpm) or reboot your server with shutdown -r now. Your new server and wordpress installation is now ready, fast and secure.

Migrating your existing site

  1. Log onto your original server
  2. cd to your wp-content folder
  3. Create a backup of the mysql database using: mysqldump -u root -p dbname > sqldump.sql
    1. If you don’t have SSH access, create a template file and add the following code: <?php shell_exec(‘mysqldump -u root -p dbname > sqldump.sql’); ?> then create a page with that template and view the page – you should now have a backup file – this is useful to get round stupid hosts like Hostgator or Godaddy if they’ve disabled SSH and backups
  4. Log onto your new server
  5. cd to your wp-content folder
  6. Connect with FTP to your old server: sftp root@ipaddress
  7. Get everything using: get -r *
  8. Exit FTP
  9. Restore your database using:
    1. mysql -u root -p
    2. create database dbname;
    3. use dbname;
    4. source sqldump.sql; — replace sqldump.sql with the full path to your sqldump.sql file (e.g. /var/www/html/websitename/wp-content/sqldump.sql)
  10. Exit mysql and delete the sqldump.sql file
  11. You might need to copy across the contents of your wp-config.php file – have a look at the differences for both using FTP.
  12. Update /etc/nginx/sites-available/domain.com to point to the folder you downloaded from your old server
  13. Once you’re happy, switch your DNS settings to point your domain name at your new server

Set up W3 Total Cache

  1. Log onto your new website
  2. Install W3 Total Cache
  3. Set up page caching, minification, database caching all using Opcode: APC caching. There is a complete guide to setting up W3 Caching here BUT use Disk:Enhanced only for page caching – use OpCode:APC Caching for everything else. If you’re using Datafeedr (or for maximum compatibility with other plugins), don’t add object caching.
  4. Optionally set up Cloudflare or similar CDN – if you do, also install the Cloudflare plugin as it fixes some peculiarities with IP address origination
  5. Add the following to your wp-config.php file:

Configure email

Edit /etc/hosts – find the 127.0.0.1 line and add your fully qualified domain name (hostname.yourdomain.com):


127.0.0.1 hostname.yourdomain.com hostname

Then run the following:


apt-get install exim4-daemon-light mailutils

dpkg-reconfigure exim4-config

From the screens that follow, choose:

  • Internet site
  • Enter your fully qualified domain name
  • Add your hostname and localhost to the list of servers that can send (as well as the fully qualified domain name) separated by semi-colons
  • Leave ‘Domains to relay for’ and ‘Machines to relay for both blank
  • Say ‘No’ to ‘Keep number of DNS-queries minimal (Dial-on-Demand)’?
  • Leave other options at their defaults

Now your server can send mail which doesn’t bounce from WordPress.

That’s it – you now have a superfast WordPress installation – the fastest it POSSIBLY can CURRENTLY be for your server configuration.

The above runs on $5pcm Digital Ocean upwards, although $20pcm+ is recommended otherwise you’ll need to disable some services – e.g. only use one engine (HHVM or PHP7), disable varnish and disable memcached. You still get great performance and better than anything else you’ll find as this is tuned for WordPress.

If you don’t want to follow the steps above, don’t have time, or are confused, I’ve listed a service where you can pay us to set this up for you. Get that fast wordpress installation service here.

Adding a new website

The best way to add a new website is to copy the file in /etc/nginx/sites-available and configure the new file as per your new site.

Chat to me

Dave Hilditch

Founder at WP Intense
Dave has been programming since 6 years old and has been developing WordPress plugins, themes and websites since 2010. In the past he built the browse view technology for Skyscanner and now he helps clients with interesting website challenges.

He is always on at least one of his computers when he's awake, so get in touch and he'll get right back to you.
Chat to me

Latest posts by Dave Hilditch (see all)