This brief tutorial shows students and new users how to easily migrate or switch from Apache webserver to Nginx running WordPress. Most websites today are powered by Apache because it’s the default choice on most Linux distributions.
Apache is the most popular web server in use today. It is almost always chosen to run websites offered by hosting providers managed by cPanel or other management tools. This tutorial should be helpful for those working their servers or having the means to install alternative web servers.
Nginx is another webserver that has been gaining popularity over time. It’s easy to manage, lightweight, and fast. There’s no need to switch for most web admins, but others may find Nginx more useful in certain situations than Apache.
This guide will help those who want to switch to Nginx from Apache 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 consideration when switching. But for standard WordPress websites and applications, the basic settings should work great.
Install Nginx
Many web servers can be installed on a Linux system, but only one can run simultaneously on any of the standard ports chosen. For example, if you’re running a web server on port 80 / 443, you can only have one web server started and used. If you attempt to run a second web server, the default port will be locked, and the second web server will fail to start.
To install Nginx, run the commands below.
sudo apt-get update sudo apt-get install nginx
After installing the Nginx web server, the commands below can be used to stop, start and enable the 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 web server can simultaneously be assigned to a single default port? So, if 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 use by Apache2)
Install Nginx PHP-FPM
Unlike Apache2, Nginx passes PHP handling to a dedicated PHP handler like PHP-FPM. So if you’re 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 may be necessary.
Open the PHP-FPM default configuration file at the location below
sudo nano /etc/php/7.0/fpm/php.ini
Then find cgi.fix_pathinfo parameter under Paths and Directories and change the line to:
cgi.fix_pathinfo=0
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 closeout.
Configuring Nginx Website Settings
Next, open the 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; } }
Finally, save the configuration file and continue to switch.
Make the Switch
After configuring the Nginx settings above, run the commands below to stop the Apache webserver and start Nginx.
sudo systemctl stop apache2.service sudo systemctl start nginx.service
Now go and test your server hostname or IP address; hopefully, your website will 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 the Apache 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
Summary:
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 an extended website outage.
Enjoy!
Thanks . It really helped me out.