The virtual host provides a domain URL in the local system. It allows you to run your application on a domain virtually. You can configure Apache virtual host easily for your application. If you create the Apache virtual host for your PHP application then it will be super easy for you. You can run any PHP framework application through that virtual host. Once, you created the virtual host, it won’t require running your application again and again. This is the most beneficial thing to have the virtual host. Only, you will need to start the Apache server and your application will up automatically by typing the host URL in the browser. In this post, I will show you to create the Apache virtual host through XAMPP in Windows. I won’t take much time on describing this kind of stuff. Hence, Let’s come to the practical things.
Before creating the virtual host, it is mandatory to have the below applications in your windows system.
- Windows OS
If you install XAMPP then it will provide you the Apache server and PHP inbuilt. So, after that, you won’t require an additional PHP development environment.
If you don’t have the XAMPP then it is highly recommended to have it in your system.
Create a PHP Project Folder
At the initial step, you will need to have the PHP application folder. Hence, I am creating a new folder inside the xampp/htdocs folder. Here, my folder name is php-app. Inside the folder, I am creating a PHP file named index.php. Also, I have added a simple message inside this file.
<?php echo "PHP application running through Virtual HOST"; ?>
Create Apache Virtual Host in Windows
You will need to open the XAMPP Control Panel. Please make sure, Apache server is stopped before configuring the Virtual host.
In the XAMPP Control Panel, there is the Explorer option. When you will click on that, it will redirect you to the Xampp installed directory.
Inside the xampp directory, you will have the apache folder as shown below.
Now, inside the apache folder, you will see the folder named extra. So, go inside that directory.
Inside the extra folder, you have to go inside the conf folder. This is basically a configuration folder of apache.
Inside the configuration folder, you will see the httpd-vhosts file. This is mainly virtual hosts file. So, you will need to configure the new virtual host inside this file.
Open this file in Notepad or Notepad++ as an administrator mode. Now, scroll down, and look for the highlighted lines.
Configure Apache Virtual Host
You need to make a copy of the existing VirtualHost configuration, it will look like it as shown below.
##<VirtualHost *:80> ##ServerAdmin firstname.lastname@example.org ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com" ##ServerName dummy-host2.example.com ##ErrorLog "logs/dummy-host2.example.com-error.log" ##CustomLog "logs/dummy-host2.example.com-access.log" common ##</VirtualHost>
- Firstly, you have to uncomment the snippet by removing (#) from each line.
- Now, just keep the DocumentRoot and ServerName.
- In the DocumentRoot, you need to pass the path of your PHP application.
- Now, the last line is the ServerName. So, this will contain the URL of your virtual host on which your PHP application run.
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/php-app/" ServerName myphp-app.com </VirtualHost>
I have set the virtual host URL to
myphp-app.com. Now, save the file and close it.
In the next step, you need to register this virtual domain in the localhost.
Configure Apache Virtual Host IP Address
We have the virtual host URL. But, now, this will need an IP address of the localhost. Hence, in order to register the virtual host domain, you will need to go to this path –
Inside this path, you will see the file named hosts.
Now, you will need to open this hosts file in the editor. So, better, you open it in Notepad++ as an administrator mode. Take a look at the screenshot, here you have to copy the highlighted line and paste it below the end of the commented lines.
Now, uncomment the host IP address. It should look like this.
Here, you need to register your virtual domain. In my case my virtual domain is myphp-app.com. Hence, I am registering this with the same IP address as we have for the local host.
Save the file and close it. That’s it for the configuration.
Restart Apache Server
Once you created Apache virtual host, it’s time to restart the Apache server inside the XAMPP control panel.
Once the Apache server is started, you will be able to access the virtual host URL in the browser. Here, you can see our PHP file is running through the virtual host.
We configured Apache virtual host in XAMPP for the Windows platform. We registered the virtual host domain with the IP address. This IP address allowed our domain to run locally in the browser. We accessed a basic PHP file in the virtual host. Similarly, you can run any PHP application whether it is any framework like Laravel, Yii, CodeIgniter, etc. I hope this will be helpful for you. Feel free to ask if you will have any doubt regarding this post.