In this tutorial, we will guide you through the process of replacing Apache with Nginx on an Ubuntu 20.04 server.
Apache and Nginx are both popular web servers, but they have different strengths and weaknesses. Apache is known for its modularity and flexibility, while Nginx is known for its speed and efficiency. Many users have switched from Apache to Nginx for better performance and resource utilization.
NGINX, founded by computer software engineer Igor Sysoev in 2004, is a modern web server that is widely used by the busiest and highest traffic websites. It seamlessly integrates with major web stacks, including the LEMP (Linux, NGINX, MySQL, PHP) stack.
If you are currently hosting your website with the Apache web server and would like to migrate to NGINX without losing any data and with minimal downtime, this tutorial is for you.
/etc/apache2/apache2.conf
for Apache and /etc/nginx/nginx.conf
for NGINX./var/www/html
.To replace Apache with Nginx, we will need to install Nginx, configure it to serve web pages, and then disable and uninstall Apache.
To avoid any conflicts with NGINX, it is necessary to remove the Apache web server before installing NGINX.
Firstly, stop the Apache service to ensure the smooth removal of Apache.
sudo systemctl stop apache2
Next, disable the Apache startup entries from systemctl to prevent Apache services from starting automatically during boot time.
sudo systemctl disable apache2
Once the Apache services are successfully stopped and the startup entries are removed, it is time to remove the Apache web server packages from the system.
sudo apt-get remove --purge apache2 apache2-utils sudo rm -rf /etc/apache2
Note that only the apache2 packages are removed, and Apache-related dependencies remain on the system. It is essential to remove these unwanted dependencies to free up space.
sudo apt autoremove
After executing the above command, the Apache web server is successfully removed. The next step is to install NGINX.
To install NGINX on Ubuntu, you can use the default repository that contains all the necessary packages. The installation process is straightforward and can be completed using the apt package manager.
First, remove and flush the old apt repository cache, then update the repository to load the latest package information, and perform a full upgrade to upgrade all the installed packages. You can use the following command to achieve this:
sudo apt clean all && sudo apt update && sudo apt dist-upgrade
Once the repository has been updated, you can proceed with the installation of NGINX packages using the following command:
sudo apt install nginx
Once NGINX is successfully installed, you can proceed to the next step, which will guide you through the firewall configuration for the NGINX web server.
In order for NGINX to function properly, it requires access to HTTP port 80 and HTTPS port 443 through the firewall. Ubuntu 18.04 uses the UFW (Uncomplicated Firewall) as the default firewall, and therefore we need to configure UFW to allow traffic to these ports.
By default, UFW has no rules in place, so we can easily add the required rules using simple commands. To allow both HTTP and HTTPS traffic to NGINX, we will use the "Nginx Full" ruleset.
To add the rules to UFW, run the following command:
sudo ufw allow "Nginx Full"
After adding the firewall rules, we can check the status of the rules using the following command:
sudo ufw disable sudo ufw enable sudo ufw status
The output should show that the "Nginx Full" rule has been added, along with any other rules that may be in place. With this step completed, we can move on to the next step and configure the NGINX web server.
While the configuration of NGINX and Apache web servers share similarities, their syntax and structure differ in their respective configuration files. A comparison of their configuration files is shown below:
Apache Configuration File Example /etc/apache2/apache2.conf
:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ServerAdmin admin@example.com DocumentRoot /var/www/html/ </VirtualHost> <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
NGINX Configuration File Example /etc/nginx/nginx.conf
:
http { server { listen 80; server_name example.com; root /var/www/html; index index.html; location / { try_files $uri $uri/ =404; } } }
As shown in the above examples, Apache uses a hierarchical structure with <Directory> tags, while NGINX uses a flat structure with directives within a block. Additionally, Apache uses .htaccess files for per-directory configuration while NGINX does not.
Understanding these differences will help when migrating from Apache to NGINX or vice versa, as well as in configuring and troubleshooting each server.
Assuming that the document root is /var/www/html
and the default domain name is example.com
.
First, you need to edit the NGINX server blocks files located in /etc/nginx/sites-available/
and create a new server block file for your domain. You can do this by running the following command:
sudo nano /etc/nginx/sites-available/example.com.conf
Then, add the following lines to the file and replace "example.com" and "www.example.com" with your domain name and subdomain respectively:
server { listen 80; server_name example.com www.example.com; root /var/www/html; location / { try_files $uri $uri/ =404; } }
Once you have added these lines, save the file by pressing Ctrl + O and exit the editor by pressing Ctrl + X.
Next, you need to create a symbolic link between the sites-available and sites-enabled directories. This can be done by running the following command:
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
After that, you can check the syntax of the configuration file to avoid any errors by running the following command:
sudo nginx -t
If everything is correct, you will see "Syntax OK" as the output. If there are any errors, you should re-check the NGINX server blocks files.
Finally, you need to restart the NGINX service to apply the changes by running the following command:
sudo systemctl restart nginx
To enable the NGINX user, www-data, to read, write, and execute the default web root directory, you can run the following command:
sudo chown www-data:www-data /var/www/html
To verify that the www-data user and group are the owners of the default web root directory, you can run the following command:
ll /var/www/html
The output should show that the www-data user and group are the owners of the default web root directory.
In conclusion, you have completed the process of replacing Apache with NGINX, and now you can use NGINX for your web property to enable fast access to web assets and reduce memory usage. NGINX can be used for various purposes, including serving static and dynamic websites. For further information on NGINX, you can refer to the man pages available in Ubuntu.