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. For those managing their own servers or having 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 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

You can have many web servers 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 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 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. Remember that only one web server can be assigned to a single default port at a time? 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. 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 After installing PHP-FPM, the following steps may be necessary. Open the PHP-FPM default configuration file at the location below Then find cgi.fix_pathinfo parameter under Paths and Directories and change the line to: Next, open the file below from this location: Then make sure the line reads After that, save the file and closeout.

Configuring Nginx Website Settings

Next, open the Nginx default configuration file from the location below: Then make sure the settings look like the content below: 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 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 the Apache package. You don’t have to, but you can. 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!