×


Steps to Install and Configure Nginx on Ubuntu Linux 18.04 LTS?

Do you need to install and configure Nginx on Ubuntu Linux 18.04 LTS?

This guide is for you.

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.
In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Software installation Tasks on Linux.
In this context, we shall look into Install Nginx on Ubuntu Linux 18.04.

How to Install and Configure Nginx on Ubuntu Linux 18.04?

In order to install Nginx on Ubuntu 18.04 LTS, simply follow the easy steps below;
i. Update the system using apt command.
ii. Install Nginx on Ubuntu using apt install nginx.
iii. Configure Nginx server.
iv. Enable and restart Nginx server.
Let us see all steps in details to install Nginx on Ubuntu Linux 18.04 LTS server.

Step 1. Login to your server using the ssh command

Start by logging into your server using the ssh command:

$ ssh user@server

Step 2. Find out your Ubuntu Linux server IP address

To do this, simply execute the following ip command:

$ ip show
$ ip addr show

Step 3. Install Nginx on Ubuntu Linux 18.04 LTS

In this step, run the following apt command to install security updates on Ubuntu 18.04:

$ sudo apt update
$ sudo apt upgrade

How to install Nginx on Ubuntu 18.04

In order to install Nginx web server, execute:

$ sudo apt install nginx

Step 4. commands to start/stop/restart Nginx server on Ubuntu

Enable Nginx server at boot time using the systemctl command:

$ sudo systemctl enable nginx

Start Nginx server using the systemctl command:

$ sudo systemctl start nginx

Restart Nginx server using the systemctl command:

$ sudo systemctl restart nginx

Stop Nginx server using the systemctl command:

$ sudo systemctl stop nginx

Reload Nginx server using the systemctl command:

$ sudo systemctl reload nginx

Get status of Nginx server using the systemctl command:

$ sudo systemctl status nginx

Step 5. Open port 80 and 443 using UFW on Ubuntu Linux (firewall config)

UFW is an acronym for uncomplicated firewall. It is used for managing a Linux firewall and aims to provide an easy to use interface for the user.
To open port 80 (HTTP) and HTTPS (443), run:

$ sudo ufw allow https comment 'Open all to access Nginx port 443'
$ sudo ufw allow http comment 'Open access Nginx port 80'
$ sudo ufw allow ssh comment 'Open access OpenSSH port 22'
$ sudo ufw enable

Verify it by running:

$ sudo ufw status

Step 6. Verify Nginx is working on Ubuntu 18.04 LTS

Now, the web server will be up and running. You can test it by using the server IP address as per step # 2.
To do this, open you web browser and enter the IP address or URL.
The default Nginx page indicates that the Ubuntu and Nginx server is running fine on your system.


Step 7. Configure Nginx server

Let us set up our public domain (e.g., sub.linuxapt.com or www.linuxapt.com) with a directory.

Create a user to store web pages

Add a new Linux user named www-linuxapt using the useradd command;

$ sudo useradd -s /usr/sbin/nologin -m -d /home/lighttpd/ -c 'sub.linuxapt.com user' www-linuxapt

Lock down the Linux user account using the passwd command:

$ sudo passwd -l www-linuxapt

passwd: password expiry information changed.
Make a directory to store web pages using the mkdir command

$ sudo mkdir -v /home/lighttpd/http/

Create a new sample web page
Use a text editor such as nano command or vim command:

$ sudo nano /home/lighttpd/http/index.html

OR

$ sudo vim /home/lighttpd/http/index.html

Append the following HTML code:

<html>
        <head>
                <title>SUB.LINUXAPT.COM</title>
        </head>
        <body>
                <h1>Welcome</h1>
                This is a test page for sub.linuxapt.com.
                <hr>
                <small>Powered by Nginx and Ubuntu 18.04 LTS</small>
        </body>
</html>

Set permission for the dir:

$ sudo chown -vR www-linuxapt:www-linuxapt /home/lighttpd/

Create virtual domain configuration for your sub.linuxapt.com domain

$ sudo vim /etc/nginx/sites-available/http.sub.linuxapt.com.conf

OR

$ sudo nano /etc/nginx/sites-available/http.sub.linuxapt.com.conf

Append the config:
# our first virtual host sub.linuxapt.com

server {
    listen      80;             # port
    server_name sub.linuxapt.com; # dns server name
 
    # log files
    access_log  /var/log/nginx/sub.linuxapt.com_access.log;
    error_log   /var/log/nginx/sub.linuxapt.com_error.lg;
 
    # document root where files stores for sub.linuxapt.com domain
    root /home/lighttpd/http;
    index index.html index.htm;
}

Save and close the file. Create a new soft link using ln command in the sites-enabled directory to enable sub.linuxapt.com domain:

$ cd /etc/nginx/sites-enabled/
$ sudo ln -v -s /etc/nginx/sites-available/http.sub.linuxapt.com.conf .

Test and gracefully reload nginx server

$ sudo nginx -t

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

$ sudo systemctl reload nginx

Make sure you map domain name to your public IP address such as xxx.xxx.xx.xxx. One can use the host command or dig command to verify A record for sub.linuxapt.com:

$ host sub.linuxapt.com
sub.linuxapt.com has address xxx.xxx.xx.xxx
sub.linuxapt.com has IPv6 address 1300:3c00:1::68c8:32e8

Fire a browser and type your domain name:

http://sub.linuxapt.com/

How to import Nginx log files?

To do this;

/var/log/nginx/ – Nginx server log files.
/etc/nginx/ – Nginx server config files directory. All active site config can be found in /etc/nginx/sites-enabled/ directory linked from actual config file directory at /etc/nginx/sites-available/
/etc/nginx/nginx.conf – Your main nginx config file.

Use the tail command or more command or grep command or cat command to view server log files:

$ tail -f /var/log/nginx/access.log
$ more /var/log/nginx/error.log
$ grep 'something' /var/log/nginx/sub.linuxapt.com_access.log
$ cat /var/log/nginx/sub.linuxapt.com_access.log

Verify that Nginx ports are open on Ubuntu Linux with the ss command or netstat command:

$ ss -tulpn
$ ss -tulpn | grep :80
$ netstat -tulpn

[Need urgent assistance in installing Software Packages on your Linux Server? We are available to help you today.]


Conclusion

This tutorial will guide you on how to get Nginx server installed and configured on an Ubuntu Linux 18.04 LTS server.

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


Nginx Configuration Files Structure

  • All configuration files are located in the /etc/nginx/ directory.
  • Nginx main configuration file is at /etc/nginx/nginx.conf.
  • It's best practice to create a separate configuration file of each domain for better maintainability.
  • New server blocks (configuration file) of each domain should be stored in /etc/nginx/sites-availabledirectory. You need to make symlink of these configuration files at /etc/nginx/sites-enabled to take in used by Nginx.
  • Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a /etc/nginx/sites-enabled directory to the sites-enabled directory.
  • The default server web root directory is /var/www/html
  • It's best practice to to follow standard naming convention. Nginx server block files name should as domain name and must end with .conf extension. For example, your domain name is example.com then server block file name should example.com.conf
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It's also recommended to have a different access and error log files for each server block.