In this tutorial you will learn how to install and setup Nginx on Ubuntu 18.04
What is Nginx?
Nginx (pronounced "engine X", /ˌɛndʒɪnˈɛks/ EN-jin-EKS), stylized as NGINX, nginx or NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004.
Step 1 - Update your Server
Before we start installing nginx, updating your system is required, so to update your system simply run the command below
sudo apt update
Step - 2 Install Nginx
Once you have updated your server, install nginx via this command
sudo apt install nginx
Step 3 - Configure the firewall (ufw)
To make sure nginx is running correctly and connections to port 80/443 are allowed you must configure them via ufw command.
To get a list of nginx profiles type the following command below
sudo ufw app list
and it should show you an output as the screenshot below

Each profile is different so please choose the right profile you need for your server
1) Nginx Full - This profile allows you to open both ports 80 and 443, port 80 means unencrypted connections and 443 means encrypted/ssl connections.
2) Nginx HTTP - This profile allows you to open only port 80
3) Nginx HTTPS - This profile allows you to open only port 443 (Secure/encrypted connections)
To allow Nginx Full
sudo ufw allow 'Nginx Full'
To allow Nginx HTTP
sudo ufw allow 'Nginx HTTP'
To allow Nginx HTTPS
sudo ufw allow 'Nginx HTTPS'
Step 4 - Test Nginx
Once nginx has been installed on your system it will automatically start, however to check whether nginx process has been started or not you can do so with this command
systemctl status nginx
If nginx is running, it should show you the Active state in green as shown in the screenshot below

Then simply go to your server's IP address and you should see the default index of nginx on your browser
The index.html file is located at /var/www/html
Step 5 - Managing Nginx after installation
Now that nginx is running, in this step we are going to show you some useful commands that you can use to manage nginx
Start Nginx
This command will start nginx
sudo systemctl start nginx
Stop Nginx
This command will stop nginx
sudo systemctl stop nginx
Restart Nginx
This command will stop and start nginx again
sudo systemctl restart nginx
Reload Nginx
In cases where you are doing only configuration changes you can use the command reload, this command allows you reload nginx without having to stop and start nginx process again.
for example, if you run a website under nginx, your visitors won't get disconnected during the reload
Disable Nginx from starting when server boots
If you want nginx process to not start automatically once server boots type
sudo systemctl disable nginx
Enable Nginx when server boots
If you want Nginx process to start automatically once server boots type
sudo systemctl enable nginx
Step 6 - Create Server Blocks
By default nginx has only one server block enabled and the index.html file is located at /var/www/html, so it will serve any content that's inside the "html" folder. One server block means only one website, so if you want to host more than one website you'll need to create another server block, to do this is very easy just follow the steps below
Note: mysite.com is your domain url, so make sure to replace it with the correct url when setting up server blocks.
Create the folder where your website content will be
sudo mkdir -p /var/www/mysite.com/html
We need to assign ownership
sudo chown -R $USER:$USER /var/www/mysite.com/html
Give the right permission
sudo chmod -R 755 /var/www/mysite.com
Create an index file
To make sure that nginx is serving the right content you can create an index file on /var/www/mysite.com/html
nano /var/www/example.com/html/index.html
and then simply paste
<html>
<head>
<title>Welcome to mysite.com!</title>
</head>
<body>
<h1>Success! The mysite.com server block is working!</h1>
</body>
</html>
Create the configuration file on /etc/nginx/sites-available
type the following command
sudo nano /etc/nginx/sites-available/mysite.com
now simply copy and paste the following server block configuration
server {
listen 80;
listen [::]:80;
root /var/www/mysite.com/html;
index index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
location / {
try_files $uri $uri/ =404;
}
}
and press CTRL + X to save it
Step 7 - Enable Server Block
Once you have created the server block located at /etc/nginx/sites-available/mysite.com, you need to enable it by typing this command
sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/
The command above is to make sure the server block is enabled when server boots
Step 8 - Reload and Restart Nginx
To make sure that server block/s and nginx has been configured correctly type the command
sudo nginx -t
Then simply restart nginx
sudo systemctl restart nginx
Conclusion
By now you should know how to Install and manage Nginx on Ubuntu/Debian operating systems.