Migrating a WordPress Blog from Apache2 to Nginx

This blog is hosted on a Linode VPS with 512 MB RAM and running Ubuntu 11.10. Up until today, Apache2 was the web server, and it worked fine except sometimes, it reached connection and memory limits, and the blog would go offline for a short period of time, especially right after a new blog post. So this week-end, I decided to switch to nginx (pronounced engine-x) web server which is said to use less memory than Apache2. Let me know if something suddenly stopped working…

nginx is a relatively recent web server, and the documentation on the web seems to become outdated pretty fast, so I’ve decided to document what I’ve done.

The first thing I would recommend is to try it in your own local server first,  and make sure most things are working including plugins.

Testing your WordPress blog with nginx in a local server

I run Ubuntu 12.04 in VirtualBox, and since we are going to run a server, I’ve set the network connection in VirtualBox to “Bridged Adapter” and not “NAT” in order to make things easier.

The very first thing you want to do is to backup your WordPress site either manually, or thru tools such as BackWPup.

Install ngnix, php and mysql in the server:


Create a nginx configuration file for your wordpress blog in /etc/nginx/sites-available/wordpress:


Source: Nginx Wiki and wpmu.

Note that I used port 8080 instead of 80, because I eventually intended to test this in the VPS at the same time as the main apache2 server was still running, but finally decided against it… Using port 80 would be slightly simpler.

Enable your wordpress blog:


As mentioned in the note in the config file above, edit /etc/php5/fpm/php.ini to enable the line:


Now import your blog using the backup file in /srv/www/wordpress/public_html, e.g.:


Open /srv/www/wordpress/public_html/wp-config.php to retrieve WordPress dabatase name (DB_NAME), user (DB_USER) and password (DB_PASSWORD).

Now add the wordpress user to mysql:


Create the empty wordpress database:


Import the database previously backed up:


If you want to access the blog from another device on the LAN, you need to make sure port 8080 is open:


Finally start nging and php5-fpm:


You should now be able to access your blog with http://localhost:8080 or http://server_ip:8080.

Check everything is working as expected. If it does, you’re now ready to update your server.

Migrating from nginx to apache2 in the VPS

There are fewer steps for this part as most of required components are already installed and configured, if you are already running a WordPress blog with Apache2.

First install nginx and php5-fpm:


Create a nginx configuration file for your wordpress blog in /etc/nginx/sites-available/wordpress. Assuming you used the same paths in your local server and your VPS, It’s the same file as in the section above, expect you need to change two lines:


Enable your wordpress blog and delete the default server config:


Edit /etc/php5/fpm/php.ini with the line:


You’ll also need to edit /etc/php5/fpm/pool.d/www.conf and change the line:


by


That’s all. Now it’s time to turn off Apache2 and start nginx:


Go to your blog (e.g. http://www.domain.com) and it should work (mostly) fine.

I had to change a couple of things.

  • W3 Total Cache plugin. I use this plugin for better performance, but  it uses some specific web server configuration files. Updating those files are easy. In the WordPress Dashboard, go to Performance->General Settings, and you should see the following warnings:
    Click on auto-install to update nginx.conf. Minify fails for me, and I haven’t found a solution yet, but page caching appears to be working fine using “Disk: basic” caching method.
  • Publish/Save draft failure with iNove Theme
    I’ve come across another issue each time I clicked on “Save Draft“:

    Warning: Cannot modify header information – headers already sent by (output started at /srv/www/wordpress/public_html/wp-content/themes/inove/functions.php:1181) in /srv/www/wordpress/public_html/wp-includes/pluggable.php on line 866

    functions.php was saved in DOS mode, and converting it to Unix mode fixed the issue:

  • Cloudflare and nginx
    Cloudflareis really a great “CDN” service which is free (as long as you don’t use https), saves a lot a bandwidth and improves the site performance. The problem is that it will screw up your stats (all visitors will appears to come from the same set of IPs), if you don’t make some modifications. For Apache, there is a module cloudflare_mod. For nginx, you need to edit /etc/nginx/nginx.conf and add the following lines somewhere in the http section:

Once you’re happy with the changes, make sure Apache won’t be start at next boot:


ngnix and php5-fpm have already been configured to start automatically at boot time during installation.

The blog appears to work fine, but I still have a few things to work out such as Awstats support and minify.

[Update: July 5, 2012. I’ve been running nginx for a little over 10 days, everything seems to be working fine, and I/O rate has decreased, and swapping is basically a thing of the past (The little spike during nginx usage was due to a virus scan)

IO Rate and swapping: Apache2 vs nginx

[Update 2: After I updated my server to Ubuntu 12.04, CPU usage and I/O rate went slightly up, so I decided to give a try at apc. It’s actually very easy to setup:

  1. Install apc: sudo apt-get install php-apc
  2. Go to your wordpress dashboard and setup W3 to use “Opcode: Alternative PHP Cache (APC)” for page and object caches.

And the results are pretty impressive with both CPU usage and Disk IO rates taking a plunge.

wordpress w3tc apc cache

Strangely I did not see any improvements in Google Analytics Page Speed charts, but Googlebot seems to be happy about it, as it now takes a bit over 400 ms to download a page on average instead of around 900 ms before APC was enabled.

APC Googlebot Download Time

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK Pi 4C Plus
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
1 Comment
oldest
newest
Khadas VIM4 SBC