Blog

03/06/2019

Install and Configure Nginx on Ubuntu Linux 18.04 LTS



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.sxi.io

Step 2. Find out your Ubuntu Linux server IP address

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


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

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:
$ sudo ufw status

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/

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.sxi.io or sxi.io 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.sxi.io 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.sxi.io.
                <hr>
                <small>Powered by Nginx and Ubuntu 18.04 LTS</small>
        </body>
</html>

<html> <head> <title>CMS.CYBERCITI.BIZ</title> </head> <body> <h1>Welcome</h1> This is a test page for cms.sxi.io. <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.sxi.io domain

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

# our first viraul host cms.sxi.io
server {
    listen      80;             # port
    server_name cms.sxi.io; # dns server name
 
    # log files
    access_log  /var/log/nginx/cms.sxi.io_access.log;
    error_log   /var/log/nginx/cms.sxi.io_error.lg;
 
    # document root where files stores for cms.sxi.io domain
    root /home/lighttpd/http;
    index index.html index.htm;
}

# our first viraul host cms.sxi.io server { listen 80; # port server_name cms.sxi.io; # dns server name # log files access_log /var/log/nginx/cms.sxi.io_access.log; error_log /var/log/nginx/cms.sxi.io_error.lg; # document root where files stores for cms.sxi.io 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.sxi.io domain:
$ cd /etc/nginx/sites-enabled/
$ sudo ln -v -s /etc/nginx/sites-available/http.cms.sxi.io.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.sxi.io:
$ host cms.sxi.io
cms.sxi.io has address 104.200.23.232
cms.sxi.io has IPv6 address 2600:3c00:1::68c8:17e8

Fire a browser and type your domain name:
http://cms.sxi.io/

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.sxi.io_access.log
$ cat /var/log/nginx/cms.sxi.io_access.log

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.

This entry is 1 of 2 in the Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 18.04 LTS Tutorial series. Keep reading the rest of the series:
  1. Install and Configure Nginx on Ubuntu Linux 18.04 LTS
  2. Secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS Validation

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

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.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...