MediaFrenzy

February 4, 2007

Setting up a PHP/MySQL development environment under OS X

Filed under: Articles,Cult of Mac,Technology — Allan @ 4:48 pm

Since my laptop got nicked last year, I started using a loaned Powerbook to try and get some work done. I’d always wanted to make the move from PC to Mac, but was always held back by the ease of use of a “known” operating environment and a large drive full of existing Windows format files. Since someone in Margate was kind enough to liberate me of the burden of sorting through all that mail and endless versions of backed up files, I reckoned it would be the perfect opportunity to make the transition.

So then there I was with a new MacBookPro, new operating system and new opportunities for breaking stuff. And the first thing that I needed to set up was a development environment.

On a Windows machine it’s really convenient to simply download and use the excellent xampp package, and I soon discovered that it existed for OS X as well. The installation is simple enough – simply extract the contents into the /xampp folder on your hard drive and you can use the mampp program to control the startup of the xampp services. Apache worked great and comes with a PHP packed full of compiled in modules. The main problem though was that MySQL failed to load, which made it fairly pointless for my purposes. I could have trolled about on the xampp forums to see why it wouldn’t load but the xampp website warns that the software is alpha quality, and with the xampp developers no longer having access to an OS X development platform I decided to just install each of the services that I needed individually.

Apache
Apache comes pre-installed with Apache, accessible through the Sharing pane in the System Preferences.

Sharing

This is a standard installation of Apache 1.3 so no PHP is installed yet. We’ll do that a bit later from the PHP source. For the moment let’s just check where the Apache is serving files from. There are 2 places that you can store files to be served out by the OS X Apache installation.

The first option is the Apache server root – files that will be served out if you browse http://localhost or others browse http://yourcomputername. These files live in the Macintosh HD : Library : WebServer : Documents folder:

Webserver Documents folder in Finder

This folder already contains all the language versions of the default home page that is installed by Apache.

Viewing http://localhost in Safari will give you

http://localhost

If you are one of a number of people who log into your OS X machine, or you do not have the administrative rights to edit files in the Library : WebServer : Documents folder, you can store files in the Sites folder in of your home folder:

Home directory

To access these through a web browser you need to add ~youshortusername to the address in Safari. My account Short Name is allank, so I browse my machine as http://localhost/~allank/

Browsing home directory

The next thing to get up and running is MySQL.

MySQL
The latest version of MySQL for OS X is available to download as a disk image directly from the MySQL web site. Remember to grab the correct version for you OS X platform – if you are working off a MacBookPro you cannot use the PowerPC version. I’m working on a MacBookPro so I grabbed the mysql-standard-5.0.26-osx10.4-i686.dmg file from the site.

Mount the disk image and you’ll see that there are 4 files. To install just MySQL double click the mysql-standard-5.0.26-osx10.4-i686.pkg.

MySQL Disk Image

The installation will run through, just click through, selecting the volume that you want it installed on.

In order to control the startup of MySQL, double click the MySQL.prefPane file in the disk image. You’ll need the administrator password since the preferences pane is installed into the System Preferences panel.

MySQL Preferences Pane

PHP
To install PHP, hop onto the php.net site and grab the latest version of the source code. I grabbed php-5.1.6.tar.gz and uncompressed it onto my desktop.

The next steps require you to work in the Terminal program, and possibly to have XCode installed. XCode comes on your OS X install disk, so it’s not a problem if you find that you need it.

Fire up the Terminal application (Applications : Utilities : Terminal) and then change to the folder where you uncompressed the PHP source code. When you’re in the terminal you’ll be working in the Darwin directory structure that underlies your shiny Aqua GUI. I unpacked the source to the /Users/allank/Desktop/php-5.1.6 folder by issuing the

cd /Users/allank/Desktop/php-5.1.6

command. Spaces and case are important in the Terminal.

Now you need to run the configure script to set up the PHP source code before actually building it. Run the command:

./configure –with-apxs

apxs is an Apache tool that let’s you dynamically load modules into Apache – by include the –with-apxs switch to configure, we’re telling the configure script that when PHP is built, it must apxs to install it into Apache. If you get error messages that required tools are not installed it means that you need XCode before you can continue the install.

When the configure script returns you to the prompt, run the make command to actually build the binary versions of the PHP files:

make

If everything goes according to plan (and it should) the make command should complete successfully and drop you back to the prompt, ready to install. At this point the binary files have all been created and all you need to do now is put the files in the correct place. For this step you will need the admin password for the computer.

sudo make install

At this point most of the files are all living in the right places, there’s just a small bit of configuration that needs to happen.

In the same terminal, change to the /etc/httpd folder:

cd /etc/httpd

and edit the httpd.conf file. To do this you’ll need the admin password again.

The installation adds the necessary lines to load the PHP5 module, but the existing IfModule secdtion in the httpd.conf is for PHP4. Find the section in httpd.conf that looks like:


# If php is turned on, we repsect .php and .phps files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

# Since most users will want index.php to work we
# also automatically enable index.php

DirectoryIndex index.html index.php


and change it to read:


# If php is turned on, we repsect .php and .phps files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

# Since most users will want index.php to work we
# also automatically enable index.php

DirectoryIndex index.html index.php

All that has changed is the mod_php4.c to mod_php5.c

The last bit of work that we need to do in the terminal is provide PHP with a php.ini to use so that we can configure it. Using the sudo command copy the php.ini-dist file that is included in the PHP source archive to the /usr/local/lib folder on your machine, renaming it to php.ini. In the terminal you can do that with

sudo cp /Users/allank/Desktop/php-5.1.6/php.ini-dist /usr/local/lib/php.ini

Open the Sharing panel in the System Preferences and restart the Personal Web Sharing so that Apache now knows about the PHP that we just installed.

I created a file called index.php in my home Sites folder that contains the PHP code:

and then browsed to http://localhost/~allank/index.php to see that PHP is properly installed:

phpinfo()

Of course, MySQL is not included in PHP by default, so to have PHP connect to you MySQL database server you will need to compile it in.

In the terminal change back to the folder where your PHP source is, and execute the following commands:

make clean
./configure –with-apxs –with-mysql=/usr/local/mysql
make
sudo make install

You’ll then have to Start and Stop the Personal Web Sharing again so that Apache is now using your newly built version of PHP and you’ll be good to go with MySQL support included in PHP. If you want to add any additional modules into PHP you will have to re-compile PHP to include those modules.

phpinfo(), now with MySQL goodness

Advertisements

3 Comments »

  1. […] So then there I was with a new MacBookPro, new operating system and new opportunities for breaking stuff. And the first thing that I needed to set up was a development environment. (more…) […]

    Pingback by Hacks, Information, and More » Setting up a PHP/MySQL development environment under OS X — February 4, 2007 @ 5:52 pm | Reply

  2. Marc Liyanage’s prebuilt packages have long been a first stop shop for many people wanting a xampp-like installation of PHP for test-server use:

    http://www.entropy.ch/software/macosx/

    Good, if you also want to run Postgres, instead of MySQL

    He has some excellent Ruby stuff available, too.

    Comment by Daniel — March 5, 2007 @ 3:08 pm | Reply

  3. Yeah, Marc Liyanage’s stuff is great. I just prefer to have some semblance of control over what I’m installing. Incidentally there is also an xampp for OS X, but it’s not nearly as stable as the Windows version.

    Comment by mediafrenzy — March 21, 2007 @ 11:00 pm | Reply


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: