Tutorial: Apache2 + PHP5 + Extensions on MAC OS X

Since we installed MacPorts, let's put it to good use and install Apache2 and PHP5 to replace versions bundled with OS X.

To make PHP database aware and more useful, we will be installing Mysql, PostgreSQL, SQLite and PEAR as extensions, along with many others built by default by MacPorts.

First things first, sync the ports

$ sudo port sync

In which variants can Apache be built?

$ port variants apache2
apache2 has the variants
universal
darwin
darwin_7
openbsd
openldap
preforkmpm
workermpm
eventmpm
no_startupitem

I'll go for OpenBSD variant

$ sudo port install apache2 +openbsd

After install completes it's time for some configuration, if you wish to have Apache startup with the OS follow the screen instructions.

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
I like to start it only when i need it, so the line above doesn't apply, and later we will see how to start Apache from Personal Web Sharing

Installation is complete and we should be able to start Apache,

$ sudo /opt/local/apache2/bin/apachectl start

You should try to browse http://localhost and it should work already.

All good? Let's stop it,

$ sudo /opt/local/apache2/bin/apachectl stop

Apache all done, let's head on to PHP, check the variants of the PHP package if you need.

We should install MySQL first, if we intend to use MySQL as server later. Why? because MacPorts does not yet provide two separate ports for MySQL (client/server) as it does to PostgreSQL.

$ sudo port install mysql5 +server

On to installation, we will be installing a bunch of variants (features), so, to make PHP ready for database access and development, modify accordingly to fit your needs.

$ sudo port install php5 +apache2 +mysql5 +postgresql +sqlite +pear

It will take a while, go drink a coffee, make a sandwich, conquer the world, whatever!

All done, let's now activate PHP, and make a php.ini available

$ cd /opt/local/apache2/modules
$ /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
$ cd /opt/local/etc
$ sudo cp php.ini-recommended php.ini

Add the following lines to /opt/local/apache2/conf/httpd.conf

Include conf/extras-conf/mod_php.conf

look for,


  DirectoryIndex index.html

change to,

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

to get functionality like http://localhost/~username this will look into your Sites folder, uncomment

# User home directories
Include conf/extra/httpd-userdir.conf

look into /opt/local/apache2/conf/extra/httpd-userdir.conf, and change to suite your needs, the configuration should be uncommented.

PHP should be working, let's test it, create a file, test.php with the following contents,

<?php
phpinfo();
?>
start Apache and browse to http://localhost/~username/test.php, the php info page.

For the last part, making possible to start and stop Apache from System Preferences, it's very easy, we just need to make OS X to think it's taking care of the old Apache.

Do the following,

$ cd /usr/sbin/
$ sudo mv apachectl apachectl-1.3
$ sudo ln -s /opt/local/apache2/bin/apachectl 
$ ls -l apachectl
and you should see apachectl pointing to /opt/local/apache2/bin/apachectl

To finish, open /opt/local/apache2/conf/httpd.conf and look for PidFile and make sure it contains the following path /var/run/httpd.pid, save it and stop Apache.

We should be able to start Apache from System Preferences -> Sharing -> (check) Personal Web Sharing now you can start and stop apache from here or from command line with apachectl start or stop, or even at system boot.

System Preferences

Send comments, suggestions, corrections, all appreciated!

That's all!