Running PHP and mySQL on OS X : missing php.ini and mySQL socketsFebruary 20th, 2006
I recently upgraded my mac to OS 10.4. Overall I’ve been pleased. Recently I decided to fire up PHP and mySQL and ran into some trouble.
Let me start at the beginning… I’ll cover the problem in a minute.
Getting php running on OSX couldn’t be easier. It is basically two steps:
- Go to system preferences, then click on Sharing. Click on ‘Personal Web Sharing’, then click the ‘Start’ Button. Voila, you’ve got Apache up and running. Go to a browser and type ‘localhost’. You should see a welcome page of sorts. The home directory for your new website is located at : Macintosh HD/Library/WebServer/Documents. So far so good
- Now, you probably want to enable PHP. You have several options. You can install it yourself, or you can use the version that shipped with OS X. To install the shipped version, do the following:
- Open apache’s configuration file, located at: Macintosh HD/private/etc/httpd/httpd.conf (note: this is a hidden file, so you will need to display hidden files first)
- In that httpd.conf file, search for this line: LoadModule php4_module , and uncomment it (remove the # sign in front). Done, save that file and close it.
That’s it. Create a file in your home directory called index.php. In that file write
Now go back to your browser, and you should see a screen full of purple php data. Easy.
- To get mySQL running, go here, download the package and run it. It’s a great package that installs mySQL in about 3 minutes.
So far so good!
Now the problem (and solution)
Everything was moving along nicely – until I tried to connect to my database from a PHP page. To make a (really) long story short, a bunch of research revealed that since OS 10.4, the PHP install tries to connect to mySQL via the socket: /var/mysql/mysql.sock, but mySQL uses /tmp/mysql.sock. So they won’t talk to one another. The trick is to change your php.ini to point to /tmp/mysql.sock.
OS X doesn’t have a php.ini initially. But it does have a php.ini.default located at Macintosh HD/private/etc. (Again, this is a hidden file.) Duplicate that file and rename your copy php.ini. Now search for the line mysql.default_socket. Update that line to say mysql.default_socket = /tmp/mysql.sock.
Now restart the web server (system preferences, sharing, turn personal web sharing off, then on).
That’s it. php sees mysql and all is well with the world.