Being a developer and today being my trip to the dentist (and a day off work), I tend to get bored really really easy.
And knowing tomorrow I've got to work with JSON I thought I'd show you how easy it is to install and compile Apache 2.2 Web Server and Enable PHP 4 on your MacBook, so you can get to and build PHP apps natively on OSX.
Now obviously you could download MAMP to get up and running fast, but really that's just not geeky enough for us hard-core developers, so let's begin.
Apple Developer Tools Required
Yes, to build from source you're going to need the GCC compiler which can be obtained for the Apple Developer Connection website (free), search my searchbox for a link otherwise just download from their site.
Apache 2.2
Installing Apache 2.2 Web Server
First goto the Apache site and download the latest sourcecode to your desktop.
Once downloaded, do the following in your TERMINAL window;
cd desktop
gnutar -xzf httpd-2.2.6.tar.gz
cd httpd-2.2.6
Now we've untar'd it to the desktop and have navigated into it, let's build from source.
Run this to setup the sourcefiles for your machine and build spec,
./configure --enable-module=most --enable-shared=max
Now compile the sourcecode, and install
make
sudo make install
Bingo, you now have Apache 2.2 (or whatever version your downloaded) installed on your MacBook.
Starting & Stopping Apache2
Start with...
sudo /usr/local/apache2/bin/apachectl restart
Stop with...
sudo /usr/local/apache2/bin/apachectl stop
Running PHP on OSX
Enabling PHP 4 ?
Now every MacBook comes with PHP4 installed as standard, it just waits until you enable it to work.
So to enable it, type in TERMINAL;
cd /etc/httpd
sudo mate httpd.conf
This will navigate to the httpd Apache config file and open it's INI file in TextMate (I'm baised but you can use NANO or TextEdit if you like to edit the file).
When you've got the file open, search for 'PHP' anywhere in the file.
You should see two occurences, commented out with #'s remove these to enable PHP.
# LoadModule php4_module libexec/httpd/libphp4.so
# AddModule mod_php4.c
Now save and close the file
And restart Apache
sudo apachectl restart
Stop with...
sudo apachectl stop
Hang on this isn't Apache 2.2?
Now what this will do is startup your Mac's native Apache server (which isn't 2.2), sorry but I haven't worked that one out.
Your fresh 2.2 build will also have an *httpd.conf file in...
cd /usr/local/apache2/conf
sudo mate httpd.conf
To edit it's config file, sorry I haven't an answer at the moment but I'll figure it out.
Next question,
Where to stick my WebPages?
First make sure Personal Web Sharing is enabled in System Preferences / Sharing.
Next in your user directory (or the first directory you appear in when opening TERMINAL), create a directory called...
mkdir Sites
Now everything you stick in this directory will be acted upon like it's a website (including PHP).
To test your PHP setup works, create a file in there called test.php and add this in it...
<?php phpinfo() ?>
Now before you leave System Preferences / Sharing, click the info link by Personal Web Sharing.
This will display a dialog and at the bottom of this will be some web addresses, the one's for the web server running on your machine!
Click on one and bookmark the link.
Add /test.php to the end and run it in Firefox or Safari, you should now see the version of PHP installed on your system.
Well done, you have now successfully installed PHP and Apache2 from source on your MacBook.
Enjoy and when I can figure out how to replace Apple's native 1.3 Apache server with 2.2 I'll let you know, but for now it works.