Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

How To Install and Configure Nginx on Ubuntu 16.04

SelvakumarSelvakumar

Nginx is the best web server which has the ability to handle websites which are receiving a large number of visitors.

Nginx is a high-performance web server software. It is developed with an intention to handle a large quantity of traffic coming to a site.

The specialty of Nginx is its capability to run on small resource and handle a large number of requests. Nginx works as a web server, proxy, load balancer and Reverse Proxy.

Here, in this article, we are going to see How to install Nginx on Ubuntu 16.04

Prerequisites:

You need the Ubuntu 16.04 server with non-root users with sudo permissions. Just refer the article how to create a sudo user in a server if you are just starting with a new server with root user.

Step 1: Installing Nginx

Installing Nginx on Ubuntu is the easier task. The Ubuntu repository has the Nginx Packages.

We are going to use the simple apt-get commands to install the Nginx.

just update the repository package index so that we will get the new updated packages list. Now, let us install Nginx.

$ sudo apt-get update
$ sudo apt-get install Nginx

The terminal will show you the details about the Nginx and ask you whether to install the Nginx. Enter Y and the Nginx will be installed on your server.

Step 2: Adjust the firewall to allow Nginx

Before we work with the Nginx, Let us set it up for the firewall access. When you install the Nginx, it will register itself as a service in ufw our server firewall.

So that you can easily enable the access to Nginx. Let us list the applications which ufw can work with. Execute the below command.

 $ sudo ufw app list

You will get an output like this.

utput
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

The above three are the available profile in Nginx. Here let us see what are all those can do.

NginxFull: This is the profile which will open both port 80 (for Unencrypted traffic to the server) and port 443 ( For SSL/TSL encrypted traffic)

NginxHTTP: Only the port 80 will be opened by this profile

Nginx HTTPS: This profile only opens the port 443( SSL/TSL encrypted traffic).

you can use any of them to allow the traffic. Here in this article, we are going to use port 80 which allows unencrypted traffic as we have not configured the SSL for our server Yet.

Now:

Let us enable the Nginx HTTP by executing the following command.

$ sudo ufw allow 'Nginx HTTP'

Now verify whether changes have been made.

$ sudo ufw status

You should get the following output which shows that HTTP traffic is allowed.

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Step 3: Checking Nginx through Browser

After the completion of the installation process, the Nginx should start and run automatically.

You can check that by using systemd init to ensure that the service is running.

$ systemctl status nginx

The output will be like this.

Output
● 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 Mon 2016-04-18 16:14:00 EDT; 4min 2s ago
Main PID: 12857 (nginx)
CGroup: /system.slice/nginx.service
       ├─12857 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
       └─12858 nginx: worker process

The above output confirms that the Nginx is running and up. But we have to test that by requesting a page from Nginx through the browser.

For that, you have to enter your IP address of the server or domain in the browser. The default Nginx page will be shown.

If you don't have the domain and want to test the Nginx through your web browser, you will need your server IP address.

If you don't know your server's Public IP address, you can get that in another way. Here is the command to get to know your server Public IP address.

$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

It will give you some lines. Just paste them in the browser and see if they work. There is also another command which you can use to display the Public IP.

$ sudo apt-get install curl
$ curl -4 icanhazip.com

Input your server's IP address or Domain name in the server. Here you will see the following Nginx page.

It shows that Nginx is running properly on the server.

Step 4: Manage the Nginx

Now your server is running and up. To manage the Nginx you should know some basic commands.

To stop the nginx from running

$ sudo systemctl stop nginx

To start the web server

$ sudo systemctl start nginx

To restart the web server

 $ sudo systemctl restart nginx

If you have made some changes in the configuration, you have to reload the Nginx. Here you can make this without dropping connection using the below command.

$ sudo systemctl disable nginx

Nginx is automatically started when the server boots. If you don't want the Nginx to start at the boot, execute the below command.

$ sudo systemctl disable nginx

If you want to start the Nginx at the server boot.

$ sudo systemctl enable nginx

Step 5: Nginx Files and Directories you should know

Now you know, how to setup the Nginx, But you should know the important directories and files in the Nginx.

Content:

/var/www/html: This is the directory where the web content of your website will be stored. When you input your IP address on the web server, you will see a Nginx page. That default Nginx page content is fetched from this /var/www/html.

Server Configuration:

  • etc/nginx: The Nginx parent directory which will contain all the server configuration file.

  • /etc/nginx/nginx.conf: This is the configuration file of Nginx. Any changes made in this file will reflect in Nginx functionality.

  • /etc/nginx/sites-available: you can store the "server blocks" in this directory. It has the configuration files which will not be used until they are linked with sites-enable directory.

  • /etc/nginx/sites-enabled/: This directory stores the "server blocks". They link to the configuration file in the sites-available directory.

  • /etc/nginx/snippets: Here the configuration fragments are stored and they can be used anywhere in the Nginx Configuration. If you are using specific configuration segments repeatedly, then they can be added to this directory.

Server Logs:

  • /var/log/nginx/access.log: It will store all the entry request to the web server if you have configured to do that.
  • /var/log/nginx/error.log: Nginx errors are recorded in this file.

Conclusion

Here in this article you have learned the following

  • How to Install Nginx on Ubuntu
  • Adjusting the firewall to make the Nginx work
  • Managing the Nginx

You can decide the type of content which you would like to serve. Nginx is powerful and flexible.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments