Sunday, March 23, 2008

Setting up Apache and PHP on Mac OS Leopard

Apache is a fairly straight forward setup if you use some utility or distribution to help you. A manual installation will take more time. You can Google installation instructions or use a utility like XAMPP to simplify the installation. XAMPP is a distribution that will walk you through an Apache, MySQL, PHP, phpMyAdmin installation. Once you get comfortable with XAMPP these software components can be installed on a Linux or Windows platform in a few minutes.

I'm running Mac OS (Leopard) which comes with a built-in Apache web server (2.2.6) and PHP (5.2.4) preinstalled. On Mac OS (Leopard), perform the following steps to get the Apache Web Server (AWS) and PHP up and running in minutes.
  1. Go to System Preferences. Under Internet and Network select the Sharing icon.
  2. Select the checkbox under Web Sharing. It will return your computer's website URL (IP address or host name) (i.e. This will also start AWS.
  3. Open up a browser and type your computer's website URL displayed in the Web Sharing page in System Preferences. After typing this URL in a browser. You should get a browser page that shows you your Apache web server is up and running. This is too cool. Mac OS's built in Apache web server takes just a few clicks to have Apache up and running.
  4. Once you've confirmed the Apache web server is up and running, type the httpd -V command to get information on your Apache installation.
Running httpd -V.
$ httpd -V

The httpd -V command returns important information
  • The Apache Web Server version you are running.
  • The location of the Apache configuration file (httpd.conf).
  • The location of the access and error log file.
  • Apache configuration options.
Example snippet of output from the httpd -V command:
Server version: Apache/2.2.6 (Unix)
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
-D DEFAULT_ERRORLOG="logs/error_log"

With Mac OS (Leopard) you can find the access_log and error_log files in the following location:

Turn on PHP 5 on Leopard by uncommenting the following line in your httpd.conf file using a text editor.
# LoadModule php5_module libexec/apache2/

AWS needs to be stopped and restarted for the changes to take effect. Starting and stopping the AWS can be done through the System Preferences | Sharing window. Clicking the Web Sharing checkbox can be used to stop and start Apache. Restart the Apache Web Server so the configuration changes take effect.

Create a PHP configuration file. Then change the error reporting option to display all errors. Instructions:
$ cd /private/etc
$ sudo cp php.ini.default php.ini
$ sudo vi php.ini
Change the error reporting line using vi or a text editor to display all error messages.
error_reporting = E_ALL & -E_NOTICE
error_reporting = E_ALL

Test your PHP configuration
  1. In a browser type your computer's website displayed in the Sharing page. This should launch Safari and display an Apache web page.
  2. Create a page called test.php (lots of examples on Google). Save it in the directory Macintosh HD:Library:WebServer:Documents.
  3. Go to your browser and type your computer URL with a test php (lots on Google) program. If you get the PHP info your configuration is working.

Apache and PHP should now be up and running on your Mac. Have fun!

No comments: