logo
. . .

How To Install And Configure Nginx on Ubuntu 18.04 LTS

How To Install And Configure Nginx on Ubuntu 18.04 LTS

How To Install And Configure Nginx on Ubuntu 18.04 LTS, phonetically articulated as Engine X, stands as an open-source, highly regarded web server deployed to accommodate websites and applications. It garners widespread acclaim for its proficiency in functioning as a Load Balancer. This guide delineates the procedural steps necessary to install Nginx on the widely-utilized Linux distribution, Ubuntu. Specifically, it outlines the installation process for Ubuntu 18.04 LTS. These instructions should be largely applicable to other Linux systems and various iterations of Ubuntu.

How To Install Git on Ubuntu 20.04

How To Install And Configure Nginx on Ubuntu

We can install Nginx directly from the Ubuntu repositories using the commands as shown below.

# Refresh packages index
sudo apt update

# Install nginx


sudo apt install nginx

# Installation output


Reading package lists… Done


Building dependency tree


Reading state information… Done


The following additional packages will be installed:
libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common nginx-core


Suggested packages:


fcgiwrap nginx-doc


The following NEW packages will be installed:


libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx nginx-common
nginx-core


Setting up nginx-common (1.14.0-0ubuntu1.7) …


Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.


Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.7) …


Setting up libnginx-mod-mail (1.14.0-0ubuntu1.7) …


Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.7) …


Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.7) …


Setting up libnginx-mod-stream (1.14.0-0ubuntu1.7) …


Setting up nginx-core (1.14.0-0ubuntu1.7) …


Setting up nginx (1.14.0-0ubuntu1.7) …


Processing triggers for man-db (2.8.3-2ubuntu0.1) …


Processing triggers for ufw (0.36-0ubuntu0.18.04.1) …


Processing triggers for ureadahead (0.100.0-21) …


Processing triggers for systemd (237-3ubuntu10.38) …
How To Install And Configure Nginx on Ubuntu

Now verify the Nginx installation by checking it’s version as shown below.

# Nginx Version

nginx -v

# Output


nginx version: nginx/1.14.0 (Ubuntu)
How To Install And Configure Nginx on Ubuntu

How To Install And Configure Nginx on Ubuntu. The aforementioned directives will facilitate the installation of the Nginx server along with its requisite dependencies. On my setup, specifically Ubuntu 18.04 LTS, I have installed Nginx version 1.14.0. This version may differ depending on your operating system. Below are the pivotal configurations and directories for Nginx when installed from the Ubuntu repositories.

  • Configurations Directory: /etc/nginx/conf.d – This directory is intended for supplementary server configurations and is initially unoccupied.
  • Modules Available Directory: /etc/nginx/modules-available – Here, you will find the configuration files for various modules.
  • Modules Enabled Directory: /etc/nginx/modules-enabled – This directory contains symlinks to the module configurations. By default, symlinks for geoip, image filter, xslt filter, mail, and stream modules are present.
  • Sites Available Directory: /etc/nginx/sites-available – This is where server block configurations are stored, including the default server block.
  • Sites Enabled Directory: /etc/nginx/sites-enabled – This directory holds symlinks to the server blocks located in /etc/nginx/sites-available. You can activate or deactivate server blocks by creating or removing these symlinks. The default server block symlink is present here.
  • Main Configuration: /etc/nginx/nginx.conf – This file contains the primary configuration settings for Nginx. It can be viewed on GitHub.
  • Default Server Block: /etc/nginx/sites-available/default – This file represents the default server block and is also available on GitHub.

Install And Configure Nginx on Ubuntu – Latest

You can opt to install the most up-to-date version of Nginx by utilizing the official Nginx repository. To completely remove the current installation, use the commands provided below. Ensure you backup any existing configuration files, especially if you have been using Nginx to host applications.

# Backup main configuration

sudo cp /etc/nginx/nginx.conf /<backups path>/nginx.conf.bak

# Backup server blocks

# Completely Uninstall Nginx


sudo apt purge nginx nginx-full nginx-common nginx-core
How To Install And Configure Nginx on Ubuntu

Now create the repository source file using the commands as shown below. I have used the nano editor to create and save the file. You can use any editor of your choice.

# Create the repository source file

sudo nano /etc/apt/sources.list.d/nginx.list

# Update the file


deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ bionic nginx

# Save and close the editor
How To Install And Configure Nginx on Ubuntu

Save the file using the Nano text editor by pressing CTRL + O, then press Enter to write the file. Press CTRL + X to close the editor. You can use the below-mentioned content for Ubuntu 19.04 by replacing bionic with disco.

# Update the file

deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ disco nginx

deb-src http://nginx.org/packages/mainline/ubuntu/ disco nginx
How To Install And Configure Nginx on Ubuntu

Now verify the package integrity using the commands as shown below.

# Get the signing key

wget http://nginx.org/keys/nginx_signing.key

# Add the key


sudo apt-key add nginx_signing.key

# Output


OK
How To Install And Configure Nginx on Ubuntu

The above commands confirm the addition of the official Nginx repository to the system. Now refresh the packages index and install the latest Nginx using the commands as shown below.

# Refresh packages index

sudo apt update

# Install nginx


sudo apt install nginx

# Installation output


Reading package lists… Done


Building dependency tree


Reading state information… Done


The following NEW packages will be installed:


nginx
….
….
Unpacking nginx (1.19.0-1~bionic) …


Setting up nginx (1.19.0-1~bionic) …


Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service →

/lib/systemd/system/nginx.service.

Processing triggers for man-db (2.8.3-2ubuntu0.1) …


Processing triggers for ureadahead (0.100.0-21) …


Processing triggers for systemd (237-3ubuntu10.38) …
How To Install And Configure Nginx on Ubuntu

Now verify the installation by checking the Nginx version as shown below.

# Check Nginx Version

nginx -v

# Output


nginx version: nginx/1.19.0

Nginx version 1.19.0 was successfully installed on Ubuntu 18.04 LTS. Below are the crucial configuration files and directories for Nginx installations derived from the official Nginx repositories.

  • Configuration Directory: /etc/nginx/conf.d – Contains supplementary server configurations. The default server block resides in this directory.
  • Modules Directory: This is a symbolic link to /usr/lib/nginx/modules.
  • Main Configuration File: /etc/nginx/nginx.conf – This file holds the primary configurations for Nginx. You can view it on GitHub.
  • Default Server Block: /etc/nginx/conf.d/default.conf – This file represents the default server block and is available for viewing on GitHub.

Upon comparing the Nginx installations from the Ubuntu repositories with those from the Nginx repositories, we observe several distinctions. The Nginx version from the Nginx repositories lacks configuration directories such as modules available, modules enabled, sites available, and sites enabled. Additionally, the default server block is configured within the /etc/nginx/conf.d directory.

# Include server block files

sudo nano /etc/nginx/nginx.conf

# Scroll down and make sure that the below mentioned lines are there in the http block
include /etc/nginx/conf.d/*.conf;


include /etc/nginx/sites-enabled/*;
How To Install And Configure Nginx on Ubuntu

Turn off the server token for production usage.

# Update config

sudo nano /etc/nginx/nginx.conf

# Update/Add server tokens config – http block
server_tokens off;

# Save and exit the editor

Certbot Plugin

Optionally, implement the Certbox extension indispensable for procuring Let’s Encrypt SSL certifications. We can leverage Certbox to fabricate and install SSL certifications, thereby fortifying the website or application with robust security measures.

# Install Certbot Plugin


sudo apt install python3-certbot-nginx
How To Install And Configure Nginx on Ubuntu

Embark on your journey of mastering Nginx by delving into comprehensive guides on setting up Virtual Hosts or Server Blocks, and on the meticulous installation of Let’s Encrypt SSL certificates on Ubuntu. These resources illuminate the intricate processes behind Server Block configurations and the generation of SSL certificates with Let’s Encrypt.

How to Install a Wildcard SSL Certificate on Multiple Servers

Authenticate Nginx Installation

To ascertain the integrity of the Nginx installation accomplished in the preceding segments, execute the following commands, which serve as a testament to the veracity of your setup:

# Check version

nginx -v

# Ubuntu repositories


nginx version: nginx/1.14.0 (Ubuntu)

# Nginx repository


nginx version: nginx/1.17.4

# Detailed version details
nginx -V

# Output


nginx version: nginx/1.17.4


built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)


built with OpenSSL 1.1.1 11 Sep 2018


TLS SNI support enabled
….
….
How To Install And Configure Nginx on Ubuntu


Also, check the configuration file and status of Nginx using the command as shown below.

# Check configuration
sudo nginx -t

# Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


# Check status
systemctl status nginx

# It will show status
● nginx.service – nginx – high performance web server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: http://nginx.org/en/docs/
….
….
How To Install And Configure Nginx on Ubuntu

We can see that the Nginx is installed successfully and the configuration is ok.

# Start Nginx

sudo systemctl start nginx
How To Install And Configure Nginx on Ubuntu

The quintessential directory for Nginx’s web files resides at /usr/share/nginx/html. This repository serves as the locus from which static content is disseminated, facilitated by the default server configuration.

Proceed to launch your preferred web navigator and input http://localhost, pressing Enter thereafter. For instances involving a remote server, substitute with the corresponding IP address, formatted as http://xx.xx.xx.xx, where xx.xx.xx.xx symbolizes the server’s IP. This should unveil the default Nginx landing page, illustrated in Fig 1.

Common Commands

This section lists the commonly used commands to manage the Nginx process. These commands are listed below.

# Nginx Status
sudo systemctl status nginx

# Start Nginx
sudo systemctl start nginx

# Check status
sudo systemctl status nginx

# Status Output after starting the server
● nginx.service – nginx – high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-10-02 14:08:59 IST; 3s ago
Docs: http://nginx.org/en/docs/
Process: 6263 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 6264 (nginx)
Tasks: 2 (limit: 4915)
CGroup: /system.slice/nginx.service
├─6264 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─6265 nginx: worker process



# Stop Nginx
sudo systemctl stop nginx

# Reload Nginx
sudo systemctl reload nginx

# Enable Nginx on system boot
sudo systemctl enable nginx

# Disable Nginx on system boot
sudo systemctl disable nginx
How To Install And Configure Nginx on Ubuntu

Disable Default Server Block

This step is optional and you can follow it in case you want to disable the default server block which shows the Default Page as shown in Fig 1. It can be done as shown below.

# Disable default configs
sudo nano /etc/nginx/nginx.conf

# Scroll down and update the http block
….
….
keepalive_timeout 65;

#gzip on;

#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

# Save and exit the editor

# Optional – Only if sites-available and sites-enabled directories exist
# Remove default config symlink from sites-enabled
sudo rm /etc/nginx/sites-enabled/default

# Reload Nginx
sudo systemctl reload nginx

Upon the actuation of an NGINX reload, the default server block is rendered inactive. Should the directory of configurations at /etc/nginx/conf.d be requisitioned for alternative configurations beyond server blocks, the pre-existing default configuration file can be excised.

# Backup the file – /etc/nginx/conf.d/default.conf
sudo mkdir /etc/nginx/sites-available
sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/default

# Enable default configs
sudo nano /etc/nginx/nginx.conf

# Scroll down and update the http block
….
….
keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

# Save and exit the editor

# Remove default server block – if exists
sudo rm /etc/nginx/conf.d/default.conf

# Optional – Only if sites-available and sites-enabled directories exist
# Remove default config symlink from sites-enabled
sudo rm /etc/nginx/sites-enabled/default

# Reload Nginx
sudo systemctl reload nginx

Thus, we can maintain the configurations within the /etc/nginx/conf.d/ directory instead of completely disabling it. If you attempt to access the default URL in your browser, an error message will be displayed: “Unable to connect.”

Configuring Individual Websites

This section delves into the process of adding configurations for each website independently. Begin by establishing the site configuration using the default settings as outlined below. In the examples provided, the domain name used is example.com. Be sure to substitute it with your own domain.

# Create the directories if does not exist
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled

# Copy the default config
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
# OR
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/example.com

Next, modify the configuration to integrate your domain and designate the system path containing your website’s files. The configuration should resemble the example provided below.

# Update configuration
sudo nano /etc/nginx/sites-available/example.com

# Content
server {
listen 80;
server_name example.com www.example.com;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /var/www/example.com/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

# Save and exit the editor

# Create the site directory and update permissions
sudo mkdir /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Use the below-mentioned commands to enable or disable the site configuration.

# Enable Site – Create the symlink
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# Disable Site
sudo rm /etc/nginx/sites-enabled/example.com
# OR
sudo unlink /etc/nginx/sites-enabled/example.com

Apply the configuration using the below-mentioned commands.

# Check configurations
sudo nginx -t

# Reload configuration
sudo nginx -s reload
# OR
sudo systemctl reload nginx

Now add the index.html file having content as shown below.

# Add index.html
sudo nano /var/www/example.com/html/index.html

# Content
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>My Domain</title>
</head>
<body>
<h1>Welcome to My Domain.</h1>
</body>
</html>

# Save and exit the editor

This is all about configuring the site. You may fine-tune the configuration by adding error pages, SSL certificate, etc based on your site needs.

Hash Bucket Memory Problem

You may face the hash bucket memory problem in case you are using server blocks. It can be avoided by updating the configuration file as shown below.

# Update config
sudo nano /etc/nginx/nginx.conf

# Update the file


http {

server_names_hash_bucket_size 64;

}

Apply the configuration using the below-mentioned commands.

# Check configurations
sudo nginx -t

# Restart nginx
sudo systemctl restart nginx

Now open your website with the URL http://ekample.com or http://www.ekample.com. It should display your website. Make sure the domain entry is properly configured for use by your server. The domain should point to your server’s IP address.

Summary

This way we can install and configure Nginx for Ubuntu. The previous section also provides common commands used for Nginx.