18 Aug 2006
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.
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:
- Change where MySQL creates its socket file
- Make PHP look for the right socket file
- 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
htaccess.txt to make it Finder-friendly), but that still left
mod_rewrite dead. I had to add this to
<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…