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 createtest1
andtest2
folder - Open
httpd-vhosts.conf
file underxampp\apache\conf\extra
- Add virtual hosts for test1 and test2
- Open
hosts
file underC:\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.
Go to the last line of the file and paste below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<VirtualHost test1.local> DocumentRoot "E:/xampp/htdocs/test1" ServerName test1.local <Directory "E:/xampp/htdocs/test1"> AllowOverride All Require local </Directory> </VirtualHost> <VirtualHost test2.local> DocumentRoot "E:/xampp/htdocs/test2" ServerName test2.local <Directory "E:/xampp/htdocs/test1"> AllowOverride All Require local </Directory> </VirtualHost> |
- 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.
Now go the last line of the file and paste below code.
1 2 3 4 |
127.0.0.1 test1.local 127.0.0.1 test2.local |
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.
If you will face any problem implementing above. Please feel free to post your question in below comments.
Also Read:
- How to Add Custom Sidebar in WordPress
- How to install Composer on Windows with XAMPP
- How to add Google Autocomplete Address using Javascript