How to Replace Apache with NGINX on Ubuntu

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"
Code language: JavaScript (javascript)

Then 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/

<VirtualHost *:80> ServerName ServerAlias ServerAdmin DocumentRoot /var/www/html/ </VirtualHost> <Directory /var/www/html> Require all granted AllowOverride None </Directory>
Code language: CSS (css)

Sample NGINX Configuration file is located at /etc/nginx/sites-available/

server { listen 80; server_name; root /var/www/html; location / { try_files $uri $uri/ =404; } }
Code language: PHP (php)

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.

Nginx config file with using PHP

server { # Using port 8080 only for testing purposes listen 8080 default_server; listen [::]:8080 default_server; # Where are the files that are being served root /srv/www/html; # Default Index file being served, have added `index.php` index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { # Added support for PHP routers try_files $uri $uri/ /index.php?args =404; } # Adding support for PHP7.3 location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Adding environmental variables for this website # include /srv/config/default.nginx.conf; } }
Code language: PHP (php)

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

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 vim /etc/nginx/sites-available/

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

server { listen 80; server_name; root /var/www/html; location / { try_files $uri $uri/ =404; } }
Code language: PHP (php)
server { listen 80; root /var/www/; index index.php index.html; server_name; access_log /var/log/nginx/SUBDOMAIN.access.log; error_log /var/log/nginx/SUBDOMAIN.error.log; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; } location ~ /\.ht { deny all; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } }
Code language: PHP (php)

When you will completely add all of these lines then exit from 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/ /etc/nginx/sites-enabled/

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
Code language: JavaScript (javascript)

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
Code language: JavaScript (javascript)

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.

How to fix 404 not found Nginx problem

Change the line in the location block to:

try_files $uri $uri/ /index.php?q=$uri&$args;
Code language: PHP (php)

and reload nginx

sudo systemctl reload nginx
No Comments

Send Comment Edit Comment

 ̄﹃ ̄
∠( ᐛ 」∠)_
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
( ๑´•ω•) "(ㆆᴗㆆ)