Iam 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:
- Update the system using apt command
- Install Nginx on Ubuntu using apt install nginx
- Configure Nginx server
- 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
First, login into your server using the ssh command:$ ssh user@server
$ ssh vivek@server1.cyberciti.biz
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
Run the following apt command to install security updates on Ubuntu 18.04:$ sudo apt update
$ sudo apt upgrade
Sample outputs:
Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Fetched 252 kB in 1s (503 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done All packages are up to date.
How to install Nginx on Ubuntu 18.04
Finally install Nginx weber server:$ 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/
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
$ 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/
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
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.