How to install Nginx on Ubuntu 18.04

0 Shares
0
0
0
0

Introduction

Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most visited sites on the internet. In most cases, it is more resource-friendly than Apache and can be used as a web server or reverse proxy.

In this guide, you will learn how to install Nginx on an Ubuntu 18.04 server and about important Nginx files and directories.

Prerequisites

Before starting this guide, you need to configure a regular, non-root user with sudo privileges and a basic firewall on your server. You can learn how to configure a regular user account by following our basic server setup guide for Ubuntu 18.04.

Once you have an account available, log in as a non-root user to get started.

Step 1 – Install Nginx

Since Nginx is available in the default Ubuntu repositories, it can be installed from these repositories using the apt packaging system.

Since this may be your first interaction with the apt packaging system in this session, refresh your local package directory to have access to the latest package lists. After that, you can install nginx:

sudo apt update
sudo apt install nginx

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

Step 2 – Configure the Firewall

Before testing Nginx, the firewall software must be configured to allow access to the service. After installation, Nginx registers itself as a service with ufw, making it easy to allow access to Nginx.

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

sudo ufw app list

Your output should be a list of application profiles:

Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

This list shows the 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 only opens port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile only opens port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that still allows the traffic you have configured. Since you have not yet configured SSL for your server in this guide, you only need to allow traffic on port 80.

You can enable this by typing the following:

sudo ufw allow 'Nginx HTTP'

Then, confirm the change:

sudo ufw status

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

Output
Status: 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 have added the appropriate firewall rule, you can check that your web server is running and can serve content properly.

Step 3 – Check your web server

At the end of the installation process, Ubuntu 18.04 will start Nginx. The web server should already be started.

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

systemctl status nginx
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 is to actually request a page from Nginx.

You can access the default Nginx landing page by navigating to your server's IP address to verify that the software is running properly. If you don't know your server's IP address, you can get it in a few different ways.

Try typing the following into your server's command line:

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

You will get a few lines. You can try each one in your web browser to verify if they work.

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

curl -4 icanhazip.com

Once you have your server's IP address, enter it into your browser's address bar:

http://your_server_ip

You should get the default Nginx landing page:

This page comes with Nginx to verify that the server is running properly.

Step 4 – Manage the Nginx Process

Now that you have your web server set up, let's review some basic administrative commands.

To stop your web server, type the following:

sudo systemctl stop nginx

To start the web server if it stops, type the following:

sudo systemctl start nginx

To stop and then restart the service, type the following:

sudo systemctl restart nginx

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

sudo systemctl reload nginx

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

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

sudo systemctl enable nginx

Nginx should now automatically restart when the server boots.

Step 5 – Set up server blocks (recommended)

When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We’ll set up a domain called your_domain , but you should replace it with your own domain name. For more information on setting up domain names with DigitalOcean, see our introduction to DigitalOcean DNS.

Nginx in Ubuntu 18.04 has a server block enabled by default that is configured to serve documents outside of a directory in /var/www/html. While this works fine for a single site, it can get cumbersome if you host multiple sites. Instead of changing /var/www/html, let's create a directory structure in /var/www for our your_domain site, and leave /var/www/html as the default directory to serve in case it doesn't match the client's request. Other sites

Create a directory for your_domain as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Next, assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain/html

If you haven't changed your umask value, your web root permissions should be correct, but you can make sure by typing the following:

sudo chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

nano /var/www/your_domain/html/index.html

Inside, add the following HTML sample:

<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain server block is working!</h1>
</body>
</html>

Save and close the file when you're done. If you're using nano, you can exit by pressing CTRL + X then Y and ENTER.

In order for Nginx to serve this content, you need to create a server block with the correct instructions. Instead of directly modifying the default configuration file, create a new file in /etc/nginx/sites-available/your_domain:

sudo nano /etc/nginx/sites-available/your_domain

Add the following configuration block, which is similar to the default, but updated for your new directory and domain name:

server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain.com www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}

Note that we have updated the root configuration to the new directory and the server_name to the domain name. Save and close the file when you are done.

Then, enable the file by creating a link from it to the sites-enabled directory, which Nginx will read from when it starts:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Now two server blocks are enabled and configured to respond to requests based on the listen and server_name directives (you can read more about how Nginx processes these directives here):

  • your_domain: Responds to requests for your_domain and www.your_domain.
  • Default: Responds to any request on port 80 that does not match the other two blocks.

To avoid the potential hash bucket memory issue that may arise from adding additional server names, it is necessary to set a single value in the /etc/nginx/nginx.conf file. Open the file:

sudo nano /etc/nginx/nginx.conf

Find the server_names_hash_bucket_size command and remove the # symbol to uncomment the line:

...
http {
...
server_names_hash_bucket_size 64;
...
}
...

Save and close the file when you are finished.

Next, test to make sure there are no syntax errors in any of your Nginx files:

sudo nginx -t

If there are no problems, restart Nginx for your changes to take effect:

sudo systemctl restart nginx

Nginx should now serve your domain name. You can test this by going to http://your_domain, where you should see something like the following:

Step 6 – Familiarize yourself with important Nginx files and directories

Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.

Content
  • /var/www/html: The actual web content, which by default consists of just the Nginx default page you saw earlier, is served outside of the /var/www/html directory. This can be changed by editing the Nginx configuration files.
Server configuration
  • /etc/nginx: Nginx configuration directory. All Nginx configuration files are located here.
  • /etc/nginx/nginx.conf: The main Nginx configuration file. This can be edited to make changes to the global Nginx configuration.
  • /etc/nginx/sites-available/: A directory where server blocks for each site can be stored. Nginx will not use configuration files in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory and then enabled by linking to another directory.
  • /etc/nginx/sites-enabled/: The directory where the server blocks enabled for each site are stored. Typically, these are created by linking to the configuration files in the existing sites directory.
  • /etc/nginx/snippets: This directory contains configuration snippets that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration sections are good candidates for re-converting into snippets.
Server reports
  • /var/log/nginx/access.log: Every request to your web server is logged in this log file, unless Nginx is configured otherwise.
  • /var/log/nginx/error.log: Any Nginx errors are logged in this log.

Result

Now that you have your web server installed, you have many options for the type of content and technologies you want to use to create a richer experience.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like