Install and Configure Nginx on Ubuntu Linux 18.04 LTS nixCraft

I am a new Ubuntu Linux user and a web developer by profession. How do I install and configure Nginx on Ubuntu Linux 18.04 LTS?

Introduction: Nginx is a free and open source web server. Nginx has a service for sending web pages over the Internet. You can send static or dynamic web pages generated by PHP. Nginx is well known for high-performance HTTP, HTTPS and reverse proxy server. It provides a simple configuration and uses low resources on the server. It is an excellent alternative to Apache server.

Install and Configure Nginx on Ubuntu Linux 18.04

The procedure to install Nginx on Ubuntu 18.04 LTS is as follows:

  1. Update the system using apt command
  2. Install Nginx on Ubuntu using apt install nginx
  3. Configure Nginx server
  4. 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

$ ssh user@server
$ ssh vivek@server1.cyberciti.biz

Type the following ip command:
$ ip show
$ ip addr show

Find out Ubuntu server IP address using IP command
Note down the IP address 10.105.28.46. You might have a public IPv4 address. It all depends upon your setup.

Step 3. Install Nginx on Ubuntu Linux 18.04 LTS

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install nginx

Install and Configure Nginx on Ubuntu Linux 18.04 LTS

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
How to start stop and restart Nginx server on Ubuntu Linux

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:
$ sudo ufw status
Open port using UFW Firewall on Ubuntu Linux

Step 6. Verify Nginx is working on Ubuntu 18.04 LTS

Your web server is up and running. It is time to test it. Use the IP address gathered in step # 2. Fire a web browser and type the URL:
http://10.105.28.46/
OR use public IP address:
http://104.200.23.232/

Check your Web Server with a Web Browser to get default Nginx page
Nginx in action with a default page

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., cms.cyberciti.biz or www.cyberciti.biz and so on ) with a directory.

Create a user to store web pages

Add a new Linux user named www-pubcms using the useradd command
$ sudo useradd -s /usr/sbin/nologin -m -d /home/lighttpd/ -c 'cms.cyberciti.biz user' www-pubcms
Lock down the Linux user account using the passwd command:
$ sudo passwd -l www-pubcms
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>CMS.CYBERCITI.BIZ</title> </head> <body> <h1>Welcome</h1> This is a test page for cms.cyberciti.biz. <hr> <small>Powered by Nginx and Ubuntu 18.04 LTS</small> </body>
</html>

Set permission for the dir:
$ sudo chown -vR www-pubcms:www-pubcms /home/lighttpd/

Create virual domain configuration for your cms.cyberciti.biz domain

$ sudo vim /etc/nginx/sites-available/http.cms.cyberciti.biz.conf
OR
$ sudo nano /etc/nginx/sites-available/http.cms.cyberciti.biz.conf
Append the config:

# our first viraul host cms.cyberciti.biz
server { listen 80; # port server_name cms.cyberciti.biz; # dns server name # log files access_log /var/log/nginx/cms.cyberciti.biz_access.log; error_log /var/log/nginx/cms.cyberciti.biz_error.lg; # document root where files stores for cms.cyberciti.biz 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 cms.cyberciti.biz domain:
$ cd /etc/nginx/sites-enabled/
$ sudo ln -v -s /etc/nginx/sites-available/http.cms.cyberciti.biz.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 104.200.23.232. One can use the host command or dig command to verify A record for cms.cyberciti.biz:
$ host cms.cyberciti.biz
cms.cyberciti.biz has address 104.200.23.232
cms.cyberciti.biz has IPv6 address 2600:3c00:1::68c8:17e8

Fire a browser and type your domain name:
http://cms.cyberciti.biz/
Testing your Nginx server

Import Nginx log files

  • /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/cms.cyberciti.biz_access.log
$ cat /var/log/nginx/cms.cyberciti.biz_access.log

Check Nginx logs file to make sure everything working properly
Check Nginx logs file to make sure everything working properly for all domains

Verify that Nginx ports are open on Ubuntu Linux with the ss command or netstat command:
$ ss -tulpn
$ ss -tulpn | grep :80
$ netstat -tulpn

Conclusion

And there you have it, Nginx server installed and configured on an Ubuntu Linux 18.04 LTS server. In the second part of the series, you would learn about configuring HTTPS (SSL/TLS certificates) server for security reasons. For more info see nginx wiki here.

Get 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use “CYBER15” coupon code.
training.linuxfoundation.org

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Posted by wiredgorilla

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.