Apache VirtualHosts – HowTo

IMAG0652

I used to do this whenever I make a new PHP project. But I forget the steps often. This post will explain you how to add a new virtual host in apache.

1. Apache server installed (I’ve used XAMPP ) with php plugin enabled

2. Make a new folder in your computer. Ensure you have an index.html or index.php on that folder. I’ve my folder on C:\xampp\htdocs\market

3. Make the following steps on C:\xampp\apache\conf\extra\httpd-vhosts.CONF file. This will be the file to define your virtual hosts

Add the following lines at the end of this file.

#this is to access the standard xampp home page
NameVirtualHost localhost:80
<VirtualHost localhost>
ServerAdmin pandian@grassfield.org
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
#ServerAlias http://www.dummy-host.localhost
#ErrorLog "logs/dummy-host.localhost-error.log"
#CustomLog "logs/dummy-host.localhost-access.log" combined
</VirtualHost>

#this is our virtual host grassfield.dev
NameVirtualHost grassfield.dev:80
<VirtualHost grassfield.dev>
ServerAdmin pandian@grassfield.org
DocumentRoot "C:\xampp\htdocs\market"
ServerName grassfield.dev
#ServerAlias http://www.grassfield.dev
#ErrorLog "logs/grassfield.dev-error.log"
#CustomLog "logs/grassfield.dev-access.log" combined
</VirtualHost>

4. Restart the apache service

5. Access http://localhost and http://grassfield.dev sites are rendered correctly on the browser. Sample file is attached in this post.

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/&gt;
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
NameVirtualHost localhost:80
<VirtualHost localhost>
ServerAdmin pandian@grassfield.org
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
#ServerAlias http://www.dummy-host.localhost
#ErrorLog "logs/dummy-host.localhost-error.log"
#CustomLog "logs/dummy-host.localhost-access.log" combined
</VirtualHost>
##<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost"
##ServerName dummy-host2.localhost
##ServerAlias http://www.dummy-host2.localhost
##ErrorLog "logs/dummy-host2.localhost-error.log"
##CustomLog "logs/dummy-host2.localhost-access.log" combined
##</Virtual
NameVirtualHost grassfield.dev:80
<VirtualHost grassfield.dev>
ServerAdmin pandian@grassfield.org
DocumentRoot "C:\xampp\htdocs\market"
ServerName grassfield.dev
#ServerAlias http://www.grassfield.dev
#ErrorLog "logs/grassfield.dev-error.log"
#CustomLog "logs/grassfield.dev-access.log" combined
</VirtualHost>

The picture at the top don’t have any relations with this post 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s