A Web Development Environment on Snow Leopard

This is an update to an earlier post I submitted about how to set up a web dev environment on Leopard.  A few things have changed from Leopard to Snow Leopard (namely switching to 64 bit code) so it makes sense to refresh this guide.

* * * * *

If you’re like me and you’d tried to have your Leopard PHP-MYSQL build just work on Snow Leopard, I’m sure you met with the same rude awakening that I did.

It didn’t work.

If you followed my guide for installing LAMP on Leopard, you should start by cleaning out MacPorts and starting fresh.

To be certain, you should honestly clean out everything.  But before you do, be sure to list out all the ports you currently have installed.  Do that like this:

sudo port installed

And copy the list of installed ports to a text file. For reference.

Next let’s do some housecleaning (BE CAREFUL! DON’T UNINSTALL UNLESS YOU MEAN IT!):

sudo port clean --all vile
sudo port -f uninstall installed

Then you can go about re-installing your environment. The guide below is slightly modified to be able to work on Snow Leopard.  Enjoy!

* * * * *

I’ve found a relatively easy way to maintain a flexible, updatable development under MacOS 10.6 which goes so far as to install my own components, but not to have to compile them (which I’ve done several times and can attest, is a pain).

This setup gives you a great web development environment under Leopard (geared in my case towards Flash development compatible with the WordPress platform), which does a better job of playing by the rules than the default install of a lot of these elements.  And with MacPorts, the whole thing is modular and very easily maintained.

And everything on the list is free and/or open-source.

* Note that you’ll need to have administrator privileges on your user account to do this.

** Also note that this is geared towards creating a relatively unfettered development environment.  I don’t make any considerations about server security or server optimization for large-scale web applications.  This is really geared towards creating a simple, local dev environment.

Includes:

  • XCode Tools
  • MacPorts
  • Apache2
  • PHP 5
  • MySQL 5

* optional installs:

  • GIT/Subversion
  • GitX/SCPlugin
  • Sequel Pro (formerly CocoaMySQL)

derived from a bunch of sources, including:

php-5.2.5 on Leopard

Using Macport to Setup PHP5 and Apache2 on Leopard/

Installing MYSQL on Mac OS X Leopard Using Macports

Here goes:

1. Install XCode Tools:

http://developer.apple.com/technology/xcode.html

pretty self-explanatory. Install them.  You’ll need them at the very least for MacPorts.  But they come in handy if you ever need to compile anything.

2. Install MacPorts

http://www.macports.org/install.php

MacPorts is a modular install system with easy update / swap / uninstall capacity.  It’s like Fink (I say, never having used Fink) — just an easy way to install a bunch of standard programs.

There are several install options, but the easiest thing to do is just use the disk image here:

http://distfiles.macports.org/MacPorts/MacPorts-1.9.2-10.6-SnowLeopard.dmg

Unless you’re an advanced user I recommend you do that.

3. Update MacPorts

sudo port -v selfupdate #makes sure ports is up to date
sudo port sync #syncs the local port index with the remote index

4. Install Apache 2

sudo port install apache2 +universal
sudo port load apache2 # set to autorun at boot

The standard web server. Standard, because it’s really stable, fast and flexible.

more info: http://blog.rajatpandit.com/2009/03/12/using-macport-to-setup-php5-apache2-on-leopard/

** notes **

You don’t necessarily need these, but I find it’s helpful to have some aliases to address apache — mostly for start, stop and restart functionality.  You can set these aliases to be whatever you want.  Or ignore them.  I just find them helpful.

alias apache_start="sudo /opt/local/apache2/bin/apachectl start"

alias apache_restart="sudo /opt/local/apache2/bin/apachectl restart"

alias apache_stop="sudo /opt/local/apache2/bin/apachectl stop"

5. Install MySQL 5

(Note that you might need to re-link your MySQL sock file before you initialize and setup if this is your first time using MySQL on your system.)

sudo port install mysql5-server +universal

sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql #initialze mysql

sudo port load mysql5-server #set mysql to autostart at boot

sudo /opt/local/bin/mysqld_safe5 #start

#create symbolic links to not have to type in mysql5 every time:

sudo ln -s /opt/local/bin/mysql5 /opt/local/bin/mysql

sudo ln -s /opt/local/bin/mysqladmin5 /opt/local/bin/mysqladmin

More info: http://matthewcarriere.com/2008/04/02/installing-mysql-on-mac-os-x-leopard-using-macports/

6. Install php5 (with apache2, mysql and pear support)

sudo port install php5 +apache2 +mysql5 +pear +universal

7. Activate PHP

sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

cd /opt/local/apache2/modules

sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

8. Move Snow Leopard’s standard Apache out of the regular path

sudo mv /usr/sbin/apachectl /usr/sbin/apachectl-leopard

9. Move your new Apache into the path

(you might not need the first 2 steps, but I’m working on a totally clean Leopard build, so I don’t have these directories yet)

sudo mkdir /usr/local

sudo mkdir /usr/local/bin

sudo ln -s /opt/local/apache2/bin/apachectl /usr/local/bin/apachectl

10. Move Leopard’s bundled PHP out of the way

sudo mv /usr/bin/php /usr/bin/php-leopard

11. Do some Apache configuration for PHP:

(all of these are modifications to the httpd.conf file located here: /opt/local/apache2/conf/httpd.conf)

a. Add index.php to the list of DirectoryIndex:

<IfModule dir_module>

DirectoryIndex index.html index.php

</IfModule>

b. Add handlers for PHP files:

# Add handling for PHP files

<IfModule php5_module>

AddHandler application/x-httpd-php .php

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

</IfModule>

This block can go anywhere in the httpd.conf — but it’s probably best to keep the module handlers in alphabetical order.

c. Restart Apache

If you added the alias to your bash profile, you can now do apache_restart. Otherwise, enter:

sudo apachectl restart

11. Include user Sites directories (enable the following file by un-commenting it):

# User home directories

Include conf/extra/httpd-userdir.conf

12. Re-link mysql.sock:

sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

This step is essential so everyone can agree where MySQL is living on the system and how to connect to it.  Before I did this, Apache and PHP seemed to be able to access MySQL, and I could access MySQL from the command line, but programs like SequelPro couldn’t find the MySQL socket.  This step seems to correct that problem and let everyone play nicely.

** OPTIONAL **

These steps are optional — certainly not essential, but I find them to be very helpful tools for web development.

13. Install Git/Subversion (Version Control)

I personally have become a huge fan of Git.  I want to encourage you to become a fan too if you aren’t already.

You can install Git using Mac OSX installer.

Alternatively, if you want to keep with Subversion (or, say, you need both), do this:

sudo port install subversion +universal

This installs the latest version of Subversion on the system — which is then used for SVN access from the command line, from Eclipse, from SCPlugin, etc.

14. Install GitX/SCPlugin

You’ll now want a way to interface with your version control system of choice.  My favorite interface for Git so far is GitX.  It’s much more for tracking revisions and simple commits than for branches and complex merges.  For those you’ll need the command line.  But for simple tracking and commits (what you do from day to day), GitX is for you.  Download GitX here.

If, instead/in addition, you’re looking for a tool to interact with Subversion, I recommend SCPlugin.

http://scplugin.tigris.org/

SCPlugin is a great little tool that integrates Subversion access with the MacOS Finder and lets you do all of your basic SVN commands from there (checkout, update, revert, etc).  It doesn’t give you deep, deep access into SVN commands like version checkout or repository browsing, but for lazy commands like checkout and update, it’s great.

15. Install CocoaMySQL / SequelPro

CocoaMySQL 0.7b6

CocoaMySQL-SBG 0.7.1

Of those two I prefer 0.7b6 — It seems perfectly stable to me.

CocoaMySQL is an awesome little MySQL editor / browser based on the CocoaMySQL codebase.  I’ve been using it for years and I’ve found it’s a lot faster than entering SQL commands in the Terminal or dealing with PHPMyAdmin (which is super-slow even running locally).

CocoaMySQL lets you manage DBs effortlessly. I find it’s great for DB creation (when you’re still sketching out a project and want to play around with table structure), and for common tasks like importing and exporting.

It’s a native MacOS application, so it’s really zippy and it lets you do local and remote DB access.  And it plays nice with all of the components we’ve installed so far.

I highly recommend it.

The only drawback is that the codebase for CocoaMySQL has been abandoned.  It’s been picked up by the SequelPro project, but honestly these guys have a ways to go before their product matches CocoaMySQL (like — they still need to add user administration).  By the time you read this maybe they’ve added some features.  If not, I’d say CocoaMYSQL still works beautifully — even though the code has been abandoned.

If you want SequelPro, though, you can get it here:

http://www.sequelpro.com/

16. ADVANCED – Virtual Hosts

I first experimented with Virtual Hosts when I started doing WordPress development — mostly because WordPress seems much happier with shorter real world style urls like:

http://radio.local

than those you’d typically see running on your local box:

http://localhost/~jeff/projects/wordpress/radio-station

But once I started playing with these, I decided it was MUCH easier to be able to deal with these super-short urls (think Bit.ly for your local box) than to have to type in the whole thing.  It’s made my life a lot simpler, so I figured I’d pass along the info.

Note that when you choose a host name, you’ll want to choose one that doesn’t exist on the web.  You’re basically overriding your machine’s host lookup functionality — hard coding urls to corresponding locations on your local disk — and thereby bypassing DNS lookup.  So if you pathed http://www.google.com to your local disk, your computer wouldn’t be able to find the real google until you undid the Virtual host.  So be careful.

I typically choose a hostname that includes the suffix .local.  That’s never given me any problems (eg. projects.local, flash.local, radio.local).

Let’s begin.

You’ll be dealing with 3 files on this:

  • httpd.conf
  • httpd-vhosts.conf
  • hosts

Which, if you followed the steps above, are located at:

  • /opt/local/apache2/conf/httpd.conf
  • /opt/local/apache2/conf/extra/httpd-vhosts.conf
  • /private/etc/hosts

a. Enable Apache’s Virtual Host configuration

in httpd.conf, uncomment the Virtual Hosts line:

Include conf/extra/httpd-vhosts.conf

This will allow you to add hosts to your vhosts configuration file.

b. Add host locations

open the vhosts file in your favorite text editor:

/opt/local/apache2/conf/extra/httpd-vhosts.conf

The first thing you’ll want to do here is comment out the default examples they give you (the ones that include ServerAdmin webmaster@dummy-host.example.com).  You might want them as a reference, but you don’t want them to be active.  Comment them out.

Next you’ll add your host.  Substitute in values below to correspond to your build and the hostname you choose:

<VirtualHost *:80>

DocumentRoot "/Users/USERNAME/PATH"

ServerName hostname.suffix

</VirtualHost>

Typically life is easier if you choose a disk path inside of your Sites directory.  If you don’t you might need to do some more file permission configuration.

In my case, the host I added is:

<VirtualHost *:80>

DocumentRoot "/Users/jeff/Sites/projects"

ServerName projects.local

</VirtualHost>

So now Apache knows that when it’s told to serve any files referenced by the url projects.local, it’ll serve them from the path I’ve given it in my Sites directory.  There are other arguments you can add to the VirtualHost node, but this is all you need.

c. Add host names to your hosts file

Next you need to tell the machine that when you ask for that particular server name, you want it to ask the local Apache server to serve it.  Editing this file is globally applicable to all browser applications — so once you edit this file, programs like Firefox, Safari, Opera, etc will all serve up your local page instead of looking the url up with the friendly neighborhood DNS.

Your machine already does this to a certain extent — for example, the ‘localhost’ prefix is already set up via the hosts file so that the machine knows to look to the local Apache server, rather than out on the web.  What we’re doing is adding one or many shortcuts that mimic this behavior — but which point to more specific locations on disk.

Open your hosts file:

/private/etc/hosts

and add the line:

127.0.0.1     hostname.suffix

(so in my case I added):

127.0.0.1     projects.local

then save the file.

d. Restart Apache

Go back to the terminal and restart apache:

sudo apachectl restart

Now you should be all set.  Open up your browser and point it towards the virtual host you just made. In our case that’s:

http://projects.local

And your browser should serve up that url like magic. ;)

Tags: , , , , , ,

  • http://pulse.yahoo.com/_UMTUWKSZTWVOHZ6ZXUMCJFPSUE Isaac Rivera

    Great post update jeff. One minor correction: the macports package linked (MacPorts-1.7.1-10.5-Leopard.dmg) won’t install on snow leopard. Version 1.9.2 will.

    • http://jeffclarke.us/ Jeff Clarke

      good catch, Isaac! Thanks! It’s updated now.

  • http://jeffclarke.us/ Jeff Clarke

    good catch, Isaac! Thanks! It’s updated now.

  • http://www.facebook.com/profile.php?id=775282846 Isaac Rivera

    Then last night I came across a limitation of this setup. No openssl support for php which means you can’t access “https” locations from within a php script. Fortunately, with macports it is very easy to add a step to this tutorial that fixes the issue:

    sudo port install php5-openssl

    done.
    (credit goes to http://ollehost.dk/blog/2010/07/23/thank-you-macports-php5-ssl)

  • http://www.facebook.com/profile.php?id=775282846 Isaac Rivera

    And another one!

    If you actually want to access mysql with php5 you better also install the php-myslql package, otherwise no game.

    sudo port install php5-mysql 5.3.8 +mysqlnd

    • http://jeffclarke.us/ Jeff Clarke

      Hey Isaac- as far as I know this command 
      sudo port install php5 +apache2 +mysql5 +pear +universal

      should install everything for you, including php-mysql and mysqld.  Did that not work for you?

  • xavier dagassan

    Hi,
    I try to follow your guide to install a dev environment on my snow leopard 10.6.8 without success with mac port 2.0.3, i  have a problem when i try to install php5

    I had a lot of error in the main.log
    :info:build Generating phar.php:info:build ld: warning: in /opt/local/apache2/bin/httpd, file is not of required architecture:info:build Undefined symbols for architecture i386::info:build   “_ap_get_brigade”, referenced from::info:build       _php_apache_sapi_read_post in sapi_apache2.o:info:build   “_ap_mpm_query”, referenced from:
    ….
    :info:build       _php_ap2_register_hook in sapi_apache2.o:info:build ld: symbol(s) not found for architecture i386:info:build collect2: ld returned 1 exit status:info:build lipo: can’t open input file: /var/tmp//cctUG9qa.out (No such file or directory):info:build make: *** [libs/libphp5.bundle] Error 1:info:build make: *** Waiting for unfinished jobs….:info:build make: Leaving directory `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_lang_php5/php5/work/php-5.3.8′:info:build shell command ” cd “/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_lang_php5/php5/work/php-5.3.8″ && /usr/bin/make -j2 -w all ” returned error 2:error:build Target org.macports.build returned: shell command failed (see log for details):debug:build Backtrace: shell command failed (see log for details)    while executing”command_exec build”    (procedure “portbuild::build_main” line 8)    invoked from within”$procedure $targetname”:info:build Warning: the following items did not execute (for php5): org.macports.activate org.macports.build org.macports.destroot org.macports.install:notice:build Log for php5 is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_lang_php5/php5/main.log

    I’ve created a ticket on macport https://trac.macports.org/ticket/31750

    If you have an idea!
    Thanks!

    • http://jeffclarke.us/ Jeff Clarke

      Hey Xavier — looks like someone responded to your ticket in Trac over at Macports.  Seems like it was related to apache2 not being installed as Universal….