How to configure Nginx Server Blocks on Ubuntu 18.04?

0
657
nginx
nginx

Recently I explained to you about Virtual hosts and how to configure them in the Apache web server. However, you should know that Apache has a quite popular, efficient and also free alternative. I’m talking about ngnix. In addition, it has Server blocks that are quite similar to the Virtual hosts of apache.

Ngnix is a high-performance lightweight inverse web/proxy server and proxy for email protocols. It is free and open source software, licensed under the Simplified BSD License. At the same time, ngnix has a paid version, dedicated to high-performance companies and projects that require commercial support.

with this in mind, I will teach you how to configure Nginx server blocks using Ubuntu 18.04 LTS.

0. Requirements

In order to perform this tutorial satisfactorily, you should keep this in mind:

  • You need to be logged in with a user with sudo enabled.
  • It is recommended that you have a basic understanding of the use of the terminal.
  • And obviously, need a computer with Ubuntu 18.04.

Note: The steps I will describe must also work on Linux Mint 19.

1.- Install Nginx

In the first place, you need to update the entire operating system. It’s always good to do this because it ensures you have the latest security patches installed. Open a terminal, and run:

1.- Upgrading the system
1.- Upgrading the system

After you enter your password, you will have the system updated.

Now, you can install ngnix. In order to reduce the time spent, install it from the package that comes in the official Ubuntu repositories. Type:

2.- Installing nginx from official repositories
2.- Installing Nginx from official repositories

Next, verify that the firewall is configured to allow nginx to operate.

3.- Listing the application configurations that ufw knows
3.- Listing the application configurations that ufw knows

As a result, you can see the applications that are in profile in the firewall. If you see Nginx Full, Nginx HTTP and Nginx HTTPS, you are doing well.

Check the status of the service.

5.- Nginx status
5.- Nginx status

Go to your browser and access with the IP address of the server.

http://IP_SERVER

6.- Nginx welcome screen on web browser
6.- Nginx welcome screen on the web browser

As can be seen, Nginx has been correctly installed.

2. Create folders for each host

For this tutorial, I will create two server blocks and consequently, a separate folder must be created for each host. Virtual hosts will be named “osradar1.test” and “osradar2.test”. Run:

7.- Creating the folders for the two virtual hosts
7.- Creating the folders for the two virtual hosts

As a result, the newly created folders belong to root. This will have negative consequences on their use. You must change the owner to your user. Type:

8.- Changing the owner of the folders
8.- Changing the owner of the folders

Finally, you must change the folder permissions.

3. Create an index.html file for each host

It’s time to create a test file for each host. Run:

Add the following:

9.- Creating the index.html file for the virtual hosts
9.- Creating the index.html file for the virtual hosts

Now do it with the other one.

10.- Creating the second index.html for the other virtual host
10.- Creating the second index.html for the other virtual host

It’s done.

4. Create the configuration file for each host

Then, you must create the configuration files for each Virtual hosts (block servers). In the first place, copy the Block server file by default, it will serve as a guide. Type:

11.- Copying the default block server configuration file
11.- Copying the default block server configuration file

Now proceed to edit them. You need to change two things: server name and root directives.

12.- Editing first configuration file
12.- Editing first configuration file

Now do the same for the other file. However, add a # on both “listen” lines.

13.- Editing the second file configuration
13.- Editing the second file configuration

That’s it. Now activate the Server Blocks.

5. Enable Nginx server blocks

Now, you can enable the new newly created server blocks. To do this, first, disable the default one:

14.- Enabling the new server blocks
14.- Enabling the new server blocks

You need to reboot the nginx service. For this reason, type.

6. Test the new server block

Now it’s time to verify that everything is correct. Edit your /etc/hosts file and add your IP and virtual hosts.

15.- Editing the hosts files
15.- Editing the hosts files

Now go to your web browser and enter the address of the server block (virtual host) and test.

16.- Showing results for osradar2.test
16.- Showing results for osradar2.test
16.- Showing results for osradar1.test
16.- Showing results for osradar1.test

In the final analysis, I can say that nginx is a valid alternative to Apache. The way virtual hosts are created is very simple and within the reach of many.

We want to hear from you and your experience with nginx, tell us how it went?

Please spread this article through your social networks.

Spread the love

PS. If you like this post please share it with your friends on the social networks using the buttons above.Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here