Migrate WordPress Blog From Apache2 to Nginx on Ubuntu 17.04 / 17.10

WordPress can run on either Apache2 or Nginx webservers. You probably won’t notice any performance difference when you switch from one to the other. My WordPress blog is powered by Nginx because it’s easier to manage and I’ve spent lots of hours learning and understanding Nginx.

Other webmasters may prefer Apache2 to Nginx. Whether you run Apache2 or Nginx, WordPress will still run happily. For those who are running WordPress websites with Apache2 and want to experience how WordPress might work with Nginx, this post shows you how to migrate your existing WordPress blog from Apache2 to Nginx server.

Switching from one webserver to another shouldn’t be difficult as long as you follow the steps below… new users and students should find the steps below very easy to read and understand.

This post assumes you’re running WordPress website(s) with Apache2. If you don’t already have a functioning WordPress website running on Apache2, then the steps below may not apply to you.

To switch from Apache2 to Nginx, follow the steps below:

Step 1: Install Nginx

Since Apache2 is already installed and functioning, run the commands below to install Nginx webserver. After running the commands below, Nginx service will attempt to startup but will fail, ignore the error.

sudo apt install nginx

Step 2: Install PHP-FPM and Related Modules

Apache2 uses PHP while Nginx requires PHP-FPM… Run the commands below to install PHP-FPM and related modules to support PHP-based apps, including WordPress.

sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-mcrypt php-ldap php-zip php-curl

After installing PHP-FPM, run the commands below to open Nginx PHP-FPM default configuration file.

sudo nano /etc/php/7.1/cli/php.ini      # Ubuntu 17.10
sudo nano /etc/php/7.0/cli/php.ini      # Ubuntu 17.04

Then make the changes on the following lines below in the file and save. You can increase the numbers below to suite your environment.

file_uploads = On
max_execution_time = 180
memory_limit = 256M
upload_max_file_size = 64M

Step 3: Configure Nginx Site

Next, create the Nginx WordPress site configuration file. This file controls how the site functions. Run the commands below to create a new configuration file called wordpress

sudo nano /etc/nginx/sites-available/wordpress

Then copy and paste the content below into the file and save it. Make sure the root location is the same as the one defined in the Apache2 configuration. The root location is where the WordPress content lives.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  mywordpress.com www.mywordpress.com;

    location / {
    try_files $uri $uri/ /index.php?$args;        

    location ~ \.php$ {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock; #Ubuntu 17.10
 #  fastcgi_pass             unix:/var/run/php/php7.0-fpm.sock; #Ubuntu 17.04
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;


Save the file and exit.

Step 4: Enable the WordPress Site

After configuring the VirtualHost above, enable it by running the commands below

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

Step 5: Make Nginx use port 80

Since Apache2 is the current default webserver, it’s automatically assigned to listen on port 80. Because we want Nginx to be the default server, we’ll want to change Apache2 to listen on a different port… (8080)… You can’t have two services listening on the same port on the same server.

To change Apache2 ports, run the commands below to open the port.conf file.

sudo nano /etc/apache2/ports.conf

Then edit the file to look like the one below:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

 <IfModule ssl_module>
        Listen 443

 <IfModule mod_gnutls.c>
        Listen 443

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Then look Apache2 site config directory and open each of the site configuration, then change port  80 to 8080

sudo nano /etc/apache2/sites-available/000-default.conf

Change the line in each file in that directory to look like this:

<VirtualHost *:8080>

Save the files.

Now everything is in place all you have to do is shutdown Apache2 and start Nginx.. and Nginx will be assigned to port 80 and begin serving WordPress content. Make sure the following is true:

The root directory should be the same for both Apache2 and Nginx.


Make sure the root directory has the following permisions

sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress

Finally,  reload and stop Apache2 then start Nginx

sudo systemctl reload apache2.service && systemctl stop apache2.service
sudo systemctl start nginx.service

Check your site to make sure everything is up and working.

Uninstall Apache2

When you know everything is running, run the commands below to see all Apache2 packages installed…

dpkg --get-selections | grep apache

Then run the apt remove commands to uninstall all the listed packages.

sudo apt remove package1 package2 package3

That’s it!

You may also like the post below:

Install WordPress 4.9 on Ubuntu 17.04 / 7.10 with Nginx, MariaDB and PHP