Categories
Uncategorized

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.

In WordPress’s wp-config.php file, I set the WP_DEBUG constant to true to see what is going on:

define('WP_DEBUG', true);

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:

tazair:smartwebdeveloper taz$ ls -l /etc/php*
-rw-r--r--  1 root  wheel  69302 20 Feb 00:42 /etc/php.ini-5.2-previous
-r--r--r--  1 root  wheel  69337 21 Jul 12:20 /etc/php.ini.default
-r--r--r--  1 root  wheel  69060 16 Dec  2010 /etc/php.ini.default-5.2-previous

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:

tazair:~ taz$ grep .default_socketΒ /etc/php.ini
pdo_mysql.default_socket=/var/mysql/mysql.sock
mysql.default_socket = /var/mysql/mysql.sock
mysqli.default_socket = /var/mysql/mysql.sock

After editing php.ini:

tazair:~ taz$ grep .default_socket /etc/php.ini
pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

I also enabled two of the three MySQL extensions, which were configured off in the default php.ini in the Lion install. WordPress perhaps only needed one of three MySQL extensions:

tazair:~ taz$ grep mysql /etc/php.ini|grep ext
extension=php_mysql.dll
extension=php_mysqli.dll
;extension=php_pdo_mysql.dll

I needed to restart Apache for it to pickup the new configuration:

tazair:~ taz$ sudo apachectl restart

Happy days! PHP could talk to MySQL, and my WordPress site was working on OSXΒ Lion.

By the way, if you need mycrypt working with PHP on Lion, Michael Gracie‘s written an excellent post on how to do this.

If you have any questions or comments about the process above, please leave a comment. If this article helped you, please click the Tweet or +1 buttons. Thanks for visiting!

68 replies on “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?

Thanks,
Tony

Tony, PHP extensions are compiled code. The compiled code for extensions are typically loaded by at PHP at runtime when it finds the extension requested in php.ini.

On Mac, Linux & Unix, runtime-loadable code ends with “.so“, standing for shared object. On Windows, runtime-loadable code ends “.dll“, standing for dynamically linked library.

PHP extensions on Mac should always end with “.so“.

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.

Hope this helps!

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,
John

John,

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.

Finally, have you configured your virtual host by modifying the file /private/etc/apache2/extra/httpd-vhosts.conf? Apple provides a template file here.

Let me know if this helps John. If you still have problems, I can post my config for you to try.

I’ve been trying the above on os x lion server and have had no luck :/. Everything still resolves to the default site.

Also tried the below comment and haven’t had much luck. I normally use XAMPP but would like to get away from it if I can get this in working mode!

John,

If I go to http://127.0.0.1, the localhost address – without any directory or username – my mac shows “It works!” in my browser.

Does Lion do the same for you?

If so, try editing /Library/WebServer/Documents/index.html.en to show something like “It works more!”

Will it work that far for you?

Hey thanks for the reply. I can get that far but I’m unable to get the other 6 virtual sites I’ve got on my localhost to work :/. Not sure what else to try.

Actually I don’t get the ‘It works’ page… I get the default site lion sets up located in /Library/Server/Web/Data/Sites/Default.

If I don’t use the “Server” app to start apache then it it sets my default directory to /var/empty and I get a 403 forbidden obviously. But, the other part of this is it won’t read my virtual hosts :/

They created a mess with this.

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).

thx for all the tips and comments!!!
127.0.0.1 saved my day, mamp init it for me and i just put all my stuff into /Applications/MAMP/htdocs and it worked!

Good luck to all!

Hi Tom!

I assume you’re using the mysql distribution from mysql.com, not the mysql that comes with Lion server or the one installed using MacPorts or Fink.

Do you get a command not found error like the one below?

tazair:~ taz$ mysql
-bash: mysql: command not found

If so, to fix this, tell the shell (bash) where to find the MySQL executable:

export PATH=$PATH:/usr/local/mysql/bin

Is the problem that you can’t connect to MySQL?

tazair:apache2 taz$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

If so, check if MySQL is running. Go to the MySQL Preference Pane in System Preferences and click Start MySQL Server to start MySQL.

MySQL should now be able to connect and login to your local MySQL database on your Mac:

tazair:~ taz$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.

Does this help you Tom?

Thanks for this, worked like a charm. I’ve come to expect some PHP+MySQL cleanup every time I upgrade. This time it was less painful than previous OS upgrades!

Html works but I can’t get php to work. I renamed the php.ini.default to php.ini and restarted the server, but this made no difference at all. Php still is not working.

Everything worked great ‘out-of-the-box’ in Leopard. Is there a path difference between Leopard and Lion? Do I need to edit something in the php.ini, or maybe somewhere else?

Thanks
Daniel

Hi Daniel!

A few questions so I can help you get PHP working on your Mac:

  • When you say PHP “doesn’t work”, what actually happens when you go to a PHP webpage?
  • What’s the path to your PHP files? e.g. /Users/taz/Sites/index.php
  • Have you edited / customised your Apache configuration at any stage? For example to create a virtual host.
  • Please run the command grep php5_module /etc/apache2/httpd.conf in the Terminal and paste the output into your reply.

My gut feel is PHP probably isn’t enabled yet on your Mac running Lion.
Even with Snow Leopard it was necessary to enable PHP.

-Taz.

Great job! I’d been struggling with this for a day or so… great to come across all this great knowledge.

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.

Was having this same problem with a WordPress install. But thanks so much for this step by step. Fixed my problem.

Im having a devil of a time…I installed mysql but I can’t access it from terminal and I can’t connect to it from phpmyadmin. Im running lion server.

Taz,

I get “-bash: mysql: command not found” when I type in any mysql command. I tried setting the export path but that only saves it for that terminal session.

Thanks,
Jarrad

Jarrad,

It sounds like MySQL is installed, but you just need a way to persistently set the PATH to the mysql executable.

One way to do this is to edit the .profile file in your home directory, which is executed by bash every time you open a new Terminal window. You can add your export PATH statement there.

Alternatively, you can set the path systemwide by editing /etc/paths or putting a new file in the /etc/paths.d/ directory.

If you need help finding the mysql executable, or want specific instructions for setting the path using one of the ways above, let me know. πŸ™‚

-Taz.

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.

Thanks,

Jarrad

Jarrad,

Yes, setting the bash shell’s PATH is needed to run the mysql command.

You may find /usr/local/bin is in your PATH by default, but mysql isn’t in that directory.

You need to add the directory /usr/local/mysql/bin to your PATH.

tazpro:~ taz$ mysql
-bash: mysql: command not found
tazpro:~ taz$ echo $PATH
/opt/local/lib/postgresql91/bin:/opt/local/apache2/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:.
tazpro:~ taz$ export PATH=$PATH:/usr/local/mysql/bin
tazpro:~ taz$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.11 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
tazpro:~ taz$ 

-Taz.

Hello,

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,
Raphael

Raphael,

It sounds like PHP hasn’t been enabled in Apache yet.

To do this, edit the Apache configuration file, for example by typing the following command in the Terminal app:

sudo pico /etc/apache2/httpd.conf

Find the line containing:

LoadModule php5_module

This line probably starts with a hash “#” character, disabling PHP loading. Delete the hash character and save the file.

The restart Apache:

sudo apachectl restart

You will need to enter your Mac user’s password here.

That’s it! Apache should be enabled now.

Let me know if you have any questions or problems Raphael. πŸ™‚

-Taz.

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.

Thanks,
Raphael

This is the example I am referring to. I forgot to remove the html tags from the original. I have removed the “” pairs.

html
title HTML with PHP /title
body
h1 Example h1

?php
//your php code here
?

/body
/html

After the whole upgrading to Lion thing I gave this a 2 hour fiddle, but with my limited knowledge couldn’t figure it out. Today I tried again – tried to find the solution by googling, that is.

Your problem was exactly the same as mine, so after following your instructions – happy days!

Thank you for sharing

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.

but php always told me like below:
ERROR – 2012-03-04 18:01:32 –> Severity: Warning –> mysql_pconnect() [function.mysql-pconnect]: No such file or directory /Library/WebServer/Documents/system/database/drivers/mysql/mysql_driver.php 91
ERROR – 2012-03-04 18:01:32 –> Unable to connect to the database

please help me… thank you.

—-
I’ve tried cp php.ini.default to php.ini , but it make the whole site down(can’t start apache) ….

I’ve fix it, thank you all.

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

which makes php can find mysql…

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.

-John (Not the ‘John’ above :))

This might help someone. But if you are using the latest mac with osx 10.7.4, do NOT uncomment the MySQL extensions in the php.ini. It looks like MySQL and sqli is already compiled with php.

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)

with regards
Tushar Joshi, Nagpur

Comments are closed.