Switch from Apache2 to Nginx to Run WordPress on Ubuntu

office 4694143 640

This brief tutorial shows students and new users how to easily migrate or switch from Apache2 webserver to Nginx running WordPress. Most websites today are powered by Apache2 because it’s the default choice on majority of the Linux distributions.

Apache2 is the most popular webserver in used today. It is the almost always chosen to run websites offered by hosting providers managed by cPanel or other management tools. For those managing their own servers or have the means to install alternative webservers, this tutorial should be helpful.

Nginx is another webserver that has been gaining popularity over time. It’s easy to manage, lightweight and fast. For most webmasters, there’s no need to switch, but others may find Nginx useful in certain situations than Apache2.

This guide will help those who want to switch to Nginx from Apache2 on Ubuntu to run WordPress.

This tutorial assumes that you’re already running a website powered by Nginx. Other custom configurations may have to be taken into considerations when switching. But for standard WordPress websites and applications, the basic settings should work great.

Step 1: Install Nginx

You can have many webserver installed on a Linux system, but only one can run at a time on any of the standard ports chosen. If you’re going to be running a webserver on port 80 / 443, you can only have one webserver started and in used. If you attempt to run a second webserver, the default port will be locked and the second webserver will fail to start.

To install Nginx, run the commands below

sudo apt-get update
sudo apt-get install nginx

After installing Nginx webserver, the commands below can be used to stop, start and enable Nginx service to always start up when the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Remember that only one webserver can be assigned to a single default port at at time? So, if you Apache2 is already started and running and you try to start Nginx, Nginx will fail because it won’t be able to bind to port 80 or 443 (already in used by Apache2)

Step 2: Install Nginx PHP-FPM

Unlike Apache2, Nginx passes PHP handling to a dedicated PHP handler like PHP-FPM. If you’re going to be running PHP-based websites like WordPress or other content management systems, you must install PHP-FPM or another PHP handler.

To do that, run the commands below

sudo apt-get install php-fpm

After installing PHP-FPM, the following steps maybe necessary.

Open PHP-FPM default configuration file at the location below

sudo nano /etc/php/7.0/fpm/php.ini

Then find cgi.fix_pathinfo paremeter under Paths and Directories and change the line to:


Next, open the file below from this location:

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

Then make sure the line reads

listen = /run/php/php7.0-fpm.sock

After that, save the file and close out.

Step 3: Configuring Nginx Website Settings

Next, open Nginx default configuration file from the location below:

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

Then make sure the settings look like the content below:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name example.com www.exmaple.com;

     location = /favicon.ico {
        log_not_found off;
        access_log off;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;

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

    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;

    location ~ /\. {
        deny all;


Save the configuration file and continue to switch.

Step 4: Make the Switch

After configuring Nginx settings above, run the commands below to stop Apache2 webserver and start Nginx

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

Now go and test your server hostname or IP address and hopefully your website should still be up. If not, go and switch Apache2 back on and stop Nginx. then go and validate your Nginx configuration settings again.

Continue validating until you’ve successfully configured Nginx, then stop Apache2 and start Nginx.

When Nginx is running as expected, run the commands below to remove Apahce2 package. You don’t have to, but you can.

sudo apt-get remove apache2 apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php


This post shows students and new users how to easily switch from Apache2 webserver to Nginx on Ubuntu 17.04 | 17.10 server. This post should come in handy to anyone wanting to switch without causing extended website outage.


You may also like the post below:

1 Comment

  1. Thanks . It really helped me out.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.