I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009


Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Thursday, December 4, 2008

Setting up PHP in Mac OS X

I tried out some PHP yesterday and I found out that OS X doesn't really come as an out-of-the-box MAMP (Mac-Apache-MySQL-PHP) server, especially on the PHP part. If you try running some server-side PHP code, you'll end up seeing the source code instead of having the actual program run. Here's how you can fix the Apache configuration to get PHP working on your Mac.

Some characters used here are only viewable on OS X.
It is recommended that you back up the original configuration file before modifying it.
Form A: Graphically
  1. In the Finder, press ⌘⇧G (Command-Shift-G), and type /etc/apache2 . Press Return or select Go.
  2. Change the permissions of the httpd.conf file. Select it, press ⌘I (Command-I), open the Sharing and Permissions pane, click the lock, and type in your password (if you have one). Change the privilege of "everyone" to "Read & Write."
  3. Open the file with a text editor like TextMate or Smultron (the latter is free). 
  4. Find the line that reads #LoadModule php5_module libexec/apache2/libphp5.so and uncomment it (remove the preceding #).
  5. After the following line, add the lines AddType application/x-httpd-php .php .phtml .php3 and  AddType application/x-httpd-php-source .phps to make PHP files recognizable.
  6. After saving and closing the file, go back to the Sharing & Permissions pane in the Info panel and change the privilege of "everyone" to "Read Only." Click the lock and close the Info panel. You're done!
Form B: In the Terminal
  1. Open Terminal.app (/Applications/Utilities)
  2. cd /etc/apache2
  3. sudo pico httpd.conf (or another text editor of choice)
  4. press ^W and enter php5
  5. See step 4 of Form A.
  6. ^X to quit, Y to confirm, and press Return. You're done!
Remember: PHP is a server-side language, not a client-side one, so you should, after enabling web sharing, at the very least open up your file by opening in your browser instead of just dragging in the PHP file. Don't forget to have web sharing turned on (System Preferences > Sharing).