Craig Francis


Apache Setup

The setup detailed below allows me to create a website on my development server, simply by creating a folder.

Ideally this setup would be used with a wild-card DNS server, to make it completely auto configuring... however, it is also possible to manually edit the '/etc/hosts' file to create the required domain names - one for each project.

Once the DNS is working correctly, just add this <VirtualHost> to the bottom of your '/etc/httpd/httpd.conf' file:

NameVirtualHost *
<VirtualHost *>

DocumentRoot /Library/WebServer/Projects/emma.homepage
UseCanonicalName Off
RewriteEngine on

#############################################################
# Main rewrite to select website, by ensuring that HTTP_HOST
# matches a valid address, taking the file path and adding to
# the HTTP_HOST (the [C] chains this rule to the next), Then
# taking this full path, to extract the project name and
# convert to a valid path on the system
#############################################################

RewriteCond %{HTTP_HOST} ^[a-z0-9]+(\.[a-z0-9]+)*\.[a-z]+\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([a-z0-9]+(\.[a-z0-9]+)*)\.[a-z]+\.domain\.com(.*) /Library/WebServer/Projects/$1$3

#############################################################
# If the request is not for a file in the /a/ folder, and
# the /a/inc/rewrite.php file exists, then re-write the
# request so that it uses this script.
#############################################################

RewriteCond %{REQUEST_FILENAME} !^/Library/WebServer/Projects/[^/]*/a/
RewriteCond %{REQUEST_FILENAME} ^(/Library/WebServer/Projects/[^/]*)/
RewriteCond %1/a/php/rewrite.php -f
RewriteRule ^(/Library/WebServer/Projects/[^/]*) $1/a/php/rewrite.php

</VirtualHost>

Remember to restart Apache after making this change.

Then you can create any projects you need in the folder:

/Library/WebServer/Projects/

And loaded them with the URL format:

http://folder.server.domain.com

This "Projects" folder should also contain "emma.homepage" for the generic homepage... this````` is used when a URL does not provide a project name.

Naming Conventions

To help with organisation, I name my projects using a 'client.project' format, for example:

/Library/WebServer/Projects/dunlop.corporate/
/Library/WebServer/Projects/dunlop.driversknow/
/Library/WebServer/Projects/hyundai.corporate/

So, as I have a BIND setup where by my laptops hostname is 'emma', I can access the websites using the URL's:

http://dunlop.corporate.emma.domain.com/
http://dunlop.driversknow.emma.domain.com/
http://hyundai.corporate.emma.domain.com/

Then, when I have built the website, and want to demonstrate it to the client before going live, I can move the projects onto a demo server, which uses the URL's:

http://dunlop.corporate.demo.domain.com/
http://dunlop.driversknow.demo.domain.com/
http://hyundai.corporate.demo.domain.com/

Switching between PHP4 and PHP5

Although I normally work in PHP5, there are times where I need to drop back to PHP4 - for example, if I need to test that my script can run on an old server, which has yet to be upgraded.

Its possible to perform this switching in a number of different ways. But I prefer to only have one version running at a time, with a good old Apache restart needed to switch between them - with it falling back to PHP5 on computer restart.

I am presuming that you have already compiled/installed a version of PHP5, and that it has been setup so that the PHP4 installation has not been effected.

First we need to update the /etc/httpd/httpd.conf. I'm using the basic one that Apple have supplied, but this configuration should work in most Apache installations.

We need to remove all of the current LoadModule, AddModule and AddType declarations relating to PHP, and add the following:

<IfDefine php4>
LoadModule php4_module libexec/httpd/libphp4.so
#AddModule mod_php4.c
</IfDefine>

<IfModule !mod_php4.c>
LoadModule php5_module libexec/httpd/libphp5.so
#AddModule mod_php5.c
</IfModule>

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

DirectoryIndex index.html index.php

In summary, if 'php4' has been specified as a startup parameter, then it will be used... otherwise, if the 'mod_php4' module has not been loaded, then load 'mod_php5'.

You may notice that the 'AddModule' lines have been commented out, this is because I have used this configuration after the 'ClearModuleList'.

Next, we need to update the /usr/sbin/apachectl script.

I should point out that editing the 'apachectrl' script might not be the best idea. Primarily because this script might be replaced when security updates are issued, and secondly because the $HTTPD variable (which we change below) is also used in the 'restart' procedure - so we should only be choosing to use PHP4 when calling 'apachectrl start'.

So, if you are happy with these potential issues... look for the line which starts 'HTTPD', and replace it with:

if [ -z $2 ]; then
HTTPD="/usr/sbin/httpd"
else
HTTPD="/usr/sbin/httpd -D $2"
fi

This simply looks to see if a second parameter is passed, and if so, adds it as a "-D" parameter to the startup script.

Next we need to stop the 'apachectrl' script from complaining when we pass a 'php4' parameter... you could re-write the script so that it does not loop though all of the parameters, but I want a quick solution, so look for the line which contains 'usage', and add the two lines, so that it looks like the following:

else
ERROR=8
fi
;;
php4)
;;
*)
echo "usage...

Now we can restart apache with either of the following:

sudo apachectl stop; sleep 1; sudo apachectl start
sudo apachectl stop; sleep 1; sudo apachectl start php4

It needs to stop and start, so that it can re-load the modules.

And because it defaults to PHP5, when we restart the computer, it will fall back to, what I consider, the 'safe' default.

Any feedback would be greatly appreciated, I don't include comments due to the admin time required, but if you email me, I will reply and make appropriate updates. Also, if you would like to take a copy of this article, please read the terms this article is released under. This article was originally written Sunday 5th August 2007 and was updated on Friday 14th September 2007.