Creating VirtualHost on your local computer to start simple index.php

When you start playing with PHP applications there is a moment when you would like to have your website to be run locally. You can build local domain for your application in some steps that I present you below.

You need to have installed xampp environment. Full instruction for xampp installation you can find in my previous post.

Hosts file configuration

Now we can start the first step. Under windows operating system you need to go to:

C:\Windows\System32\drivers\etc

Open file named hosts and type at the end of it the line with your local domain name, for example:

mydomain.com

Your web browser will start looking for the entered address from the list of entered domain names in the hosts file.

Creating virtual host

When we have our local domain configured we need to define how this domain will be connected with local directory.

I like to put all my web contents in one main directory like C:/Web. Let’s create this main directory and inside it create directory that will be consumed by mydomain.com address. Name this folder mydomain. You should have created such path in your system:

C:/Web/mydomain

In order to configure mapping between domain and folder we need to go to xampp configuration and to be more precise we need to configure apache serwer.

Go to the path like below and open file name httpd-vhosts.conf

C:\xampp-5-6-3\apache\conf\extra

In httpd-vhosts.conf file you need to uncomment the line with:

NameVirtualHost *:80

and add your configuration like below:

<VirtualHost *:80>

  ServerName mydomain.com

  DocumentRoot "C:\Web\mydomain"

  <Directory "C:\Web\mydomaink" >;
    AllowOverride All
    Require all granted
  </Directory>

</VirtualHost>

That is all. You have created connection between domain and local folder in your system.

Running your index.php file

The last step is to create simple index.php file in C:/Web/mydomain location with classic content:

<?php

echo "Hello world";

?>

If you didn’t make any mistakes you can run in your web browser the address:

http://mydomain.com/index.php

Leave a Reply