Install Nginx on Ubuntu 18.04 & 16.04 – Beginner Tutorial [5 Minutes]

Install Nginx on Ubuntu 18.04 & 16.04 – Beginner Tutorial [5 Minutes]

Introduction

Install Nginx on Ubuntu 16.04 and Ubuntu 18.04 are the same. There is not much different between this procedure.

Now, let us go through the install nginx ubuntu process.

What is Nginx?

Nginx is the Web Server similar to Apache. Nginx is more powerful and powers many high traffic website. It servers static content much faster than Apache. However both of them have their own advantage and benefits.

Nginx can be downloaded from the Ubuntu repository.

Looks like our task is more simplified.

Now, let us look at the requirement and procedures.

Install Nginx on Ubuntu

Requirements:

  • You just need to setup your Ubuntu server as per this ubuntu server setup guide.

Now, let us see how to configure nginx ubuntu.

1. Installing Nginx on Ubuntu 16.04

First update the Ubuntu package manager using the below command.

Since, ubuntu maintains a lot of software in their repository, it makes us easier to download and install Nginx.

The beauty of the process, it’s all simple commands that anyone can execute perfectly.

We will use .apt package manager to install Nginx. Here is the command for that.

It will show you the packages and disk space requirement and accept and install.

Now, all the required packages and Nginx will be installed on server.

The next step in install nginx on ubuntu tutorial is to make sure firewall allows Nginx to function properly.

2. Firewall Configuration for Nginx

When you install Nginx, it will automatically register itself with UFW as a service.

So, basically Nginx is allowed to work on the server without any problem.

Now, Let us see how the list of services allowed by firewall. Use the below command for that.

This sudo app list command will give the following output.

You can see three profiles for the Nginx. Let me explain what are they.

Nginx Full: This profile let’s the Nginx to allow the port 80 and port 443. Here port 80 is unencrypted and the port 443 encrypted.

Nginx HTTP: This profile only allows traffic on port 80 (unencrypted)

Nginx HTTPS: This profile allows Nginx on port 443 (Encrypted)

Now, only we are installing the Nginx on the server and we dont have any website so there is no need of SSL.

So, we only need to allow http traffic. So, just allow that profile using the below command.

Check the status using the below command.

You will get the below output.

3. Test the Web Server Status

To test the web server, use the below command.

We have installed the Nginx on the server already as mentioned in the above install nginx on ubuntu procedure.

So, at this time, it should be running. use systemctl to check the status.

You should get the following status as output.

The active status indicates that the service is running successfully.

We can test it on another way. Just enter your server public IP, you will get the Nginx page displayed on the browser.

Enter your server IP like below.

4. Nginx Management Commands

We can start and stop the Nginx using the commands.

Now, the server is currently running. To stop the Nginx server, use the below command.

To start the service again, use the below command.

You can restart the server using the below command.

If you have made any changes on the server, just use the below command. It won’t cut the existing connections and simply update the changes.

Nginx always starts at system boot everytime when you restart the server. To avoid this, just use the below command to stop Nginx from starting after system boot.

If you want to start the Nginx at system boot, use the below command.

5. Nginx Server Block Setup

Configure Nginx Ubuntu

The Nginx server blocks can be used to host multiple site on single server.

Yes, it is possible to do that. All you have to do is make a small change in the Nginx configuration.

We are not going in that as we focus on single domain setup. I am going to use the example.com here.

But:

You can use your own domain name there.

Nginx always servers the content from /var/www/html/ directory by default.

This setup is only suitable if you are hosting a single site.

Here:

We are going to create another directory for our domain example.com. It will be serve your domain content.

Also, we will leave the /var/www/html to serve if no domain is matching in the nginx configuration.

Execute the below command to create the directory.

Here, the -p will allow us to create any parent directory if neccessary.

After that, we will assign the ownership of the directory using the $USER variable.

Set the permissions for web root by executing the below command.

Then, you have to create the index.html page using the nano editor as mentioned below.

Add the below code in that file, then save and close.

After that, save and close file.

In order for the above content to be served, we have to create a Nginx server block at /etc/nginx/sites-available/example.com. 

Use the nano editor to open the server block.

Paste the below content in the file and save.

After that, execute the below command to enable the link from sites-enabled directory. The sites-enabled file will be ready by Nginx when it starts.

We have configured two blocks to server requests.

  • example.com It will serve the content for example.com and www.example.com
  • default It will serve content for the request on port 80.

We have to make a small change in the Nginx configuration file to avoid the memory hash problem. the problem arises when you add more server names.

Open the file using nano editor as mentioned below.

Go through the configuration  file and find the server_name_hash_bucket_size directory and uncomment the line by removing the # symbol.

Once you are done with the above steps, now check for the syntax error in the Nginx file.

After that, restart the Nginx server using the below command.

Now, if you go to your IP address on your browser, you should see “Success! The example.com server block is working!

6. Nginx Files and Explanations

When you install Nginx for the first time, you will be confused with it’s file and directory names.

For any changes, you have to directly work on those files. At that time, you will not know which directory has to be checked.

Having basic knowledge about such directories and will help you understand the basic functionalities of Nginx.

  • /var/www/html: This directory contains the content which is served on internet. You can upload your website content here to served via the Nginx Server. By default, this directory will have the page which we saw earlier.
  • /etc/nginx: This is Nginx configuration directory which contains the all the configuration file for Nginx.
  • /etc/nginx/nginx.conf: this is main Nginx configuration file. Whatever the changes you make here will reflect globally.
  • /etc/nginx/sites-available/: This directory will contain the server blocks for websites. But, this server blocks will not be used unless they are linked to sites-enabled directory.
  • /etc/nginx/sites-enabled/: This directory contains the enabled server block linked from the sites-available directory.
  • /etc/nginx/snippets: Configuration fragments are contained by this directory.
  • /var/log/nginx/access.log: When you make request to the server, that will be stored here. Each and every request coming to the server is stored here.
  • /var/log/nginx/error.log: All Nginx errors are stored in this Log file.

The above ones are the important Nginx files that you should know.

Uninstall Nginx Ubuntu

If you want to uninstall Nginx on Ubuntu server, use the command below to uninstall nginx while keeping the configuration file for later use.

If you want to uninstall Nginx entirely, just use the below command.

Start Nginx Ubuntu

If you want to start the Nginx Server on Ubuntu server, use the below command.

 

Conclusion

This is the basic Install Nginx on Ubuntu tutorial which is enough to configure the Nginx to serve a single website.

You can install other components such as PHP and MySQL to work with wordpress on this server.

It will be covered in future tutorial. Just, stay tuned with our news letter to get notified for the Next tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *