Snow Leopard for web developer

My FileVault image got corrupted and I lost all my settings and few documents (most of them were backed up on server). Because I had to create my configures again I was thinking if it’s time to upgrade to Snow Leopard. With fresh install it is good time for rethinking your own workflow and setups. This was what I did after OS install.

First of all Apple has made great work with this new version of OS X. They have also remembered web developers because Snow Leopard has quite fresh versions of Apache, PHP and even Subversion. OS X comes also with BIND DNS server. Before image-file corruption I had over 100 different web development projects installed on laptop. That means I needed easy way to create and install new projects. This is where properly configured DNS server and dynamic virtual hosts comes handy. In this tutorial we do required steps to achieve mature and easy to use web development environment with dynamic virtual host support.

First step is to enable PHP support and virtual hosts from Apache configuration. Open file /etc/apache2/httpd.conf with your favorite editor. Search for word ‘php’. You should find following line.

#LoadModule php5_module        libexec/apache2/libphp5.so

Remove ‘#’ start from the line which uncomments that line and enables PHP support. Apache restart is needed before this change will take effect, but before restart we should enable virtual host support too. Find for word vhost. You should find following line.

#Include /private/etc/apache2/extra/httpd-vhosts.conf

Again remove ‘#’ to load vhosts related configuration. Now you can save and close this file. Next we should edit /etc/apache2/extra/httpd-vhosts.conf file to create configuration for dynamic virtual hosting. Because I mainly use symfony web framework my project folders has lot of files which should not be located under web server’s document root and only web folder under project should be accessible from web server. I store all projects under Sites folder which is located on my home folder. This way my project’s document root should look like /Users/tero/Sites/project_name/web. Following configuration will follow this layout. Open httpd-vhost.conf with your favorite editor and replace its content with following configuration (of course replace my username with yours).

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName default
    ServerAlias *

    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    UseCanonicalName Off
    VirtualDocumentRoot /Users/tero/Sites/%-2+/web/
</VirtualHost>

First line says we want to use name based virtual hosts configuration for all IP-addresses on port 80

In VirtualHost-section we match for every domain name with ServerName and ServerAlias definitions. Most important line after ServerName and ServerAlias is VirtualDocumentRoot. As I mentioned earlier my web roots will look like /Users/tero/Sites/project_name/web. I wan’t access to project with url http://project_name.local. In VirtualDocumentRoot-configuration ‘%-2+’ means the penultimate and all preceding parts from requested domain. If you wan to know more about dynamic virtual hosts and how to configure them I recommend to read mod_vhost_alias documentation. Save and close this file.

Now it is time to start (or restart) Apache. This can be done from command line or more “macintosh way” from Sharing preference pane (System Preferences -> Sharing). If you have Apache already running Web sharing checkbox should be checked. If this is your case first disable web sharing and after that enable it again. If web sharing wasn’t enabled now is time to start it and we have Apache running with PHP support and dynamic virtual hosts configured. We have still few steps to do before we are done.

Next we will install MySQL. This is really simple step because you can download pre-builded packages from http://dev.mysql.com/downloads/. You should choose x86_64 version for Leopard (in time of writing there wasn’t own build for Snow Leopard). Download package and install MySQL from .pkg file. I also recommend to install MySQL.prefpane. This way you get own preference pane for MySQL on System Preferences. Start MySQL from this preference pane.

Last step is to configure and enable BIND. This step is done mostly from terminal and many commands need root privileges. So lets open Terminal.app and run following command to switch as root

sudo -s

Generate new secret key and configuration with rndc which is DNS server’s configuration utility:

rndc-confgen -b 256 > /etc/rndc.conf
head -n5 /etc/rndc.conf |tail -n4 > /etc/rndc.key

First command generate configuration and second gets secret key from configuration and saves it to /etc/rndc.key file. The rndc-confeg is nice utility but somehow it uses different port than is configured on named.conf. Open /etc/named.conf with your favorite editor and search for ‘port’. You should find following line.

inet 127.0.0.1 port 54 allow {any;}

As you see named was configured to use port 54. Next open few steps ago generated /etc/rndc.conf and search for ‘port’. You should find following line.

default-port 953;

If it differs from named.conf change it for equal (I changed to 54) and we can start BIND.

/usr/sbin/named

I also recommend to add named on launchd configuration to get DNS server up and running on system startup. For this run following commands:

launchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist
echo "launchctl start org.isc.named" >> /etc/launchd.conf

Next we will configure our local domain which is done with zone file. I won’t cover too deeply how these zone files are defined. I only show simple example file which will work for our purpose.

$TTL    86400
$ORIGIN local.
@                       1D IN SOA       @ root (
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum
                        1D IN NS        @
                        1D IN A         127.0.0.1
* IN A 127.0.0.1

Only interesting lines for us are second line which defines domain (local) and last line which defines DNS record. We have only one A-record which is asterisk and match for all subdomains. These requests will be routed to IP address 127.0.0.1 which is loopback address and points to your own computer. You can use named-checkzone to check your zone files for typos:

named-checkzone local /var/named/local.zone

If you got OK response we can continue with updating /etc/named.conf file to tell BIND about our new zone. Insert following lines after the existing zone configurations.

zone "local" IN {
        type master;
        file "local.zone";
        allow-update { none; };
};

Now we ask BIND to load configuration and zone files to get our own domain to work:

rndc reload

Now we have DNS running and our own domain configured but we have still one step to do before we can test if our setup is working. We have to take our own DNS in use. Open Network Preferences and from there select Network. From this pane click Advanced… and choose DNS tab. Add your own DNS server here (127.0.0.1).

Now you can test how our setup is working. Checkout some project on Sites folder or create some test folder (Sites/test/web) and simple php file under that folder. Go with your browser to URL http://project_name.local (test.local) and if you see your page as expected, everything went fine and now you have setup where you can start new projects as easy as creating new folders under Sites folder. No anymore virtual host configuration and hosts file manipulation.

Add post to: Delicious Reddit Slashdot Digg Technorati Google
(already: 3) Comment post

Comments

avatar

Oh, it looks great, it looks a complete step-by-step instructions. Thank you for this info! My doubt now is: should I buy an Mac Mini Server to host websites, or the Leopard can handle many websites? Also, a Macbook Pro or an iMac will not spend to much electricity? Thank you in advance for any hints…

avatar

Thank you for your comment. I’m sorry, but I can’t give any advices about hardware. I don’t actually care much about hardware. I only want that it should work ;)

avatar

Opps, not working for me. I don’t know what to do since the hint:

/usr/bin/named

I cound’t find this

avatar

I’m sorry. The path is missing one s it should be following:

/usr/sbin/named

I will fix this to original post too.

avatar

Great article thanks for sharing

Comment post

Login

login