setup apache virtual hosts

How to Setup Apache Virtual Host in Windows

Virtual Hosts allow you to run multiple websites on a single server. It reduces server costs and space in case you want to host multiple domains in one place. In this article, we will look at how to setup Apache virtual host in Windows.


How to Setup Apache Virtual Host in Windows

Here are the steps to setup Apache virtual host in Windows. Let us say you want to host 2 domains – example1.com and example2.com on a single server.


1. Update hosts file

Open the system hosts file, generally found at c:\WINDOWS\system32\drivers\etc\hosts in a text editor.

You will find the following line in it

127.0.0.1 localhost

Add the following lines below it

127.0.0.1 example1.com
127.0.0.1 example2.com

You need to add 1 line for every domain that you want to host on your server. This will ensure that requests sent to these domains are routed to server on your machine.


2. Create folders for domains

Next, create separate folders, 1 for each domain in your Apache document root location C:/Apache24/htdocs. Each folder will host the files for that domain. For example, create C:/Apache24/htdocs/example1 and C:/Apache24/htdocs/example2 for example1.com and example2.com respectively.

Make sure the index.html or index.php file for example1.com is at C:/Apache24/htdocs/example1 and index.html or index.php file for example2.com is at C:/Apache24/htdocs/example2.

If you are using XAMPP then you need to create these folders at C:/xampp/htdocs instead.


3. Update Virtual Hosts in Apache Configuration

Open virtual host configuration file located at apache\conf\extra\httpd-vhosts.conf in a text editor.

Add the following VirtualHost tags 1 for each domain.

<VirtualHost *:80>
     DocumentRoot C:/Apache24/htdocs/example1
     ServerName example1.com
 </VirtualHost>
 <VirtualHost *:80>
     DocumentRoot C:/Apache24/htdocs/example2
     ServerName example2.com
 </VirtualHost>

If you are using XAMPP, then create your folders in C:/xampp/htdocs (or wherever your xampp installation is located) and update your virtual host configuration as shown.

<VirtualHost *:80>
     DocumentRoot C:/xampp/htdocs/example1
     ServerName example1.com
 </VirtualHost>
 <VirtualHost *:80>
     DocumentRoot C:/xampp/htdocs/example2
     ServerName example2.com
 </VirtualHost>


4. Check configuration file

Open Apache configuration file in XAMPP installation folder. Generally, it is located at C:/xampp/apache/conf/httpd.conf

Uncomment the following line in it by removing # at its beginning.

Include conf/extra/httpd-vhosts.conf

If you don’t find the above line you can add it afresh.

Restart XAMPP or Apache server to apply changes.

Open browser and go to http://example1.com you will see the index.html page of example.1com. Similarly, open http://example2.com and you will see the index.html page of example2.com.

Also read:

How to Escape Percent Character in Apache using .htaccess
How to Log POST data in NGINX
How to Read POST data in NodeJS
How to Setup Catch-all Subdomains in NGINX
How to Uninstall NodeJS, NPM in Ubuntu

Leave a Reply

Your email address will not be published.