How To Install and configure Nginx on Ubuntu 16.04/18.04 LTS

Introduction

Nginx is one of the most popular web servers in the world which serves worlds some high traffic and largest sites on the Internet. It is free, open-source, high-performance HTTP and reverse proxy  server.In this tutorial we will learn how to install and configure nginx on ubuntu 16.04 .

Before we begin

  • You should have a regular, non-root user with sudo  privileges
  • Make sure you don’t have Apache or any other service running on port 80 

Step 1: Install Nginx

Nginx packages are available in Ubuntu default software repositories.So installation is simple and straight forward.

First,update our local package index in order to get latest packages

sudo apt-get update

Now install nginx as below

sudo apt-get install nginx

This will install Nginx and any required dependencies like nginx-core,nginx-common to your server.

Once the installation is completed Nginx will be automatically started.

2.Check Nginx installation

You can make sure that Nginx service is running with the following command:

sudo systemctl status nginx

Will get output like

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-07-11 20:53:40 IST; 9min ago
 Main PID: 5016 (nginx)
   CGroup: /system.slice/nginx.service
           ├─5016 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─5017 nginx: worker process                          
           └─5018 nginx: worker process  
Jul 11 20:53:40 gitserver systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 11 20:53:40 gitserver systemd[1]: Started A high performance web server and a reverse proxy server.
Jul 11 20:53:55 gitserver systemd[1]: Started A high performance web server and a reverse proxy server.                        

You can also use command sudo service nginx status to check the nginx status

As you can see above, the service appears to have started successfully. By default nginx will run on port 80.However, the best way to test this is to actually request a page from Nginx.

When correctly installed Nginx’s default file will appear in /var/www/html as index.nginx-debian.html.

You can access the default Nginx landing page to confirm that the software is running properly. You can access this through your server’s domain name or IP address.

Since it is installed on our local machine just type http://localhost (If you hava domain name or ip type instead of localhost).You can see output like below

Manage the Nginx service with systemctl

You can manage the Nginx service  by using below commands

To stop the Nginx service, run:

sudo systemctl stop nginx 

To start the Nginx service, type:

sudo systemctl start nginx 

To check Nginx configuration is correct

nginx -t

It must give below output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

if it gives any error we need to check the configuration files and fix the issue

Restart the Nginx service:

sudo systemctl restart nginx

Reload the Nginx service after you have made some configuration changes:

sudo systemctl reload nginx

Enable the Nginx service to start at boot

sudo systemctl enable nginx

Nginx Configuration File’s Structure and Best Practices 

  • All Nginx configuration files are located in the /etc/nginx/ directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • Nginx server block files are stored in /etc/nginx/sites-available directory. The configuration files found in this directory are not used by Nginx unless they are linked to the /etc/nginx/sites-enabled directory.
  • To activate a server block you need to create a symlink (a pointer) from the configuration file sites in a sites-available directory to the sites-enabled directory.
  • It is a good idea to follow a standard naming convention, for example if your domain name is myexample.com then your configuration file should be named /etc/nginx/sites-available/myexample.com.conf
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory.
  • You can set your domain document root directory to any location you want. The most common locations for webroot include:/home/<user_name>/<site_name>,/var/www/<site_name> ,/var/www/html/<site_name>

Conclusion

 You have successfully installed Nginx on your Ubuntu 16.04 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server

Leave a Reply