Install Apache 2 and PHP 5 with MacPorts

PHP logo

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:

  1. Use the stock Apache 1.3 with a PHP package, like the one from www.entropy.ch
  2. Install everything but the kitchen sink with MAMP or XAMPP
  3. Use MacPorts to install Apache 2 and PHP 5

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:

  • +apache2 or +apache (Apache 1.3) - We're installing the apache2 module
  • +fastcgi - Probably not needed for development servers
  • +imap - IMAP email functions
  • +tidy - HTML cleanup utilities
  • +mssql - MS SQL Server support
  • +snmp or +macports_snmp - Network monitoring and management
  • +mysql3 - You're not still!?
  • +mysql4 - Better :)
  • +mysql5 - Now we're talkin'!
  • +postgresql - Nice transactional database.
  • +sqlite - Nice light-weight database engine
  • +ipc - Building a site with high-performance demands?
  • +pcntl - Unix process control functions
  • +pear - PHP extension manager, a must have.

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):

 

sudo port install php5 +apache2 +mysql5 +pear

 

The tail of my PHP install was:

 

--->  Attempting to fetch php-5.2.4.tar.bz2 from http://www.php.net/distributions/
--->  Verifying checksum(s) for php5
--->  Extracting php5
--->  Configuring php5
--->  Building php5 with target all
--->  Staging php5 into destroot
Warning: php5 requests to install files outside the common directory structure!
--->  Installing php5 5.2.4_1+apache2+darwin_8+macosx+mysql5+pear
If this is your first install, you might want
cd /opt/local/apache2/modules
/opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
* copy  /opt/local/etc/php.ini-dist to  /opt/local/etc/php.ini
--->  Activating php5 5.2.4_1+apache2+darwin_8+macosx+mysql5+pear
--->  Cleaning php5

 

Create an Apache Configuration File

MacPorts installs a sample Apache configuration file. Make a copy of this file with:

 

sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

 

Activate the PHP 5 Module

After installation is complete, activate the newly installed PHP module as directed with:

 

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
[activating module 'php5' in /opt/local/apache2/conf/httpd.conf] 

 

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

 

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

 

And finally, to enable user directories, uncomment:

 

Include conf/extra/httpd-userdir.conf

 

Save and close the httpd.conf file.

Create a php.ini File

Create an INI file from which you can set error display, file upload settings, and other PHP options.

 

sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini

 

If you've installed MySQL, tell PHP where it can find the MySQL socket. Set the following in the php.ini file:

 

mysql.default_socket = /tmp/mysql.sock

 

Starting and Stopping Apache

Adding aliases to your shell environment will make it easier to start and stop Apache. Edit your .profile or .bash_profile:

 

pico ~/.profile

 

Copy the following to add an alias to the apachectl binary. You can name the alias apache2ctl to avoid potential conflicts with the stock apachectl binary

 

alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'

 

Reload your profile or bash_profile:

 

source ~/.profile

 

As noted previously in the Apache install output, you can configure Apache to start at system boot with:

 

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

 

Try It!

Fire up Apache with:

 

apache2ctl start

 

Drop the following in a PHP file and save it your document root or user directory.

 

<?php
phpinfo();
?>

 

You should see something like the following in your browser.

PHP Info display

Conclusion

You're all set to start writing PHP applications on your Mac. Take a look at the LightTPD link below if you're interested in an alternative, you should be able to substitute the Ruby steps with PHP.

Related Links

Share