Recently I brought a new iMac for my home development, music, movies, games and general play things. For work I develop Web Applications and with my ISP I have a static IP address, so I decided to set up a small testing webserver. Since Apple has built in PHP and Apache2, this was pretty much a couple simple steps.
I used my post about Installing PHP5 and mySQL on Mac OSX Leopard to recompile PHP and mySQL, then I started setting up my Virtual Hosts.
Heres what I did from a fresh Install of Leopard;
Step One: Enable Apache Web Sharing
Open your System Preferences, then Select the Sharing option.
Enable the Web Sharing checkbox.
Step 2: Install PHP and mySQL
Download and install PHP 5 and mySQL 5 and install using my guide @ Installing PHP5 and mySQL on Mac OSX Leopard.
Step 3: Enable Virtual Hosts
Next you have to tell apache that we are going to use ‘Virtual Hosts’ and not just the default document server.
I am going to explain how to edit this in TextMate, but if you don’t use TextMate ( and why not? ) in a later post I can explain how to use Terminal and PICO to do the changes.
Open TextMate, and go to teh file menu, and select Open…
Check the Show Hidden Files checkbox and navigate to
and select the httpd.conf file.
Now if you press the Apple ( Command ) + L kets which will bring up the Go to Line dialog box. Go to line 465.
You will see the Virtual Hosts Include line.
Remove the # so that the file will now be included when Apache runs.
# Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf
Save and close the file. You will be asked for your password.
Step 4: Create your Virtual Hosts
Still in TextMate go to the File Menu and Select Open…. This time you want to navigate to the Virtual Hosts file.
The first line to edit in this file is to remove the :80 on the NameVirtualHost *:80.
For each site you you are going to be serving, you need to add a VirtualHost block.
ServerAdmin email@example.com DocumentRoot "/Library/WebServer/Documents/mysite.com" ServerName mysite.com ServerAlias www.mysite.com ErrorLog "/Library/WebServer/logs/mysite.com_error" CustomLog "/Library/WebServer/logs/mysite.com_access" common ServerAdmin firstname.lastname@example.org DocumentRoot "/Library/WebServer/Documents/mysite2.com" ServerName mysite2.com ServerAlias www.mysite2.com ErrorLog "/Library/WebServer/logs/mysite2.com_error" CustomLog "/Library/WebServer/logs/mysite2.com_access" common ServerAdmin email@example.com DocumentRoot "/Library/WebServer/Documents/mysite3.com" ServerName mysite3.com ServerAlias www.mysite3.com ErrorLog "/Library/WebServer/logs/mysite3.com_error" CustomLog "/Library/WebServer/logs/mysite3.com_access" common
Save this file, using your Admin Password.
Step 5: Restart Apache
Now we need to restart the Apache Web Server, you can do this the same as Step 1, disable Web Sharing then re-enable it.
Virtual Hosting Summary
Now you should be able to host more than one domain successfully on your Mac. Things to note are you must be pointing the A Record for your domain DNS Records to your IP Address, and you must point port 80 of your Router or internet gateway to your Mac Internal IP address, I would recommend. using a static Internal IP Address for this machine.