Archive for November, 2006

How to run PHP on OS X within the Sites directory

Saturday, November 4th, 2006
As a web developer, I am constantly creating and/or editing php-based web sites on my Mac. I have my own web space to upload these files to and test my work, but the extra step of FTP'ing a file to my web server each time I wanted to see my edits was a large annoyance for me. I wanted to streamline my process and increase productivity.

I knew it was time to take advantage of the built in web server in OS X, using Apache and the PHP module that comes preinstalled. Since I keep all web sites in the default “Sites” directory, I wanted to use it as the directory which Apache served files from. Here is what I did, and how you should do it…

By default OS X ships with Apache and PHP, but PHP is disabled. In order to enable it you must do the following:
  1. Open up Terminal.app, and type the following: sudo vi /etc/httpd/httpd.conf
  2. You'll be asked for your root password, so type it in and you'll be brought into the httpd.conf file
  3. You have to uncomment two lines of code by removing the hash symbol in front of them. To quickly jump to these lines of code without having to scroll through the file, type each of the following commands, with the leading forward slash, and press enter: /LoadModule php4_module and /AddModule mod_php4.c
  4. To remove the hash symbols type i to go into Insert mode, and using your arrow keys, you can delete the hash symbols
  5. Once you're done deleting the hash symbols, press the escape key to leave Insert mode
Next, you'll want to tell Apache to serve files from your directory of choice. By default, it will serve only files placed into /Library/WebServer/Documents/, but I have always kept my sites in /Users/Marc/Sites/, and didn't really want to change where I keep them. Below I'll show you how to change the default directory to one of your choosing by using my personal choice as the example:
  1. If you're not inside httpd.conf, follow steps 1 & 2 above.
  2. To jump to the line of code we need to change, type the following command and press enter: /DocumentRoot
  3. Press i to go back into Insert mode, and using your arrow keys, position the cursor so you can delete the default path (usually /Library/WebServer/Documents/) and type in the new path to your desired directory. (Mine is /Users/Marc/Sites/ but replace “Marc” with your own username)
Finally, once that is done you need to save the file, close it, and restart Apache.
  1. To save & close the file, type in the following command and press enter: :wq. This will save & close the file in one action
  2. After completing the above step, the httpd.conf file is closed, but you are still inside Terminal.app. From here, it is easy to restart Apache by typing the following command: sudo apachectl graceful
That is it! If you followed the steps above, you should now have Apache running with the PHP module enabled, and any files you keep inside /Users/yourname/Sites/ will be visible in your web browser simply by typing http://localhost/.