Nginx is Powerful - Easy Steps to Migrate WordPress From Apache

March 4, 2015 | By
| Reply More

WordPress works well on a lot of web servers, most people have started the blog on Apache web server, because it's a very popular and established solution. Apache offers a lot of features, good performance, strong security and support for many programming language like PHP, Perl, Python, Tcl and so on. While Apache offers a well rounded and good experience, in the last few years nginx has became very popular in the WordPress running websites, because of its focus on high-performance. NGINX is following the current trend and already planned to include support for HTTP/2 by the end of 2015.

This leaves a lot of WordPress blog users with the task of migrating their blog from Apache to Nginx. The task is not very complex but has quite a few pitfalls and issues that can arise. For our article we will move a WordPress blog from Apache to Nginx on an Ubuntu server.

Installing Nginx

Our first task is to install the nginx server on our server, because of it's popularity the package is included and well maintained in the Ubuntu repository, to install it run the following commands:

sudo apt-get update
sudo apt-get install nginx

nginx install

Installing php5-fpm

The web server does not come with build-in support for running PHP, instead it can uses a specialized software called a PHP handler to run the PHP content, the most popular choice is php5-fpm, to install it run the following command:

sudo apt-get install php5-fpm

By default php-fpm uses a port for connections, since we will be using it locally it is advisable to use a socket instead, to do this use your favorite text editor to open the file /etc/php5/fpm/pool.d/www.conf and make sure the listen line looks like this:

listen = /var/run/php5-fpm.sock

If it points to an ip address and port change it so it looks like the above.

After you are done, restart the php5-fpm server

service php5-fpm restart

Configuring nginx

Now you must configure your site block in nginx so open the configuration file called /etc/nginx/sites-enabled/default to look like this:

server {
root /var/www/html;
index index.php;

location / {
try_files $uri $uri/ /index.php?q=$request_uri;

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
expires max; log_not_found off; access_log off;

This is how the default server block looks like without the comments and a few additions, let me explain each row:

  • root - will point to the location of your files, by default it will be /var/www/html, but it can be /home/username/public_html for example
  • index - is the file that the web server will automatically serve to your clients, in the case of WordPress and most php website it's index.php
  • server_name - the domain name syou with the website to be accessed by
  • location and rewrite - they are specific to wordpress and help with seo url's
  • second location - this points to your php5-fpm file so the web-server can handle php files
  • last location - will add a maximum expire time to static files, this helps you to leverage browser caching

nginx config

After the configuration is done the last step you need to take is to stop apache and run the new nginx server instead, you can do this by running the following commands:

service apache2 stop
service nginx start

Now you can go ahead and check your new website, if everything is good you can finally remove the apache2 project so you are sure it will not be started by mistake:

apt-get remove apache2

(Optional) W3 Total Cache support

If you are using the popular W3 Total Cache plugin to speed up your site, and you should, you need to add the following rules to your nginx configuration (as provided by the WordPress Codex):

set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
if ($query_string != "") {
set $cache_uri 'null cache';

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';

# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args ;

I hope you enjoyed our article and your migration runs smoothly, please leave a comment below if you have any problems or issues.


Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.