How to install Nginx on Ubuntu

Feb 11, 2022·3 min read

Introduction

NGINX is a free, open-source, high-performance HTTP server and reverses proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. This article will show you how to install Nginx on Ubuntu and use it as a  web server or reverse proxy.

Install Nginx

ssh user@hostname
  • The user is the username of your server. (e.g. root)
  • The hostname is your server's public IP address. (e.g. 123.45.67.890)

After you press enter, a prompt will pop up asking for your password. When typing your password, you will not see anything in your terminal.

ssh to a virtual private server

Before you start installing Nginx. You need to make sure that you have the latest updates installed.

sudo apt update
Update packages

Install Nginx from Ubuntu package manager.

sudo apt install nginx

To verify if Nginx is running. Run the following command:

sudo systemctl status nginx
Nginx status

Grant access to Nginx

You must enable Nginx through the firewall software to allow access.

Display available applications that ufw knows by running the following command:

sudo ufw app list

You will get the following output:

available applications that ufw knows

We only want to allow HTTP traffic on port 80. Run the following command:

sudo ufw allow 'Nginx HTTP'

Test Nginx web server

Navigate to your browser and Enter your server's public IP address or domain name.

You should see Nginx default page.

Nginx default web page

NGINX Configuration

Now that you have learned how to install and set up Nginx. It's time to configure Nginx as a reverse proxy to point to your API.

  1. Remove the default Nginx config
sudo unlink /etc/nginx/sites-enabled/default

Restart Nginx by running the following command

sudo systemctl restart nginx.service
  1. Create index.html file
mkdir -p /var/www/test.com/html
sudo nano /var/www/test.com/html/index.html

add the following HTML code

<html>
  <head>
    <title>Hello world!!</title>
  </head>
  <body>
    <h1>Hello world!!</h1>
  </body>
</html>
  1. Create a config file for your app
sudo nano /etc/nginx/sites-available/test.com

Add the following config below:

server {

        listen 80;
        root /var/www/test.com/html;
        index index.html;

        server_name _;

        location / {
        try_files $uri $uri/ =404;
    }
}
  1. Enable the server by symlinking to the /etc/nginx/sites-enabled directory
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/

Change test.com with your config name.

  1. Restart Nginx service
sudo systemctl restart nginx.service
  1. Verify your configuration
sudo nginx -t
Nginx verification
  1. Verify in the browser

Open the browser and navigate to your server IP address, You should see the following:

Web page

Conclusion

This article showed you how to install and configure Nginx on Ubuntu, You can now start deploying your apps and use Nginx as a web server or reverse proxy.