LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How to Replace Apache with NGINX on Ubuntu 18.04

NGINX is the modern web server founded by computer software engineer Igor Sysoev in the year 2004. NGINX is used by the most busiest and high traffic website. NGINX works out of box with the most major web stacks including LEMP (Linux, NGINX, MySQL, PHP) stack. This tutorial assumes that your website is hosted with the Apache web server and you want to migrate to NGINX . The process of migration includes replacing the Apache web server with the NGINX without loosing the website data with less downtime.

Why Replace Apache with NGINX ?

There are several reasons to replace Apache with NGINX are:

  • NGINX is the fastest web server that supports concurrent connections and supports high traffic website load.
  • NGINX consumes less RAM and CPU compared to Apache and it is resource friendly.
  • NGINX improves performance of website by supporting inbuilt cache system for faster access for website static contents like Images, CSS, JavaScript, etc.

What is the major difference between Apache and NGINX?

These are the major difference between Apache and NGINX are:

  • The main configuration files for Apache and NGINX are located at /etc/apache2/apache2.conf and /etc/nginx/nginx.conf respectively.
  • NGINX uses server block but Apache uses virtual host.
  • NGINX and Apache both are using same default root directory /var/www/html.
  • NGINX has inbuilt cache system but Apache don’t have any inbuilt cache system.

Pre Requirements

Before starting the tutorial you will need:

  • You will need a Ubuntu 18.04 VPS with minimum 1GB of RAM for smooth operations.
  • The Apache web server must be previously installed on your VPS.

Step-1: Remove the Apache Web Server

Before installing the NGINX you will remove the Apache web server to avoids conflict between them.

First of all you will stop the Apache service before removing the Apache web server. This enables us to remove the Apache without any issues.

$ sudo systemctl stop apache2

After stopping the Apache you will remove the startup Apache entries from systemctl. This enables us to remove the startup entries so that Apache services won’t be automatically started during boot time.

$ sudo systemctl disable apache2

When Apache services are successfully stopped and startup entries are also remove then it time to remove the Apache web server packages from the system.

$ sudo apt remove apache2

Above command will remove only apache2 packages on but Apache related dependencies are kept on with system. So it is essential to remove those unwanted dependencies to free your space. This can be run by given command.

$ sudo apt autoremove

Now, the Apache web server has successfully removed. The installation of NGINX is described in the next step.

Step-2: Install the NGINX Web Server

Let’s begin with the installation of NGINX on Ubuntu. The Ubuntu default repository contains all the packages of NGINX. Installation is straight forward so you have to install it without any hassle using apt package manager.

First remove and flush the old apt repository cache then update the repository to load latest packages information and perform a full upgrade to upgrade all the installed packages.

$ sudo apt clean all && sudo apt update && sudo apt dist-upgrade

After updating the repository it is the right time to install the updated NGINX packages.

$ sudo apt install nginx

When NGINX has successfully installed then Let’s begin with the next step that will guide you firewall configuration for NGINX web server.

Step-3: Configure UFW Firewall

The NGINX web server requires HTTP Port that is Port No. 80 and HTTPS port that is Port No. 443 to successfully work with firewall. So it is essential to keep this port open for that purpose so that NGINX works flawlessly. The UFW (Unified Firewall) is the default firewall for Ubuntu 18.04 Linux distribution. Hence, you will add firewall rules to allow HTTP and HTTPS ports.

By Default there is no rules are added to UFW firewall so it is so easy to add those rules. You are required to add the HTTP and HTTPS port rules to UFW firewall this can be done by simple commands. The NGINX Full rules contains both the HTTP and HTTP ports and this will allow these ports to be kept open by the UFW firewall.

$ sudo ufw allow "Nginx Full"

After adding the firewall rules its time to check the rules which had been added or updated using these rules using status command.

$ sudo ufw status

The above command show given sample output.

Status: active

To Action From
-- ------ ----
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
22/tcp LIMIT Anywhere
Nginx Full ALLOW Anywhere
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
22/tcp (v6) LIMIT Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

Above output shows that you have successfully added the firewall rules and you ready to move forward to configure the NGINX web server that is described in next step.

Step-4: Understanding the Configuration File of NGINX Web Server Compared to Apache Web Server

The configuration of NGINX are almost same like Apache web server but the structure and syntax is different referred to configuration files. This Difference between the can be understand by the given sample configuration file of Apache and NGINX

Sample Apache Configuration file is located at /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/
</VirtualHost>

<Directory /var/www/html>
Require all granted
AllowOverride None
</Directory>

Sample NGINX Configuration file is located at /etc/nginx/sites-available/example.com.conf

server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;

location / {
try_files $uri $uri/ =404;
}
}

If you look carefully from the both the configuration files you will find that Apache configuration file are expressed in virtual host and NGINX configuration file are expressed in server block. After understanding the difference between the configuration file of Apache and NGINX. Now, you ready to configure the rest of the NGINX configuration files that is described in next step.

Step-5: Configure NGINX Web Server

The NGINX has same capabilities like Apache web server but it has faster support of concurrent connections. The configuration file of NGINX uses the server blocks in configuration. You have to configure it wheres the same location of document root where all your static web assets like HTML, CSS, JavaScript and Images are stored.

Note: In this guide we throughout assume that your document root is /var/www/html and default domain name is example.com

In Ubuntu, the NGINX Server Blocks are located at sites-available and sites-enabled directory inside the NGINX configuration directory. You will edit the server blocks files located in /etc/nginx/sites-available/ and you will be create one for enabling the server blocks for your domain. This method is highly recommended because it allows you to host more than one website and at different domains and files locations on your Ubuntu.

$ sudo nano /etc/nginx/sites-available/example.com.conf

Add the given lines and don’t forget to replace example.com and www.example.com with your base domain name and subdomain to enable server blocks for NGINX.

server { 
listen 80; 
server_name example.com www.example.com; 
root /var/www/html; 

location / { 
try_files $uri $uri/ =404; 
} 
}

When you will completely add all of these lines then hit Ctrl + O to save and Ctrl + X to exit from nano text editor.

In NGINX server blocks configuration files you will create the symbolic link using soft links between sites-available and sites-enabled directory. Soft links allows you whenever you will make changes to server blocks configuration file located in sites-available directory and it will immediately replicated to sites-enabled directory.

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

You will check for correct syntax and to avoid any error present in NGINX configuration files. This command will also tells you where you have errors are present.

$ sudo nginx -t

When all syntax is correct then it will shows you Syntax OK as a output. If any thing goes wrong please re-check NGINX server blocks files. When all steps are completed then restart the services for making changes in the effect.

$ sudo systemctl restart nginx

After all things are ready then you will change the permission for default NGINX user www-data to enable read, write and execute permissions for default web root directory.

$ sudo chown www-data:www-data /var/www/html

To verify whether the www-data user and groups are owned the default web root directory by running the given long listing command

$ ll /var/www/html

After running this command the output shows www-data user and group is owned by the default web root directory /var/www/html . This means default NGINX user www-data will able to read, write and execute the default web root directory.

Conclusion

Lastly, you have successfully replaced the Apache to NGINX. Now you will ready to use the NGINX for your web property to enable fast access to web assets and low memory foot-printing. In the end, the NGINX can be used for various proposes and it can be used for both static and dynamic websites. For more information regarding the NGINX refer the man pages available in Ubuntu.

Jon Biloh

2 Comments

  1. Agree, using Nginx consumes less RAM and CPU compared to Apache.

    June 21, 2019 @ 6:13 pm | Reply
  2. Joost:

    Thx for your tutorial! Any follow-up on enabling SSL?

    November 18, 2021 @ 12:30 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *