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

Share me please

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:

[code lang=”javascript” tab_size=”2″ lines=”40″]
C:\Windows\System32\drivers\etc
[/code]

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

[code]
mydomain.com
[/code]

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:

[code]
C:/Web/mydomain
[/code]

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

[code]

C:\xampp-5-6-3\apache\conf\extra
[/code]

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

[code]
NameVirtualHost *:80
[/code]

and add your configuration like below:

[code escaped=”true”]

ServerName mydomain.com

DocumentRoot “C:\Web\mydomain”

<directory “c:\web\mydomaink” >
AllowOverride All
Require all granted

</directory>

[/code]

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:

[code lang=”php” ]


[/code]

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

[code]
http://mydomain.com/index.php
[/code]

Leave a Reply