Tuesday, July 5, 2011

Create virtual hosts in PHP with apache

Virtual hosts are mainly used to run different applications with in a single Apache server by creating different port numbers in Apache configuration.

The default port number that Apache will run is 80.

Now we will see the steps to create virtual hosts in both Windows and Linux.
Both the cases assume that we have installed wamp on D:/wamp
Creating virtual hosts Windows box

1)Go to the directory: D:\wamp\bin\apache\Apache2.2.17\conf
2) There you will find apache.conf file, open it in any text editor.
3) In that file you will be having 2 lines
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Now you need to include the virtual hosts file as like below.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4) Now Open file conf/extra/httpd-vhosts.conf in any text editor.
In that file you will be having lines like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

So name your virtual host below these line like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:8001

5) Then you need to set up directory of your new site. By default for virtual host 80 the following code will work, the code block follows below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


Then we need to copy and paste the same block and make changes like below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/wamp/www/"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/mydomain.com-error.log"
CustomLog "logs/mydomain.com-access.log" common


This process will finishes the creating of your first virtual host.

6) Restart your Apache server and try it in your browser like http://localhost:8001/

Creating virtual hosts Linux box

No comments:

Post a Comment