Setting up MySQL 5 and PHP 5 under Mac OS X (Tiger)

While setting up MySQL 5 and PHP 5 today, I documented the steps I took to get it all running under Tiger. Here’s the basic rundown if you’d like to move to these new versions as well.

Downloads

Install Xcode

Installation of Apple’s Xcode Developer Tools is straightforward — just install the .pkg file inside the .dmg if you downloaded it, or run the .pkg from the discs that came with your computer.

Install MySQL 5

Setting up MySQL is just as easy as installing Xcode, as the developers have kindly put together a .pkg installer. You could always compile it yourself if you have the need. Also included in the .dmg are a StartupItem and Preference Pane, both of which I find handy (although StartupItems are a bit out-of-date, and should probably be replaced with a launchd item).

Install PHP 5

Unpack PHP’s .tar.gz file with your favorite decompression utility, and open a Terminal window. Type cd, followed by a space, then drop the uncompressed php-5.2.1 folder right into the Terminal window and hit Return to change directory to the PHP files.

Type ./configure --with-mysql=/usr/local/mysql --with-apxs=/usr/sbin/apxs to configure the PHP installation, noting the location where MySQL keeps its files (so PHP knows how to get to it in the future).

While PHP is configuring, in a new Terminal window, you should set the root password for MySQL.
Enter cd /usr/local/mysql/bin,
then ./mysqladmin -u root password 'new-password', where new-password is your chosen MySQL root password. Note that this has nothing to do with your computer’s “root” password, and is entirely separate.

Back at the PHP prompt, you’ll see “Thank you for using PHP.” when it’s done configuring. You can then enter make to compile PHP from the source code. This may take a while. When done, enter sudo make install to install the files to their predetermined locations.

Reconfigure Apache

You’re almost done, except that the Apache web server doesn’t know to handle .php files for PHP 5. PHP 4 comes preinstalled with Mac OS X, but PHP 5 changes your Apache config file to enable itself (to include its shared objects when Apache boots), which turns off PHP 4. In this state, .php files will be printed out to the web browser as text files instead of executed — that’s not good! You’ll have to tweak the config file and reboot Apache to make it all go again. If you have the TextMate command-line utility mate installed, these next steps are quite easy.

Enter sudo mate /etc/httpd/httpd.conf (or sudo pico /etc/httpd/httpd.conf without TextMate) in the Terminal to edit the Apache config file. Find <ifmodule mod_php4.c> and change that 4 to a 5, as we’re now using mod_php5.c.

You’ll also want Apache to recognize index.php as a Directory Index file, so hunt down DirectoryIndex index.html and add a space, then index.php to the list, so it looks like DirectoryIndex index.html index.php when done.

At this point you can save and exit if you prefer, but I like to make one last change so I don’t have to dig to /Library/WebServer/Documents every time I want to edit a file I’m working on. In the root of your hard drive, create a folder called WebServer, and then jump back to TextMate (or the Terminal window) where you’re editing the Apache config file. Find the line DocumentRoot "/Library/WebServer/Documents" and change it to DocumentRoot "/WebServer". After making this change, also change the location a few lines down: <directory "/Library/WebServer/Documents"> to <directory "/WebServer">. Finally, save and exit.

Back at the Terminal prompt, enter sudo apachectl graceful to reboot Apache and let everything fly. I hope that helps some people setting up a PHP coding environment out there! A final note: watch out for the “smart quotes” WordPress likes to insert — they may cause copy-and-pastes to produce failures in the Apache config file.

Advertisements
Setting up MySQL 5 and PHP 5 under Mac OS X (Tiger)

12 thoughts on “Setting up MySQL 5 and PHP 5 under Mac OS X (Tiger)

  1. um… why the need for Xcode? Does it offer something I am not aware of? Secondly, I have had (in the past) to switch my socket port in the terminal. Ever come across this issue?

    Like

  2. Good question! Xcode itself isn’t necessary, but the Developer Tools package (which includes Xcode) is needed, as it contains the gcc compiler which builds PHP from the source code. The default Mac OS X installation doesn’t include gcc, so installation of the Developer Tools/Xcode is required.

    Like

  3. Why again would we compile from scratch instead of MAMP?

    I mean, the reason I ask is…if you need a tutorial to teach you how to compile something, you probably shouldn’t be doing it…that and MAMP has been absolutely wonderful thus far…I have yet to run into an issue with it…although mod_rewrite does act a little funny on Macs from my experience.

    Like

  4. If you want to add additional features, tweak the installation to your liking, or just get good at deploying a server, compiling everything yourself is the way to go. (For example — correct me if I’m wrong — the default Mac OS X installation of PHP doesn’t come with the GD image library, which requires you to rebuild it.) You’d also want to compile and run MySQL and PHP on a server to run your shiny new code, and most production servers don’t run double-click-to-launch environments because they have no GUI.

    MAMP is pretty cool, though, saving lots of time if you just want to get rolling right away — perhaps you’ve got a project idea that simply can’t wait for rather lengthy ./configure, make, make install :-)

    Like

  5. Rob says:

    So I have installed MAMP, but want to use it for an Internal Intranet web server, not just locally. How do I get the Intranet to see my MAMP Apache Install instead of the one that ships with Mac OS X?

    Like

  6. Shawn says:

    I followed these instructions and now my PHP pages display as text. My configuration worked previously with the default PHP version that came with OS 10.4. Help?

    Like

  7. Shawn says:

    Nevermind – I fixed it by adding the following 2 lines at the end of my httpd.conf file:

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

    -I’m new and inexperienced but I found this on phpmac.com so I’m passing it on.

    Like

  8. Johnny says:

    Jeez,

    I thought PHP was simple, I’ve spent days trying to get PHP installed many different ways and all have failed. I finally just compiled from source, what I should have done to start.

    Like

  9. Kirt says:

    Once you complete the process, if you experience a problem with calls to mysql_connect(…) causing Segmentation Fault errors, re-edit the /etc/httpd/httpd.conf file and comment out the old php4 module lines: “LoadModule php4_module” and “AddModule mod_php4”, then restart apache (sudo apachectl graceful). That should do the trick. It took me forever to figure that one out.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s