VirtualHost allows you to assign different websites across domains to the same IP address. They allow easy access to a PHP application hosted on an Apache web server through a simple domain. For this reason, is very used for web developers and system administrators. In this article, I will show you how to configure an Apache virtual host on Debian 9.
0.What you need
Before you start, you must have certain requirements for everything to work perfectly.
- Have, at least, a notion of how to use the command line interface.
- A user who can use sudo.
- A desire to learn. Vital to acquire the knowledge that I will give you.
- A computer with Debian 9 installed. Obvious, isn’t it?
1. Install Apache web server
Generally, before you start installing and configuring a system, it is a good idea to perform a general system upgrade. With this, you will obtain the last security patches.
Open a terminal and run on it:
:~$ sudo apt update && sudo apt upgrade
After you enter your password, the update process will begin. As a result, you will have an updated and secure system.
Now, you can install apache2 from the official Ubuntu repositories. Run:
:~$ sudo apt install apache2
As a result, you can now go to your browser and set the IP address of the server and you will see a page similar to this one. Open it.
http://IP_server/
Good job. Apache is running smoothly on your computer.
2.-Â Create web directory for each Apache virtual host
In order to create a structure of efficient directories and that the Virtual hosts do not have problems, you must create a directory for each one of them. In this case, I will only create two virtual hosts. One called “osradar1.local” and other called “osradar2.local”.
Create the folders:
:~$ sudo mkdir -p /var/www/html/osradar1.local/public_html :~$ sudo mkdir -p /var/www/html/osradar1.local/public_html
With this in mind proceeds to change the owner of them. Run,
:~$ sudo chown -R $USER:$USER /var/www/html/osradar1.local/public_html :~$ sudo chown -R $USER:$USER /var/www/html/osradar2.local/public_html
Finally, change the permissions on the folders.
:~$ sudo chmod -R 755 /var/www/html/
Now, create an example index.html file, to check the operation.
:~$ sudo nano /var/www/html/osradar1.local/public_html/index.html :~$ sudo nano /var/www/html/osradar2.local/public_html/index.html
For the first hosts (osradar1.local) in index.html, type this:
<html> <header> <title> osradar1.local website </title> </header> <body> This message is from osradar1.local website </body> </html>
And for the second host:
<html> <header> <title> osradar2.local website </title> </header> <body> This message is from osradar2.local website </body> </html>
3.- Create the configuration file for each host
In this step, you must create the configuration files for each virtual host, but it’s not complicated because you can rely on the default configuration file.
In the first place, copy the default configuration file, and rename it for each virtual host.
:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/osradar1.local.conf :~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/osradar2.local.conf
Now, you must edit the files to adapt them to each virtual. For example, edit osradar1.local.conf
.
:~$ sudo nano /etc/apache2/sites-available/osradar1.local.conf
In that file, you must modify ServerName, ServerAlias, ServerAdmin, and DocumentRoot.
Do the same for the other virtual host.
4. Enabling configuration files
Once the configuration files are ready, you must activate them. First, disable the default configuration file and then activate the newly created virtual hosts.
:~$ sudo a2dissite 000-default.conf :~$ sudo a2ensite osradar1.local.conf :~$ sudo a2ensite osradar2.local.conf
And it’s ready. Now let’s check that everything was successful. Restart apache2.
:~$ sudo systemctl restart apache2
5.- Test the virtual hosts
In the first place, edit the file /etc/hosts
and add your IP with the names of the virtual hosts.
:~$ sudo nano /etc/hosts
Now, go to your web browser and enter your virtual host.
And this is it. As you can see it’s really simple.
Tell us, how was your experience with Apache?
Please spread this article through your social networks.