Hello, how are you? In a previous article, we saw how to install Apache in Windows 10. Consequently, we can use all the possibilities offered by this powerful web server. In this opportunity, I will show you how to enable Apache Virtual Host in Windows 10.
Enable Virtual Host in Apache
As mentioned above, we have learned how to install Apache. However, the configuration applied only allows you to set up a single website through the localhost address. On the negative side, This configuration does not offer enough flexibility of work, as it limits to a single website. This is inconvenient, as it is likely to require more than one website to be mounted on the server. Especially for those applications that do not need a dedicated server. Fortunately, Apache allows the configuration of multiple virtual hosts. On these hosts, it is also possible to interpret the PHP language. Let’s see how to do it!
Edit the index.php file
The first thing we need to do is to modify the index.php file located in the next path: C:\Apache24\htdocs. If you want to know how to do it, check our post about PHP in Windows 10.
With this modification what we will do is change the message of the site to show Default website. This is to work more comfortable and not to confuse with the two virtual sites that we are going to add. After saving the file, reload the browser and the page should look like this:
Edit the httpd-vhosts.conf file
Next, we are going to edit the virtual host configuration in Apache. With this intention, please go to this address C:\Apache24\conf\extra, once there select the httpd-vhosts.conf file and modify it with your favorite text editor:
In the shaded area that we saw in the previous image, we can see the information that we have to edit. In this opportunity, we are going to add two virtual hosts called osradar1.com and osradar2.com. Please note that you can assign the name of your preference, you just have to modify the file to your liking. In addition, we have also enabled port 80, but it is also possible to enable port 443. Thus, we’re going to replace that code with the following lines:
<VirtualHost osradar1.com:80> ServerAdmin email@example.com DocumentRoot "/sites/osradar1.com" ServerName osradar1.com ServerAlias www.osradar1.com <Directory "c:/sites/osradar1.com"> AllowOverride All Require all granted Options Indexes FollowSymLinks </Directory> ErrorLog "logs/osradar1.com-error.log" CustomLog "logs/osradar1.com-access.log" common </VirtualHost> <VirtualHost osradar2.com:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/sites/osradar2.com" ServerName osradar2.com ServerAlias www.osradar2.com <Directory "c:/sites/osradar2.com"> AllowOverride All Require all granted Options Indexes FollowSymLinks </Directory> ErrorLog "logs/osradar2.com-error.log" CustomLog "logs/osradar2.com-access.log" common </VirtualHost>
Save the file to make the changes effective.
Edit the httpd.conf file
Next, we are going to modify the Apache configuration to load the Virtual Host module. With this in mind go to the next address: C:\Apache24\conf. Then select the httpd.conf file and open it with the text editor. Look for a line with the text vhost, approximately line number 515. Then delete the number sign (#).
By removing the numeral sign, then the line is used and the extra configuration that has just been modified is downloaded. Save the file to continue. Without leaving the file modification, please locate line 187 corresponding to vhost_alias_module modules/mod_vhost_alias.so. In the same way that was done with the previous line, it is necessary to delete the numeral sign.
This modification will allow Apache to be able to host more than one website. Press Save to continue.
Create folders to host virtual websites.
As you may remember, in the previous steps we modified Apache to host two virtual sites called osradar1.com and osradar2.com. Well, it is necessary to manually create the folders where the necessary files will be stored so that the sites load correctly. First, go to the root of the C drive and create a folder named sites. Once inside it please create two new folders with the name of the website. In this case osradar1.com and osradar2.com:
Then enter the folder osradar1.com and create a new file called index.php:
Now it is necessary to add the following configuration to the newly created index.php file. Thereupon, opens the file with the text editor and copies the following code:
<html> <body> <h1>Osradar 1</h1> <p>Welcome to Osradar 1 site</p> </body> </html>
This procedure has to be followed in the folder osradar2.com:
In the same way, we have to modify the index.php file adding the following lines of code. Open the file with the text editor and paste the following text:
<html> <body> <h1>Osradar 2</h1> <p>Welcome to Osradar 2 site</p> </body> </html>
Save the files to continue. Please remember that every time we make a change in the Apache configuration, it is necessary to restart the service. This way the changes will be effective.
Edit the hosts file in Windows.
Finally, we are going to edit the host file in Windows 10. This will allow the system to load correctly the virtual hosts we have just created. In that case, opens a CMD as administrator and enters the following commands:
cd drivers cd etc dir notepad hosts
The last command will launch the notepad and it is necessary to add the following lines to the hosts file:
127.0.0.1 osradar1.com www.osradar1.com 127.0.0.1 osradar2.com www.osradar2.com
What we have done is added the IP address and the names of the websites we have just created. This way the system will be able to load them correctly.
Testing virtual hosts in the browser.
If you have followed to the letter the instructions I have given you, then it will already be possible to host more than one website on our Apache server. Remember that if you get any errors, then check if the files were edited correctly. To check if everything is OK, open the web browser and enter localhost. Open two more tabs and enter the name of the websites we created, in this case, http://www.osradar1.com// and http://www.osradar2.com// And all three websites will be running simultaneously on the Apache server.
Finally, we have seen how to enable Apache virtual host in Windows 10. This configuration will allow loading more than one website in the Apache server. Consequently, it will give us more flexibility to program websites. This is all for the time being, until the next time.