As you probably already know, Mac OS X comes with Apache and PHP preinstalled, all you have to do in order to turn it on is to turn Web Sharing On in System Preferences and do some changes in httpd.conf/php.ini files.

You can probably find tons of tutorials on that topic (try this one for example) so not gonna waste my time on explaining how to do it.
Problem with bundled PHP/Apache setup on Mac is lack of flexibility. You already have most of extensions you’d ever need enabled, but there are still some missing, and if you want something not already there, you’re out of luck. For example recently I’ve started working on new project which requires memcache and xdebug support and there isn’t any way to have them enabled in bundled Apache/PHP setup.
Mac Ports to the Rescue
——————————————
In my previous post I’ve explained how to setup MacPorts on Mac OS X. Now we’re going to use it in order to setup fully working and flexible “LAMP” environment. Speaking of LAMP, I assume that you already have MySQL installed which is really easy to do with pkg installer available at http://dev.mysql.com/downloads/mysql/ (just download DMG Archive, unpack and run installer).
We will start our port setup by installing Apache 2. So open terminal and type this:
sudo port install apache2
After Apache is successfully installed we will deal with PHP 5 part. There are many extensions that we may get installed, but great thing with MacPorts is that you don’t have to install all of them at once, it’s quite okay to install them later. So we’re going to start with really basic PHP 5 setup by running this cmd in terminal:
sudo port install php5 +apache2 +mysql5
and you’ll get really long output which tale looks like this:
...
Warning: php5 installs files outside the common directory structure.
---> Installing php5 @5.3.8_0+apache2
---> Activating php5 @5.3.8_0+apache2
To customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.
If this is your first install, you need to activate PHP in your web server.
To enable PHP in Apache, run
cd /opt/local/apache2/modules
/opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
---> Cleaning php5
Dragan-Dinics-MacBook-Pro:~ dinke$
As you can see at the end of output, we also need to run some cmd’s in order to enable PHP in Apache, so execute these in terminal:
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n “php5” libphp5.so
and you’ll get output as bellow:
Dragan-Dinics-MacBook-Pro:~ dinke$ cd /opt/local/apache2/modules
Dragan-Dinics-MacBook-Pro:modules dinke$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
[activating module `php5' in /opt/local/apache2/conf/httpd.conf]
Dragan-Dinics-MacBook-Pro:~ dinke$
Now we need to edit /opt/local/apache2/conf/httpd.conf and tweaks it to our needs. You will probably want to change default DocumentRoot, for example my Doc root is under /Users/dinke/htdocs so on my Mac it’s set like this:
DocumentRoot "/Users/dinke/htdocs"
Note: If you change DocumentRoot path don’t forget to change path in Directory directive as well (should be the same as path in DocumentRoot).
Make sure that that in LoadModule section you have proper PHP modul:
LoadModule php5_module modules/libphp5.so
and last but not least, add proper entries in block to tell Apache to use PHP module for processing php files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
That should be all regarding Apache conf.
Now copy PHP.ini file to proper location:
sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
and edit it if you want now or leave it for later, up to you.
Time to start Apache now! Let’s run this cmd:
sudo /opt/local/apache2/bin/apachectl start
and if you don’t get any error message, that means that Apache is up and running. If you do, check again your config and make sure that you don’t have any errors.
Now create usual phpinfo() page and save it in your docroot (/Users/dinke/htdocs/test.php in my example), and try to see if it works:
http://localhost/test.php

If you get usual PHPinfo page simmilar as above, your installation was successful. Well done!
Now what is great with MacPorts setup is that you can now add more extensions easily. For example if you search for php5 extensions you will get 111 ports!
sudo port search php5
php5 @5.3.8 (lang, php, www)
PHP: Hypertext Preprocessor
php5-amf @0.9.2 (php, devel)
ActionScript Message Format extension
php5-apc @3.1.9 (php, devel)
Alternative PHP Cache
php5-bbcode @1.0.2 (php, devel)
BBCode parsing Extension
...
Found 111 ports.
So for example if you want mcrypt module installed, all you have to do is to run this:
sudo port install php5-mcrypt
and you’ll get output like this:
Dragan-Dinics-MacBook-Pro:~ dinke$ sudo port install php5-mcrypt
---> Computing dependencies for php5-mcrypt
---> Dependencies to be installed: libmcrypt
---> Fetching archive for libmcrypt
---> Attempting to fetch libmcrypt-2.5.8_1.darwin_10.x86_64.tbz2 from http://packages.macports.org/libmcrypt
---> Attempting to fetch libmcrypt-2.5.8_1.darwin_10.x86_64.tbz2.rmd160 from http://packages.macports.org/libmcrypt
---> Installing libmcrypt @2.5.8_1
---> Activating libmcrypt @2.5.8_1
---> Cleaning libmcrypt
---> Fetching archive for php5-mcrypt
---> Attempting to fetch php5-mcrypt-5.3.8_0.darwin_10.x86_64.tbz2 from http://packages.macports.org/php5-mcrypt
---> Fetching php5-mcrypt
---> Verifying checksum(s) for php5-mcrypt
---> Extracting php5-mcrypt
---> Configuring php5-mcrypt
---> Building php5-mcrypt
---> Staging php5-mcrypt into destroot
---> Installing php5-mcrypt @5.3.8_0
---> Activating php5-mcrypt @5.3.8_0
---> Cleaning php5-mcrypt
Now restart Apache, and you should be able to see mcrypt module activated on phpinfo page. Enjoy!
