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.
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.
Start by logging into your server using the ssh command:
$ ssh user@server
To do this, simply execute the following ip command:
$ ip show
$ ip addr show
In this step, run the following apt command to install security updates on Ubuntu 18.04:
$ sudo apt update
$ sudo apt upgrade
In order to install Nginx web server, execute:
$ sudo apt install nginx
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
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
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.
Let us set up our public domain (e.g., sub.linuxapt.com or www.linuxapt.com) with a directory.
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/
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
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