Lion Upgrade Killed My PHP Website – and How I Fixed It

After installing Mac OS X Lion, I found the PHP site I use for development stopped working. I’d just get an empty webpage when I went to the website. It’s a WordPress site, using both PHP and MySQL. Lion ships with PHP 5.3.6, the latest production release of PHP.

It turns out WordPress / PHP could not connect to the MySQL database on my Mac running Lion:

Warning: mysql_connect() [function.mysql-connect]: [2002]
No such file or directory
(trying to connect via unix:///var/mysql/mysql.sock)
in /Users/taz/Sites/smartwebdeveloper/wp-includes/wp-db.php
on line 1036

I checked my Apache config. The Lion upgrade process left my Apache config alone, so my virtual host was still intact and working. It put a new httpd.conf.default in place in case I Apache wanted to use an more up to date Apache configuration. Happily it had left my configuration that enabled the PHP 5 module in place.

I remembered that Mac OS X Server’s MySQL install puts the socket for communicating with MySQL at /var/mysql/mysql.sock. The install of MySQL I’m using, from mysql.com, in order to not conflict with Apple’s MySQL, puts the socket at /tmp/mysql.sock. I’d configured PHP back when I was using Snow Leopard to talk to the MySQL socket in /tmp.

I created a quick PHP file calling the phpinfo() function in my website’s home directory to check the PHP configuration:

<?php phpinfo();

I went to that webpage through the web and found, sure enough that all three PHP MySQL extensions – MySQL, MySQLi and PDO MySQL were all expecting the MySQL socket to be in /var/mysql.

I checked the PHP configuration directory /etc/php. The reason why PHP wasn’t working properly (i.e. connecting to MySQL) was OS X Lion install had moved my PHP configuration file – php.ini – out of the way:

Lion renamed my php.ini file to /etc/php.ini-5.2-previous, and put a new sample PHP configuration file, /etc/php.ini.default. It makes sense to do this: Lion uses PHP 5.3 which has new options and defaults, compared to PHP 5.2 used on Snow Leopard.

I copied the example PHP configuration into place:

sudo cp /etc/php.ini.default /etc/php.ini

I edited the new PHP configuration to update all references to /var/mysql/mysql.sock to /tmp/mysql.sock. Before editing php.ini:

68 Responses to Lion Upgrade Killed My PHP Website – and How I Fixed It

I am having the same problem you were. I got the mysql sock directory changed, but the big problem I’m having is that all of my old extensions were .so files and not .dll files. I can’t find any information about this difference except a lot of info on how .dll files are for Windows apache servers while .so files are for the rest of the world.

Do you have any info you can share with me on how I get these .dll extensions?

The configuration lines you’re seeing in /etc/php.ini like ;extension=php_bz2.dll are just examples of enabling PHP extensions on Windows.

Many of the extensions in the Windows examples are already enabled for PHP by Mac on default, for example bz2, curl, mbstring and mysql. These extensions have already been linked with PHP executable, so there are no separate “.so” files for them. You can use the phpinfo() function to show what extensions are preloaded for you on Lion.

The default directory for PHP extensions on Lion and Snow Leopard is /usr/lib/php/extensions/no-debug-non-zts-20090626. I’ve got only two extensions available there: phpcups.so and xdebug.so. It is also possible to include an extension from another directory by giving the full path to it.

If you want to use a PHP extension on Mac:

(1) Check if the extension is already included using phpinfo().

(2) If it’s not included, check if you already have the extension’s “.so“, usually in /usr/lib/php/extensions/no-debug-non-zts-20090626. If you do, enable it in php.ini.

(3) If you don’t have the extension’s “.so“, install the extension. The standard way to do this is with pecl, but I don’t think I’ve tried this on Mac.

I can’t even get to this point! Any virtual host I create redirects me back to the built in OS X page no matter where I store the web files for the custom site. Did you run into this problem at all? I can find no references to it anywhere, so either I am missing something simple, or its a bug only I am getting.

Thanks for your question. You are not alone! I can remember having problems getting virtual hosts working on Mac a few years back and getting frustrated. From memory the solution was very simple. Unfortunately this was before I took detailed notes!

You’re probably having an Apache configuration issue. Virtual hosts work fine on Lion – I’m using them right now.

First off, have you restarted Apache after configuring your virtual hosts in Apache? Apache won’t pickup your new configuration until you restart it.

Next, in httpd.conf have you uncommented the line Include /private/etc/apache2/extra/httpd-vhosts.conf? To uncomment it, delete the “#” at the beginning of the line. Without uncommenting this line, Apache won’t read the virtual hosts configuration file.

I’ve run into this problem in the past, particularly when trying to use the user folder. Instead of banging my head, I simply swap out the contents of /Library/WebServer/Documents and edit my host file with the respective domain name. Easy to test with a file containing nothing but in it pointed from “http://localhost/” (which should be the only usable item left in said host file after upgrade).

Thanks very much for this. I upgraded to OS X Lion 10.7.2 so I could start using iCloud and your post saved me a lot of time and frustration. The Lion upgrade was much easier than going from Tiger to Leopard.

Since there is an alias in /usr/local/ for mysql, does that really need done? Im not sure where mysql was installed on 10.6 but when I installed it on 10.7, it was installed to /usr/local/mysql-5.5.19-osx10.6-x86_64 with an alias of mysql placed in the /usr/local folder.

I’m having a different problem. I have php, html, mySql working. My problem has to do with having html in a .php file like;

HTML with PHP

My Example

I get a blank page in Safari and a dump of the .php in FF. If I change the extension from .php to .html, it works. This was never a problem in the past on Leopard. I’m I missing a configuration setting?

Thanks Taz. The problem has to do with mysql.sock. I tested my mysql installation with my local sql browser but not with php. PHP could not locate the mysql.sock file in the default /var/mysql directory. Once I linked /var/mysql to /tmp, it worked.

I’ve got some different error.
I installed a new lion, and enable php module(it works) , and install an order verion mysql (5.5.2), it works too, I can access mysql via client tools, and also in shell.

Just a little update: I’m on Snow Leopard but I came across this page in Google.

I was trying to get this to work and was getting nowhere. Today, 3-25-2012, I downloaded: mysql-5.5.22-osx10.6-x86_64.dmg from the Mysql site.

Notice the little one increment update of the version? Well, using this with a system that I had deleted the previous installed, (not the stock default version) of MySQL Community Server, then updating the php.ini to point to: /tmp/mysql.sock, restarting the server (I just used the preferences panel).

“And there was much rejoicing….” ‘yay…’

Thank you for pointing me in the right direction and I hope my info might help someone else.

Thanks a ton!
You saved a lot of frustation and time for me. Thanks for spending time to write this blog and sharing your findings with all. I followed all the instructions and got my site back on Mac (Mountain Lion)