Provides all the steps required to install 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
October 02, 2019

Nginx pronounced as Engine X is an open-source popular web server used to host websites and applications. It's popular for its capability to act as Load Balancer. This tutorial provides the steps required to install Nginx on the popular Linux distribution Ubuntu. It provides all the steps required to install and use Nginx on Ubuntu 18.04 LTS. The steps should be similar for other Linux systems and Ubuntu versions.

Install Nginx - Default

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
...
...
Setting up nginx-core (1.14.0-0ubuntu1.6) ...
Setting up nginx (1.14.0-0ubuntu1.6) ...
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.29) ...

The above-mentioned commands will install the Nginx server and its dependencies. I got Nginx 1.14.0 installed on my system i.e. Ubuntu 18.04 LTS. It may vary based on your system. We can also install the most recent version of Nginx as explained in the next section.

Install Nginx - Latest

We can also install the most recent version of Nginx using the official Nginx repository. You can remove the existing installation completely using the commands as shown below. Make sure to take the backup of existing configuration files in case you have used Nginx to host the applications.

# Backup main configuration
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

# Backup sites configuration

# Uninstall Nginx
sudo apt remove nginx nginx-full nginx-common nginx-core

# Remove configurations
sudo apt purge nginx

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

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

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

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
....
....
Unpacking nginx (1.17.4-1~bionic) ...
Setting up nginx (1.17.4-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.29) ...

I got the most recent version of Nginx on my system i.e. version 1.17.4 while writing this tutorial. You can also include server block files and install the Certbox plugin for Nginx using the commands as shown below.

# 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/*;

# Install Certbot Plugin
sudo apt install python3-certbot-nginx

Verify Nginx

We can verify the Nginx installed by us in the previous sections using the commands as shown below.

# 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
....
....

Turn off the server token for production usage.

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

# Update server tokens config
server_tokens off;

# Save and exit the editor

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/
....
....

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

# Start Nginx
sudo systemctl start nginx

Now open your browser and navigate to http://localhost and hit Enter. You can use the system IP address in case of remote server - http://xx.xx.xx.xx where xx.xx.xx.xx is the server IP address. It should show the default page of Nginx as shown in Fig 1.

Nginx Installed

Fig 1

In case you are unable to see the page, as shown above, make sure that your firewall is configured properly and port 80 is available for Nginx.

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

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

You can also delete the default config file in case the configurations direrctory /etc/nginx/conf.d is required for other configs.

# 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/*;
}

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

# 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

In this way, we can keep the configurations within the directory /etc/nginx/conf.d/ instead of completely disabling it. Now if you try to open the default URL in the browser, it will show an error message.

Configure Site

This section explains about adding the site configuration separately for each site. We can start the site configuration using the default config as shown below. I have used the domain example.com for the examples. Make sure to replace 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

Now update the configuration to configure your domain and the system path having site files. The configuration should look like the one as shown 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 site using the URL - http://example.com or http://www.example.com. It should show your site. Make sure that the domain record is configured properly to use your server. The domain should point to the IP address of your server.

Summary

This is how we can install and configure Nginx for Ubuntu. The last section also provided the commonly used commands for Nginx.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS