Adventures in DarwinPorts (Installing Apache2, PHP5, MySQL 4.1, PostgreSQL, and Django)
Yeah, that’s quite a mouthful.
I decided a couple weeks ago that I wanted to learn Django as well as Ruby on Rails. Django is a little bit less trendy, but seems to have more flexibility, and gives you an entire admin section of your app “for free”, as opposed to RoR, for which there are a zillion different ways to hack on user management, and no sense of “best practices” for a novice.
Django requires an up-to-date installation of Python, and a number of other dependencies, including a database API for Python. The best installation instructions I could find for OS X (one of the reasons I use a Mac is the availability of all these great UNIX tools that are so useful for a webmaster) recommended using DarwinPorts (soon becoming MacPorts) to simplify installation.
DarwinPorts is awesome.
Getting the Latest Apache / PHP / MySQL
I peeked through the list of available packages, and decided that as long as I was at it, I might as well add an Apache 2 / PHP 5 / MySQL 4.1 server to run on port 81 alongside the default Apache 1.3 / PHP 4 / MySQL 4.0 that I already had on port 80.
An earlier attempt to do this had run out of steam as I realized I would have to choose, locate, configure, and compile all my Apache modules by hand, which is really beyond my ability and ambition. But DarwinPorts would allow me to install Apache with a whole bunch of common modules included, and would download and install all the dependencies for each one automatically.
Well, even on my top-of-the-line-a-year-ago machine, this stuff takes a while to compile, but about an hour later (after cursing at myself for misunderstanding the sudo -u mysql command as running MySQL and trying to fix an error by prepending a path to the username) I had a working secondary server. DarwinPorts not only installs dependencies for each piece, and not only configures everything for you, and not only reminds you of changes to make to your $PATH variable in case the installer couldn’t, but sometimes it even tells you what commands to run after installing each program to properly do things like initalize databases. Sweet.
Here’s how you do this:
- port install mysql4 +server
- port install apache2 +server
- port install php5 +mysql4 +apache2
- copy httpd.conf.sample to httpd.conf
- [optional] change path in DocumentRoot and associated directives
- [optional] change Listen 80 to Listen 81 (If you want to run side-by-side with an Apache 1.3 installation on port 80)
- add the following to the directive:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Don’t forget to follow the extra directions DarwinPorts gives you.
Python, PostgreSQL, and Psycopg Spell Problems
I easily updated my Python installation, and then I ran into trouble. You see, the instructions I had for DarwinPorts were for installing with SQLite. I wanted to use PostgreSQL, partly because I figured it would be a useful thing to learn, and partly because skimming the initial tutorial and its comments had showed me that PostgreSQL was perhaps the optimal database choice because of its handling of foreign key constraints.
The main installation instructions for OS X used PostgreSQL, but compiled it by hand.
So I decided to wing it. The DarwinPorts available ports list included three versions of the PostgreSQL package, so I picked the latest one (postgresql81), and installed it. Then I installed the next dependency (py-egenix-mx-base). No problem. And then I ran into the final piece, psycopg.
Psycopg is a package that allows Python to interact with PostgreSQL databases. The instructions were very specific about installing version 1.1, not version 1.0 or 2.0, of psycopg. Great. I asked DarwinPorts to install psycopg.
Well, first problem: the DarwinPorts psycopg package depends on the postgresql8 package, not postgresql81.
Second problem: every server from which it attempted to download postgresql8 timed out.
I was a little bit flummoxed at this point.
I backtracked, downloaded the source code for psycopg, compiled it by hand using the main OS X installation instructions (I had to tweak a lot of paths to reference DarwinPorts’ installation locations), and boom! I’m off and running.
I feel like an idiot for not documenting the exact commands I used, for the edification of future generations. If I ever do this again and am not running down blind alleys looking for solutions to problems caused by my own typos, I promise to do so faithfully. In the meantime, here are the steps to do it the easy way (assuming the postgresql8 servers are working):
- sudo port install python24
- sudo port install postgresql8
- sudo port install py-egenix-mx-base
- sudo port install py-psycopg
- [optional] sudo port install subversion (If you’re using the development version of Django, or just want subversion)
- Install Django
Next up, I’ll tell you what it’s like to jump from MySQL to PostgreSQL, and all about my experiences with the Django tutorial.