Install Apache 2 and PHP 5 with MacPorts

4/2/08 - Added php.ini settings to use the proper MySQL socket at /tmp/mysql.sock.

3/23/08 - After going through these instructions on a new Leopard system, I made a few minor updates. The steps should now work for Leopard as well as Tiger.

While options abound, MacPorts may be the easiest option to configure a local web development environment on your Mac. I'll mention a few of the other options and then share the steps I've used to install Apache 2 and PHP 5 with MacPorts.

Here are a few of the options Mac users have to set up a PHP development environment:

Use the stock Apache 1.3 with a PHP package, like the one from www.entropy.ch

If you haven't already done so, install MacPorts. If you're going to develop site's against a database, might I suggest installing MySQL.

LightTPD: An Alternative to Apache

I've read great things about LightTPD, a light-weight HTTP server. LightTPD does not consume the CPU cycles or memory that Apache does and looks like an efficient alternative. I hope to try LightTPD sometime but for now I'll stick with Apache simply because I'm comfortable configuring and administering it.

If you have MacPorts and your database of choice installed, let's get started.

Install Apache 2

Important: To avoid conflicts with the stock Apache installed on your Mac, turn off Personal Web Sharing under the Sharing System Preferences pane. It's easy to switch back and forth between stock- and MacPorts-Apache instances, but running both at the same time, without changing network port settings, will cause problems.

There aren't many port variants for Apache 2, and those that exist are probably not needed by most developers. The variants are:

+openldap - Provides LDAP authentication features.

+preforkmpm, +workermpm, +eventmpm - I grouped these together because they're related to server process management. Review Apache documentation on each if you're building a server to handle a lot of traffic.

+no_startupitem - Include this if you don't need to start Apache at system boot.

Install Apache 2 by issuing the following in a Terminal window (add variants, if desired):

sudo port install apache2

Here's the tail of my installation's output.

###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
###########################################################
Warning: apache2 requests to install files outside the common directory structure!
---> Installing apache2 2.2.6_0
---> Activating apache2 2.2.6_0
---> Cleaning apache2

Install PHP 5

MacPorts enables several key extensions by default, including curl, freetype, jpeg, libpng, libmcrypt, libxml2, libxslt, mhash, and tiff. In addition to these extensions you may add the following variants:

As some may already recognize, support for for Oracle, Sybase, dBase, and other less popular extensions is missing. Try searching the MacPorts list archives, I've seen discussions detailing the use of patches to add other extensions.

Before installing PHP, if you started Apache 2, be sure to stop it. To install the latest version of PHP 5 (add desired variants):

Configure Apache and PHP 5

Set Apache's Document Root, Enable User Directories

Are you the only one using the server or are you collaborating with others? If multiple developers need access, should each have their own sandbox or will the team develop in a single directory? Are there existing pages and scripts in Apple's default Apache document root directory (/Library/WebServer/Documents) and do you want to continue developing in this directory? Your answers to these questions will determine which edits you'll make to your httpd.conf file.

Open /opt/local/apache2/conf/httpd.conf in your favorite text editor.

sudo pico /opt/local/apache2/conf/httpd.conf

If you want to change the default MacPorts Apache document root to Apple's, look for:

DocumentRoot "/opt/local/apache2/htdocs"

and change it to

DocumentRoot "/Library/WebServer/Documents"

If you changed the DocumentRoot, change the Directory directive

<Directory "/opt/local/apache2/htdocs">

to

<Directory "/Library/WebServer/Documents">

Add index.php to the dir_module directive:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the <IfModule mime_module> block. Without this, all you'll see is the text of your PHP scripts