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.
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!
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.
Modifying the index.php fileWith 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:
localhost websiteNext, 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:
default httpd-vhosts.conf file configurationIn 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:
ServerAdmin [email protected]DocumentRoot "/sites/osradar1.com"ServerName osradar1.comServerAlias www.osradar1.com AllowOverride All Require all granted Options Indexes FollowSymLinksErrorLog "logs/osradar1.com-error.log"CustomLog "logs/osradar1.com-access.log" commonServerAdmin [email protected]DocumentRoot "/sites/osradar2.com"ServerName osradar2.comServerAlias www.osradar2.com AllowOverride All Require all granted Options Indexes FollowSymLinksErrorLog "logs/osradar2.com-error.log"CustomLog "logs/osradar2.com-access.log" commonSave the file to make the changes effective.
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 (#).
Default settingConfiguration to load the vhost module.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.
Default setting Configuration that allows Apache to host more than one module.This modification will allow Apache to be able to host more than one website. Press Save to continue.
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:
Creating folders for virtual sites.Then enter the folder osradar1.com and create a new file called index.php:
Creates the index.php fileNow 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:
Osradar 1
Welcome to Osradar 1 site
This procedure has to be followed in the folder osradar2.com:
Create the index.php file in the folder osradar2.comIn 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:
Osradar 2
Welcome to Osradar 2 site
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.
Run the services.msc command and restart the Apache server.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 driverscd etcdirnotepad hostsAccessing the host fileThe last command will launch the notepad and it is necessary to add the following lines to the hosts file:
127.0.0.1 osradar1.comwww.osradar1.com127.0.0.1 osradar2.comwww.osradar2.comHosts file finally edited.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.
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//andhttp://www.osradar2.com// And all three websites will be running simultaneously on the Apache server.
localhost in Apache webserverSample website osradar1.com running in the browser.Sample website osradar2.com running in the browser.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.
PREV: Defect #11685: Can't connect to MySQL server on 'localhost ...