Migrate WordPress Blogs from Nginx to Apache2 on Ubuntu 17.04 | 17.10

WordPress can run on either Nginx or Apache2 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 Nginx and want to experience how WordPress might work with Apache2, this post shows you how to migrate your existing WordPress blog from Nginx to Apache2 web servers.
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 currently running WordPress website(s) with Nginx webserver. If you don’t already have a functioning WordPress website running on Nginx, then the steps below may not apply to you.
To switch from Nginx to Apache2, follow the steps below:
Step 1: Install Apache2
Since Nginx is already installed and functioning, run the commands below to install Apache2 webserver. After running the commands below, Apache2 service will attempt to startup but will fail, ignore the error.
sudo apt install apache2
Step 2: Install PHP and Related Modules
Apache2 uses PHP while Nginx requires PHP-FPM… Run the commands below to install PHP and related modules to support PHP-based apps, including WordPress.
sudo apt install php libapache2-mod-php 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, run the commands below to open Nginx PHP default configuration file.
sudo nano /etc/php/7.1/apache2/php.ini # Ubuntu 17.10 sudo nano /etc/php/7.0/apache2/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_filesize = 64M
Step 3: Configure Apache2 Site
Next, create the Apache2 WordPress site configuration file. This file controls how the site functions. Run the commands below to create a new configuration file called wordpress.conf
sudo nano /etc/apache2/sites-available/wordpress
Then copy and paste the content below into the file and save it. Make sure the DocumentRoot location is the same as the one defined in the Nginx configuration. The root location is where the WordPress content lives.
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/wordpress/
ServerName example.com
ServerAlias www.example.com
<Directory /var/www/html/wordpress/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save the file and exit.
Step 4: Enable the WordPress Site
After configuring the VirtualHost above, enable it by running the commands below
sudo a2ensite wordpress.conf sudo a2enmod rewrite
Step 5: Make Apache2 use port 80
Since Nginx is the current default webserver, it’s automatically assigned to listen on port 80. Because we want Apache2 to be the default server, we’ll want to change Nginx to listen on a different port… (8080)… You can’t have two services listening on the same port on the same server.
To change Nginx ports, change to the /etc/nginx/sites-available directory and edit all the site config files in there to listen on 8080
sudo nano /etc/nginx/sites-available/wordpress
Then edit the file to look like the one below:
server { listen 8080; listen [::]:8080; root /var/www/html/wordpress; index index.php index.html index.htm; server_name example.com www.example.com;
Save the files and exit.
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.
/var/www/html/wordpress
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 Nginx then start Apache2
sudo systemctl reload nginx.service && systemctl stop nginx.service sudo systemctl start apache2.service
Check your site to make sure everything is up and working.
Uninstall Nginx
When you know everything is running, run the commands below to uninstall Nginx
sudo apt remove nginx
That’s it!
You may also like the post below:
How the rewrite rules will work ? nginx config files htdcos etc? will wp automatically alter and manage them? also plugins which uses htdocs to work ?