The foibles setting up a development environment on Mac OS 10.5

Setting up PHP, Apache and MySQL

Mac OS 10.5 is a great place for web developing, as I am discovering. When I got my new MacBook Pro, I found that it had Apache already installed, including several other development tools that are useful to me, such as perl, PHP and subversion. It did not, however, have MySQL.

Through many web searches, and much trial and error, I finally got my system up and running and working how it’s suppose to. Mostly.

Setting up Apache:

This was fairly easy. You can either go to the sharing screen in System Preferences and check Web Sharing:

mac - web sharing pic

Or you can start it via the command line:

> sudo apachect1 start

Working with PHP:

PHP5 comes pre-installed on Leopard, but it’s not fully functional with Apache. You have to go into the Apache configuration file and tell it to use PHP.
Open up /etc/apache2/httpd.conf and uncomment the line (remove the #):

#LoadModule php5_module libexec/apache2/libphp5.so

and save the changes.

Installing and running MySQL:

The installation of MySQL went easily and quickly, as most things do on a Mac. However, there were a few problems.

First, the latest version of MySQL for Mac (5.1) doesn’t install in the same place as the system expects it to. Instead of installing in /usr/bin like the system expects, the executables install in /usr/local/mysql/bin. This means you have to change your environment to point to this location.

$PATH:/usr/local/mysql/bin

The MySQL starter package caused me the most troubles. I couldn’t seem to get MySQL to start up when I booted my computer - even the GUI in System Preferences wouldn’t start the database package.

The problem turned out to be the location where the MySQLCOM folder was installed. The default location was /usr/local but Leopard needs it to be in /Library/StartupItems for it to work.

So you have to move the folder into the right place:

> sudo cp -r /usr/local/MySQLCOM /Library/StartupItems

That should make it possible for MySQL to start up automatically every time you turn your computer on.

MySQL installation has a habit of putting files in the wrong places on Leopard, the most annoying (so far) being the location of the mysql.sock file.

Trying to integrate MySQL and PHP was a trial. PHP is looking for mysql.sock in /var/mysql/mysql.sock, but MySQL installed it in the old location /tmp/mysql.sock.

This is easily fixed, once you know what the problem is.

There are two ways to do this. The easy way:

> sudo mkdir /var/mysql
> sudo chown _mysql /var/mysql
> sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

This is the best way - and easiest - way to make it all work, especially if you want to run the DBI perl module on your system. (DBI still looks for /tmp/mysql.sock.)

Or the harder way (but is probably better from a programming stand-point):

First, create a my.cnf file in /etc and add in:

[client]
socket = /var/mysql/mysql.sock
[mysqld]
socket = /var/mysql/mysql.sock

to /etc/my.cnf

Then, copy the socket file over to the proper directory for PHP to access. Also, create a symbolic link in the /tmp directory to the /var/mysql/mysql.sock so that other programs that depend on that file being in /tmp (ie. perl DBI::mysql) will still be able to function.

> sudo mkdir /var/mysql
>sudo chown _mysql /var/mysql
> sudo mv /tmp/mysql.sock /var/mysql/mysql.sock
> sudo ln -s /var/mysql/mysql.sock /tmp/mysql.sock


This may end up causing problems if you’re using the GUI aspect of MySQL, but since I’m not, I didn’t bother to fix that.

Apache needs to be restarted for the changes to take effect:

> sudo apachect1 restart

PHP should work after this.

You can find out how to solve the MySQL GUI issue, as well as setting up virtual hosts on Apache from this blog post.

This solution, however, also causes problems when using the DBI perl module. As I stated above, DBI looks for /tmp/mysql.sock and if it doesn’t find it (as /tmp is emptied upon computer shutdown), then it returns an error.

I started off with the harder, more complex way, but ended up switching to the first configuration I mentioned, as it’s easier and causes less problems.

Write a comment