PHP 5 & Apache 2 installation on OSX with MacPorts

Now that MacPorts 1.7.x is finally out I thought I'd re-new my MacPorts installation and document the installation procedure for a basic PHP 5 and Apache 2 setup.   Let me know if anything below fails but it worked fine for me today (20 December 2008).
Pre-requisites
Read the installing MacPorts guide and get all the pre-requisites installed, such as X11, X11User/SDK, XCode.
Steps
Install the MacPorts DMG for your specific version of Mac OSX.   In my instance I was using the Leopard installer.
sudo port -v selfupdate
sudo port install php5 +apache2
Go and find something to do for about an hour whilst the installation takes place… During the install you'll have noticed the following displayed in the 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 ########################################################
It's your choice if you want to execute that or not.  I don't personally like Apache2 automatically starting every time I boot my machine so I start and stop in manually.  If you want Apache2 to start whenever you boot then use the command above. At the end of the install log you'll have also noticed the following displayed…
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
Ignore that for now, we need to do a couple of extra things first…
sudo cp /opt/local/etc/php.ini-recommended /opt/local/etc/php.ini
sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf
Then we want to use the command mentioned above, but through sudo…
cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n “php5” libphp5.so
Add the following two lines to the end of /opt/local/apache2/conf/httpd.conf
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
Start Apache with…
sudo /opt/local/apache2/bin/apachectl start
Check it works by firing up your web browser and going to http://localhost.  You should see “It works!”. Create a file, phpinfo.php, in /opt/local/apache2/htdocs with the following content…
<?php phpinfo.php; ?>
Try to access it at http://localhost/phpinfo.php and hopefully it'll show you your PHP configuration within Apache.  If it does then everything is configured correctly, if not then retry the steps above!
Getting things to launch at boot time
If you want Apache and Memcache to load at boot time then use the following:-
sudo launchctl load -w /Library/LaunchDaemons/org.macports.memcached.plist sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
Optional installation bits
At work I use XDebug, PHPYaz and Memcache as well.  I'll document these some other time.

Leave a Reply

Your email address will not be published. Required fields are marked *