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.
In order to perform this tutorial satisfactorily, you should keep this in mind:
- You need to be logged in with a user with
- 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:
:~$ sudo apt && sudo apt upgrade
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
Next, verify that the firewall is configured to allow nginx to operate.
:~$ sudo ufw app list
As a result, you can see the applications that are in profile in the firewall. If you see
Nginx HTTP and
Nginx HTTPS, you are doing well.
Check the status of the service.
:~$ sudo systemctl status nginx
Go to your browser and access with the IP address of the server.
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
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/
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:
Now do it with the other one.
:~$ sudo nano /var/www/html/osradar2.test/public_html/index.html
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
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
Now do the same for the other file. However, add a # on both “listen” lines.
:~$ 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/
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
Now go to your web browser and enter the address of the server block (virtual host) and 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.