Apache is one of the most popular web servers today; When searching Google for both paid and free hostings, we will see that they all use Apache as their web server.
As you well know, Apache is based on modules or components that are independent and we can configure them independently. One of these configurations is the VirtualHost that allows assigning different websites through domains to the same IP address.
In this entry we will see what VirtualHosts are and how to use them in our web applications with PHP.
VirtualHosts are a very useful configuration when developing our applications since they allow easy access to a PHP application hosted on an Apache web server through a simple domain that we configure on our machine or server.
Configuring the VirtualHost
In Linux, specifically Fedora, we must enter the path:
sudo gedit /etc/httpd/conf/httpd.conf
From our folder where Apache is hosted and after all the comments we can enter something like this:
<VirtualHost desarrollolibre2.net>
ServerName desarrollolibre2.net
DocumentRoot /var/www/html/desarrollolibre
</VirtualHost>
The first line <VirtualHost desarrollolibre2.net> simply indicates the IP (through the domain) of the virtual host and the default port is 80; We could also use a configuration like the following:
<VirtualHost *:80>
ServerName desarrollolibre2.net
DocumentRoot /var/www/html/desarrollolibre
</VirtualHost>
The ServerName desarrollolibre2.net establishes the base or root of the domain and with this address it is the one that we will use in the browser to access our application.
The DocumentRoot /var/www/html/libredevelopment sets the location to the directory which in other words means the site where our domain configured with the VirtualHost points to.
This would be the minimum we need; There are other configuration directives such as ServerAdmin to specify the administrator's email, the alias with ServerAlias, etc.
We restart the Apache services with the commands:
sudo service httpd stop sudo service httpd start
Or from the Xampp, Wampp or any other interface if you are in Windows:
If we try to enter the previous domain we will see a screen like the following
What about our VirtualHost?
The hosts file has several utilities such as establishing the IP of the site to avoid going through the DNS, blocking sites, etc. In our case, we are interested in indicating that the domain in question will be resolved by our own machine; For this we must go to the hosts file of our machine, in Linux it will be in:
sudo gedit /etc/hosts
In the case of Windows it will be in:
C:WINDOWS\system32\drivers\etc\hosts
We add the previous domain:
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 127.0.0.1 desarrollolibre2.net
Now, if we try to navigate again, we will see a screen like the following, which corresponds to the DesarrolloLibre website that I host locally:
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter