How to install Nginx on Ubuntu 18.04

Last updated on June 27, 2020

Testing the Installation

To test the Nginx installation open http://YOUR_IP in your browser, and you will get the Nginx default landing page like below:

Managing the Nginx Process

You can manage your Nginx web service same like other systemd services by using the below commands:

To stop the Nginx service:

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 first publicly released in 2004. It pronounced "engine x" is a free, open-source, high-performance HTTP and reverse proxy server. Compare to Apache, Nginx handle a large number of concurrent connections. It is more resource-friendly than Apache.

Prerequisites

Before installing Nginx, you should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges, and you don’t have Apache or any other process running on port 80 or 443.

Installing Nginx

By default, Nginx is available in Ubuntu’s default repositories, so to install it run the following commands:

Once the installation is completed, the Nginx service will start automatically. Nginx configuration files are located in the /etc/nginx directory. You can verify it by running:

sudo systemctl status nginx

The output will look something like this:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-04-29 06:43:26 UTC; 8s ago
     Docs: man:nginx(8)
  Process: 3091 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 3080 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 3095 (nginx)
    Tasks: 2 (limit: 507)
   CGroup: /system.slice/nginx.service
           ├─3095 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─3097 nginx: worker process

Updating the Firewall

Before going to use Nginx need to update the firewall settings and open HTTP (80) and HTTPS (443) ports. By using the below command to open those ports

sudo ufw allow 'Nginx Full'

To verify Nginx status by using the below command:

sudo ufw status

The output will get something like the following:

Status: active
To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)
sudo apt updatesudo apt install nginx
sudo systemctl stop nginx

To start the Nginx service:

sudo systemctl start nginx

To restart the Nginx service:

sudo systemctl restart nginx

To reload the Nginx service after made some changes in the configuration:

sudo systemctl reload nginx

By default, the Nginx service will start automatically when boot. If you want to disable this using below command:

sudo systemctl disable nginx

Re-enable automatically start Nginx service:

sudo systemctl enable nginx

Conclusion

Congratulations, you have successfully installed Nginx on Ubuntu 18.04 server. You can use the server blocks to host multiple domains on your server.