22 C
Texas
angeloma
Senior Writer and partner

How to configure Nginx Server Blocks on Ubuntu 18.04?

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.
- Advertisement -

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:

:~$ sudo apt && sudo apt upgrade

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:

:~$ sudo apt install ngnix

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

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

:~$ sudo ufw app list

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.

:~$ sudo systemctl status nginx

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:

:~$ sudo mkdir -p /var/www/html/osradar1.test/public_html
:~$ sudo mkdir -p /var/www/html/osradar2.test/public_html

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:

:~$ sudo chown -R $USER:$USER /var/www/html/osradar1.test/public_html/
:~$ sudo chown -R $USER:$USER /var/www/html/osradar2.test/public_html/

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:

:~$ sudo nano /var/www/html/osradar1.test/public_html/index.html

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.

:~$ sudo nano /var/www/html/osradar2.test/public_html/index.html

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:

:~$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/osradar1.test.conf
:~$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/osradar2.test.conf

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.

:~$  sudo nano /etc/nginx/sites-available/osradar1.test.conf

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

:~$ sudo nano /etc/nginx/sites-available/osradar2.test.conf

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:

:~$ sudo rm /etc/nginx/sites-enabled/default
:~$ sudo ln -s /etc/nginx/sites-available/osradar1.test.conf /etc/nginx/sites-enabled/
:~$ sudo ln -s /etc/nginx/sites-available/osradar2.test.conf /etc/nginx/sites-enabled/

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

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

:~ sudo systemctl restart nginx

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.

:~$ sudo nano /etc/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.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article