PureDevOps Community

Steps, How To Install Nginx on Ubuntu 18.04

Introduction

One of the most widely used web servers in the world, Nginx hosts some of the biggest and busiest websites on the internet. It can be used as a reverse proxy or web server and often uses fewer resources than Apache.

You’ll learn how to install Nginx on your Ubuntu 18.04 server as well as about significant Nginx files and directories in this tutorial.

Prerequisites

You should have a regular, non-root user with sudo permissions and a fundamental firewall set up on your server before starting this guide. By using our initial server configuration instructions for Ubuntu 18.04, you can discover how to set up a standard user account.

When you have an account available, log in as your non-root user to begin.

Step 1 – Installing Nginx

Since Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.

Since this may be your first interaction with the apt packaging system in this session, update the local package index so that you have access to the most recent package listings. Afterward, you can install nginx:

sudo apt update
sudo apt install nginx

Copy

After accepting the procedure, apt will install Nginx and any required dependencies to your server.

Step 2 – Adjusting the Firewall

Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.

List the application configurations that ufw knows how to work with by typing the following:

sudo ufw app list

Copy

Your output should be a list of the application profiles:

OutputAvailable applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

This list displays three profiles available for Nginx:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since you haven’t configured SSL for your server yet in this guide, you’ll only need to allow traffic on port 80.

You can enable this by typing the following:

sudo ufw allow 'Nginx HTTP'

Copy

Then, verify the change:

sudo ufw status

Copy

You should receive a list of HTTP traffic allowed in the output:

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Now that you’ve added the appropriate firewall rule, you can check that your web server is running and able to serve content correctly.

Step 3 – Checking your Web Server

At the end of the installation process, Ubuntu 18.04 starts Nginx. The web server should already be up and running.

Check with the systemd init system to make sure the service is running:

systemctl status nginx

Copy

Output● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Fri 2021-10-01 21:36:15 UTC; 35s ago
     Docs: man:nginx(8)
 Main PID: 9039 (nginx)
    Tasks: 2 (limit: 1151)
   CGroup: /system.slice/nginx.service
           ├─9039 nginx: master process /usr/sbin/nginx -g daemon on; master_pro
           └─9041 nginx: worker process

This output shows that the service has started successfully. However, the best way to test this is to actually request a page from Nginx.

You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, you can get it a few different ways.

Try typing the following at your server’s command prompt:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Copy

You will receive a few lines. You can try each in your web browser to confirm if they work.

An alternative is running the following command, which should generate your public IP address as identified from another location on the internet:

curl -4 icanhazip.com

Copy

When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

You should receive the default Nginx landing page:

Nginx default page

This page is included with Nginx to verify that the server is running correctly.

Step 4 – Managing the Nginx Process

Now that you have your web server up and running, let’s review some basic management commands.

To stop your web server, type the following:

sudo systemctl stop nginx

Copy

To start the web server when it is stopped, type the following:

sudo systemctl start nginx

Copy

To stop and then start the service again, type the following:

sudo systemctl restart nginx

Copy

If you are simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:

sudo systemctl reload nginx

Copy

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing the following:

sudo systemctl disable nginx

Copy

To re-enable the service to start up at boot, you can type the following:

sudo systemctl enable nginx

Copy

Nginx should now start automatically when the server boots again.