DKR Computing

                                                            

  • Increase font size
  • Default font size
  • Decrease font size
Home DKR Computing Blog

Enable PHP for Apache under Mac OS 10.6.5

E-mail Print PDF

If you want to skip my commentary and see the necessary steps to make PHP work under Mac OS 10.6.5, just go straight to the bottom of this article. If you want to see my troubleshooting steps, then please read on!

I do the majority of my primary development work on the Mac and then move the results over to Linux and perform any necessary tweaks there. That way, my stuff will work on both systems without any other modifications when I send it along to my clients.

I usually reload my Mac a couple of times each year so I can work with a fairly clean system. Of course that means I need to make some minor configurations in order to do my dev work.


PHP does not come enabled by default on Mac OS so that's one of the changes I have to make after a reload. Usually that meant editing /private/etc/apache2/httpd.conf and uncommenting the line that reads:

LoadModule php5_module        libexec/apache2/libphp5.so

Then I restart Apache with "apachectl restart" and I'm good to go. At least, it used to be that way.

First off, when I ran "apachectl restart" I got an error.

/usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument

I dug into /usr/sbin/apachectl (it's a shell script) and saw line 82:

$ULIMIT_MAX_FILES

Well, since the script executes the value of the variable I need to look higher up in the script to find out how it's set. I find the following on line 64:

ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"

On my system, "ulimit -H -n" returns "unlimited" so the command the script is ultimately trying to run is "ulimit -S -n unlimited". Looks like "unlimited" is not a valid value for -n. I did some digging around with Google and found that changing line 64 to "ULIMIT_MAX_FILES="ulimit -S -n 1024" will work just fine. I changed that line and then apachectl started working again. Since I'm not running a production web server with a lot of vhosts on my Mac, the 1024 setting should work just fine. FYI, I found the solution at http://articles.itecsoftware.com/shell-scripting/fix-for-mac-os-10-6-5-apachectl-line-82-ulimit.

Then things went a lot more smoothly, until I tried to submit a form using a PHP file as the method. Then, my browser wanted to download the PHP file rather than load it as a page. This indicates that Apache is not handling the PHP file properly. Time to dig into the Apache configuration again. The main PHP module configuration is located in /private/etc/apache2/other/php5.conf. It was configured as follows:


AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


DirectoryIndex index.html index.php



Hmm, looks OK to me but it's not working. Did some more digging on Google and found http://bugs.php.net/bug.php?id=36772. That said that there should be AddHandler entries. All I did then was to copy the two AddType lines and change "AddType" to "AddHandler", restarted Apache, and then the form method worked properly. I probably don't really need the AddType lines but I left them there anyway.

Well, that was all well and good until I tried to connect to my MySQL database using the PEAR MDB2 module. In my browser, I saw "MDB2 Error: unknown error". If there's one thing I can't stand it's an error message telling me it doesn't know what the error is. Now it's time to look at my code and look for a problem. This is frustrating to have to do as well since this same code worked without a hitch before the OS reload.

I was using an MDB2 phptype setting of "mysqli" and so I changed it to "mysql" just in case it was a problem with the MDB2 mysqli driver. I didn't get the "MDB2 Error: unknown error" message again. That was the good news. The bad news is that I now saw "MDB2 Error: connect failed". But then again, that is good news since I have something to work with other than "unknown error".

My first thought was that maybe I had forgotten to setup the MySQL user account for the application but a quick check confirmed that I had. I was also using the correct username, password, host, and database name in the app config. I logged in to MySQL with the mysql client using the same credentials and got in without a problem. I figured that maybe the Apache PHP module wasn't connecting to the local MySQL socket.

I went to check the php.ini file in /private/etc and found that it wasn't there so the PHP module was using it's default settings. By default, the PHP module tries to connect via the MySQL local socket at /var/mysql/mysql.sock. On Mac OS, the MySQL local socket is located at /private/tmp/mysql.sock. Therefore, I edited /private/etc/my.cnf as follows:

[mysqld]
skip-networking
socket=/private/tmp/mysql.sock

[client]
socket=/private/tmp/mysql.sock

This tells MySQL to not listen on a TCP port and to use the local socket at /private/tmp/mysql.sock.

This wasn't really necessary since that's the default local socket anyway but I did it just in case I needed to look it up later. Then I proceeded to configure a php.ini file.

There is a php.ini.default file in /private/etc so I copied it over to /private/etc/php.ini and changed the following lines:

Line 1063: pdo_mysql.default_socket=/private/tmp/mysql.sock
Line 1216: mysql.default_socket = /private/tmp/mysql.sock
Line 1275: mysqli.default_socket = /private/tmp/mysql.sock

I then saved the file and restarted Apache. Now all is working well. No more MDB2 errors.

To sum up, here is what I did:

1. In /private/etc/apache2/httpd.conf, I uncommented the line reading:
LoadModule php5_module        libexec/apache2/libphp5.so

2. In /private/etc/apache2/other/php5.conf, I copied the two AddType entries and changed "AddType" to "AddHandler".

3. In /usr/sbin/apachectl, I changed line 64 to:
ULIMIT_MAX_FILES="ulimit -S -n 1024"

4. Edited /private/etc/my.cnf and added the following line under both the "[mysqld]" and "[client]" sections:
socket=/private/tmp/mysql.sock

5. In /private/etc, copied the php.ini.default file to php.ini and set the following lines:
Line 1063: pdo_mysql.default_socket=/private/tmp/mysql.sock
Line 1216: mysql.default_socket = /private/tmp/mysql.sock
Line 1275: mysqli.default_socket = /private/tmp/mysql.sock

6. Restarted Apache.

7. Restarted MySQL.

Hope this helps you.

Later,
Keith

 

Last Updated on Wednesday, 24 November 2010 23:01  

Add comment


Security code
Refresh


Follow us on Twitter