Using virtual hosts in Mac OSX

A guide to multiple-site local PHP-Nuke development using Apache, PHP and MySQL


1. Download and install Apache 2

I recommend using ServerLogistics "Complete Apache2" package, which includes both the Apache distribution and a Preference Pane in order to get apache control through the System Preferences. Just install it, stay away from the httpd.conf for now.

You can download it here: http://www.serverlogistics.com/apache2.php

2. Download and install PHP

I'll stick with ServerLogistics, and recommend their "Complete PHP" package. This is an extremely simple way of installing PHP, and it works like a charm!

You can download it here: http://www.serverlogistics.com/php4.php

3. Download and install MySQL

Yet again, I have no problems recommending ServerLogistics. This time, download the "Complete MySQL" package. This includes both the MySQL distribution, and a Preference Pane in order to get control over the server, including the option to set a root password for the database server, in the System Preferences. Install it, initialize the database, and set a root password for it.

You can download it here: http://www.serverlogistics.com/mysql.php

4. Adjusting the httpd.conf file

Ok, this is where it gets somewhat tricky. But keep your tongue straight, and pay attention...:) Remember; Wherever I write "$username", insert your Mac OSX username, in order for the code to work...

First off, set the document root:

#
DocumentRoot "/Users/$username/Sites"

Next, set the Directory variable to the same as the above:

#
<Directory "/Users/$username/Sites">

Then, tell apache to look for the index.php file:

#
DirectoryIndex index.php index.html index.html.var

Let's jump to the VirtualHost section, and start off by enabling it. Change this:

#
# Use name-based virtual hosting.
#
#NameVirtualHost *

...into this:

#
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1

Next, you'll add the different sites. I use "http://dev.*" to define local development-versions, where "http://www.*" points to the live version on the web.
Locate this:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

..and change it into this:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost 127.0.0.1>
ServerName 127.0.0.1
DocumentRoot /Users/$username/Sites
</VirtualHost>

<VirtualHost localhost>
ServerName localhost
DocumentRoot /Users/$username/Sites
</VirtualHost>

<VirtualHost dev.yoursite.com>
ServerName dev.yoursite.com
DocumentRoot /Users/$username/Sites/yoursite.com
</VirtualHost>

<VirtualHost dev.yoursite2.com>
ServerName dev.yoursite2.com
DocumentRoot /Users/$username/Sites/yoursite2.com
</VirtualHost>

And that should take care of the httpd.conf file.

5. Adding the files

Ok, next you'll add the actual files. Download your preferred version of PHP-Nuke, I used 6.9 when I developed this guide, but that is not important, as it'll work with any version.

Next, put the content of the htdocs-folder into /Users/$username/Sites/yoursite.com and another copy into /Users/$username/Sites/yoursite2.com

6. Adding the sql-data

Use your favorite SQL-application or command line and create two databases, for instance "site1nuke" and "site2nuke", I prefer CocoaMySql myself. Next, import the nuke.sql into both of them.

7. Adjusting the config.php file

In each site's config.php file, correct the dbname, dbuser and dbpass variables.

8. Setting the dev.* URLs to point the right way

Ok, last, but not least, we need to point the URLs to local resources, so that your computer won't conduct a DNS query in order to find it.

Open the file "/etc/hosts" in your favorite editor (I prefer command line "vi" or BBEdit). Remember that the file is hidden, so you'll need root access to edit it.

Locate and change this:

##
127.0.0.1    localhost
255.255.255.255    broadcasthost
::1    localhost

...into this:

##
127.0.0.1    localhost
127.0.0.1    dev.yoursite.com
127.0.0.1    dev.yoursite2.com
255.255.255.255    broadcasthost
::1    localhost

This makes your Mac think that dev.yoursite.com is located on your own computer, while still letting www.yoursite.com point to the live version on the web.

9. The finishing touch

Now, just restart the Apache server, using the "Graceful" button of ServerLogistics Apache2 Preference Pane, and you're home free. Next, test the configuration by entering "http://dev.yoursite.com" in Safari's address bar, and check out the brand new PHP-Nuke site begging for your developing touch!

--------------------------

This guide is ©2003 tersus web solutions
If you like it - great! If not, well, it's free...:)
Publishing it outside domains owned by the author requires this information to be included, and unchanged.

Thanks to ServerLogistics for publishing the installation packages for Apache, PHP and MySQL.
Thanks to Lorenz Textor for the CocoaMySQL software.
Big thanks to Francisco Burzi for PHP-Nuke. Keep up the good work!

Every trademark in this guide is © their respective owners.