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@serverTo do this, simply execute the following ip command:
$ ip show$ ip addr showIn this step, run the following apt command to install security updates on Ubuntu 18.04:
$ sudo apt update$ sudo apt upgradeIn order to install Nginx web server, execute:
$ sudo apt install nginxEnable Nginx server at boot time using the systemctl command:
$ sudo systemctl enable nginxStart Nginx server using the systemctl command:
$ sudo systemctl start nginxRestart Nginx server using the systemctl command:
$ sudo systemctl restart nginxStop Nginx server using the systemctl command:
$ sudo systemctl stop nginxReload Nginx server using the systemctl command:
$ sudo systemctl reload nginxGet status of Nginx server using the systemctl command:
$ sudo systemctl status nginxUFW 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 enableVerify it by running:
$ sudo ufw statusNow, 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-linuxaptLock down the Linux user account using the passwd command:
$ sudo passwd -l www-linuxaptpasswd: 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.htmlOR
$ sudo vim /home/lighttpd/http/index.htmlAppend 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.confOR
$ sudo nano /etc/nginx/sites-available/http.sub.linuxapt.com.confAppend 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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginxMake 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.comsub.linuxapt.com has address xxx.xxx.xx.xxxsub.linuxapt.com has IPv6 address 1300:3c00:1::68c8:32e8Fire 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.logVerify that Nginx ports are open on Ubuntu Linux with the ss command or netstat command:
$ ss -tulpn$ ss -tulpn | grep :80$ netstat -tulpnThis 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