This posts is one of those "note to self" kinda posts. I just finished installing PHP and Apache on my Mac OS 10.5.6 and though I should document the experience should I (or you) need to do it again.
It could already be there
The default OS install came with goodies like ruby and php already there. So I could use php on the command line already. But it wasn't "hooked" to Apache for proper web development.
Also turns out Mac comes with some version of Apache, looks like it's disabled by default, but if it isn't, disable it from System Preferences / Sharing / Web Sharing.
Now let's start fresh with PHP5 and Apache 2, ignoring the PHP that's already there.
Mac ports prerequisite
Mac ports makes installing many software packages a breeze on the Mac. If you don't have it already, do set up Mac ports first.
Ready? Set? Go
- Log into the mac ports prompt:
$ sudo port Password: MacPorts 1.700 Entering interactive mode... ("help" for help, "quit" to quit) [Users/stoyan] >
- inside the Mac ports prompt simply do:
[Users/stoyan] > install php5
- This is it! Give it a bit of time to pull out all dependencies, including Apache2. The rest is just some configuration...
- Start Apache and make it start when you power on the computer next time:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
- Test that Apache runs fine by pointing your browser to http://localhost/. You should see a page that says "It works!"
- Tell Apache that PHP exists:
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so [activating module `php5' in /opt/local/apache2/conf/httpd.conf]
- Create a php.ini file (PHP configuration) by copying the default .ini
sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
- If you need Apache stuff, like config files, error/access logs, htdocs... look around
/opt/local/apache2. The web root for example is
/opt/local/apache2/htdocs. I found it kinda convoluted so decided to move the web root to my home directory. So next two steps are optional.
- I'll store all web apps and pages and scripts in a directory called /localhost in my home directory:
- Time to edit the Apache config to tell it about the new location of the web root. Open httpd.conf
sudo vi /opt/local/apache2/conf/httpd.conf
Search for "DocumentRoot" and replace the current value
The result would be like:
Then do the same further down in the config where it says
<Directory..., so it should read:
# # This should be changed to whatever you set DocumentRoot to. # <Directory "/Users/stoyan/localhost">
- Apache also need to know that files that end with .php will be handled by the php module, so edit httpd.conf (the same one from the previous step). Search for "php" and you'll find:
LoadModule php5_module modules/libphp5.so
Add one more line so it looks like:
LoadModule php5_module modules/libphp5.so AddHandler application/x-httpd-php .php
- restart Apache (see below) and you're all done
Here's how you start/stop/restart Apache:
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper stop
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart
Or to stop and disable starting up every time you power on:
$ sudo launchctl unload -w /Library/LaunchDaemons/org.macports.apache2.plist
Verify that all is good
You can check that all is good by creating a PHP info script.
echo "<?php phpinfo(); ?>" > ~/localhost/test.php
Now point your browser to http://localhost/test.php. It should give you the php info page (a looong page of PHP-related information)