Core PHP
Apache 2, PHP 4 & PHP 5 on Windows XP
Core PHP
Blog: CorePHP Blog     Current Projects: PHP on XP Guide  —  NFO Viewer  —  Easy Reflections  —  HotWire  —  FileGlider
< Previous Index Next >

Step 14 - Adding another web site (the detailed version)

You may be wondering why I got you to use the Apache Virtual Hosts conf, and to mess around with the Windows hosts file? The reason is that with this combination you can run as many local sites on your PC as you want.

As a developer I am often working on several projects at once, and over the years I have built-up a massive collection of sites I've worked on. Using this technique I can have all of those sites running on my server, all of the time. If I want to test something out then I can quickly create a brand new virtual host and start working on it in less than a minute. This is ideal for creating local 'playgrounds' (sandboxes) for new PHP apps or frameworks I may wish to test. Or of course for that new project your client has bought you.

With that said let's add a new site to our development environment:

1) Go to C:\www where our local web sites live, and create a new site folder. A naming convention I like to use is to take the domain name of the live site, and then name the local folder after it. So for this example I'll create a folder called reflection.corephp.co.uk. Inside this folder create two sub-folders: public_html and www_logs.

( as this site has already been built I will copy over the existing html / php / graphics files into the public_html folder. Obviously you can't do this step, so instead copy over some html files you may already have on your hard drive, or use the index.php from the sandbox.dev site. )

2) Now we need to edit the Apache Virtual hosts file to tell it about our new site. Open up C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf into Notepad.

3) As we've already got an entry in the httpd-vhosts.conf file for sandbox.dev we can simply copy and paste the whole block. Then all we need to do is go through it, changing the paths to reflect the new folder we created in Step 1. The end result will look like this:

<VirtualHost *:80>
    DocumentRoot C:/www/reflection.corephp.co.uk/public_html
    ServerName reflection.corephp.dev
    ServerAlias www.reflection.corephp.dev
    ErrorLog C:/www/reflection.corephp.co.uk/www_logs/error_log.txt
    CustomLog C:/www/reflection.corephp.co.uk/www_logs/access_log.txt common
    php_value error_log "C:/www/reflection.corephp.co.uk/php_error_log.txt"
    <Directory "C:/www/reflection.corephp.co.uk/public_html">
   	AllowOverride All
   	Options Indexes FollowSymLinks
   	Order allow,deny
   	Allow from all
    </Directory>
</VirtualHost>

The new conf file will now look like this:

4) Save the file.

5) With that done the final thing we need to do is add the two new development domains to our Windows Hosts file. Using either the Host Administrator utility, or the manual method outlined in Step 11, add in two new domain names:

127.0.0.1	reflection.corephp.dev
127.0.0.1	www.reflection.corephp.dev

6) Save the hosts file.

7) Open the Apache Service Monitor from the system tray and click Stop.

8) Wait for the stopped message and then click Start

This will stop and restart the Apache service. Sadly using the Restart button will not be enough to pick up the change to our Virtual Hosts file, so you have to use this method instead.

9) If you now go to http://reflection.corephp.dev you'll see the site that was set-up there. In this example I've put my Easy Reflections web site into that folder, so I can work on it locally, so I get to see this:

(obviously you will see whatever files you placed into the public_html folder, and not the above!)

Friendly Naming Scheme

I'm a big fan of using the fake domain extension .dev for all of my local development sites.

For example the domain name of this guide is wamp.corephp.co.uk, so my local version is called wamp.corephp.dev.

This technique will work just fine, unless they ever invent a .dev extension :)

I can use PHP code within my scripts to check what the domain is, if the domain ends in .dev then my scripts can tell they are running on a local test server, and to use local paths and settings. This allows for automatic detection between dev and live in your code, meaning you don't have to modify anything before uploading.

Because we've got a fully working Apache environment, complete with ModRewrite, it is possible in most cases to build a complete mirror of the live site on your local PC.

You can repeat the above steps to create as many local test sites as you need. Once you've got used to doing this a couple of times you'll be able to churn them out as quickly as you require them.

< Previous Index Next >

Need help? Post in the WAMP Guide Forum!

© Copyright 2007 - 2008 Richard Davey