Skip to navigation

malevolent design weblog

This blog is now defunct, but you can find more stuff over at my personal site

Setting Up Apache, PHP, MySQL, .htaccess and mod_rewrite in OS X

OS X comes with the Apache web server, and PHP’s in there somewhere, so you’d think it’d be easy to add the finishing touches to produce a simple PHP/MySQL development server, right? Hmm, not quite.

(Disclaimer: I’m far from being a systems administration expert and am simply putting all this out there in case it’s helpful, so don’t blame me if it’s inappropriate for your system/requirements and screws something up.)

Apache and PHP

Switching on ‘Personal Web Sharing’ works OK, although it forces you to open port 80 on the firewall whether you want to or not, and only lets you run one site. So I installed Headdress, a little utility that hacks around with the configuration on your behalf, making setting up multiple sites easy. I set up a couple of sites nested within my account’s Sites folder and activated PHP.

MySQL

I downloaded MySQL 5 and installed the server, start-up item and preference pane. I then set a password for the root user with

/usr/local/mysql/bin/mysqladmin -u root password [password]

and installed CocoaMySQL to help with managing databases and moving data across.

It then became clear that PHP wasn’t talking to MySQL, as it looks for the wrong socket file by default; great. There are a few ways to fix this:

  1. Change where MySQL creates its socket file
  2. Make PHP look for the right socket file
  3. Use a symlink to point one location at the other

The first two approaches are covered by this handy OS X MySQL reference, and I reckon the second option is probably the most sound, but I got it working with the third using

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/

.htaccess and mod_rewrite

The fun wasn’t over yet, as by default OS X’s web server doesn’t support .htaccess files, which are handy if you’re doing nifty things with mod_rewrite to manipulate URLs.

I followed clagnut’s advice to enable .htaccess (except I used AllowOverride All in both files, and altered AccessFileName to htaccess.txt to make it Finder-friendly), but that still left mod_rewrite dead. I had to add this to /private/etc/httpd/users/[username].conf

<Directory "/Users/[username]/Sites/*/">
Options Indexes FollowSymLinks
</Directory>

(Remember, I’m putting sites in folders within my OS X account’s Sites folder, none of this’ll work if you’re doing things differently)


I really didn’t expect over half a day of head-scratching for such a basic setup, and chances are I’ll have to repeat much of it after each OS update. Maybe I should switch to MAMP


Comments

Hi Matt,
I find setting up my own installs separate from the apple bundled ones are safer:
You get the version you want configured the way you want.
Less risk of unexpected breakages when you apply an Apple update.

— Mike, 22nd Aug, 5:20pm

Yeah, I though it'd be nice to use what's already there, but it requires so much hacking around to be usable I'm getting the clear impression it's not worth the effort. If MAMP really is as self-contained as it sounds then it'll be easy for me to give it a quick try for comparison.

Matt Round, 27th Aug, 10:33am


Comments are now closed for this entry.