How to Setup Multiple Virtual Hosts on XAMPP

Virtual host is one of the best ways to host multiple domains. Virtual host can be IP based or name based. IP based means that difference IP address of different websites and home based means running multiple names in each IP address.

In local PHP development environment like xampp/wampp, virtual host is also helpful because it reduces base url problems and it is better to hit local.wordpress then localhost/wordpress.

In this tutorial I am going to show you how to setup multiple hosts on xampp. Because as a PHP developer, We are usually running multiple projects at a same time.

If you don’t have xampp on your local machine or you want to install xampp on your machine. Please read my below 2 posts according to your OS.

Steps: Setup multiple virtual hosts on XAMPP

  • Create folder in xampp\htdocs folder. I will create test1 and test2 folder
  • Open httpd-vhosts.conf file under xampp\apache\conf\extra
  • Add virtual hosts for test1 and test2
  • Open hosts file under C:\Windows\System32\drivers\etc and add host for test1 and test2 and save hosts file as a administrator.
  • Restart Xampp

I have installed xampp on my E:\ directory on windows.


Create Folders in Htdocs:

Go to your htdocs folder and create 2 folders test1 and test2. Now create index.php file in both of the folder and write “This is virtual host 1” and “This is virtual host 2” in test1/index.php and test2/index.php respectively.

Open httpd-vhosts.conf:

Now go to xampp/apache/conf/extra and open httpd-vhosts.conf. It wil look something like below.

httpd vhosts conf

Go to the last line of the file and paste below code.

  • VirtualHost is used as a group of directives what will apply on a particular virtual host. In my case, I have 2 virtual host test1.local and test2.local.
  • DocumentRoot directive is used to declare directory route what will bind to server name.
  • ServerName directive set request url. ServerName is used to uniquely identify a virtual host.
  • AllowOverride All is used to set allow the use to .htaccess with in the current virtual host
  • Require is used for access control and current projects it is local.

Open hosts and add hosts:

Go to C:\Windows\System32\drivers\etc and open hosts file. Hosts file will look something like below.

hosts file

Now go the last line of the file and paste below code.

Save hosts file as an Administrator user. If you don’t have administrator rights then copy hosts file to the desktop add above lines and then again copy desktop hosts file to the original and replace to the previous file.

Restart XAMPP:

Restart apache and mysql services on xampp and to go browser and type test1.local in tab and press enter and in other tab type test2.local and press enter. You will see below page.

test1 local

test2 local

If you will face any problem implementing above. Please feel free to post your question in below comments.

Also Read:

 

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *