Getting the pre-complied PHP Apache Module from Marc Liyanage’s site was always the preferred way to get a fresh PHP distribution on your Mac. However, creating 4-way binaries (32- and 64-bit versions for G4/G5 and Intel CoreDuo / Core-2-Duo processors) seems to be broken and if Apache is running in the more efficient 64-bit mode, installing the PHP module not only doesn’t work, it will also break Apache. However, there is an easier way to get PHP working with your Apache Server on the Mac.
Starting Apache is as easy as opening System Preferences – Sharing and checking the box next to Web Sharing. After that’s done, open your Web browser and browser to http://127.0.0.1 (or http://localhost, or http://computername.local) to see the global Apache start page or go to http://127.0.0.1/~username (or http://localhost/~username, or http://computername.local/~username) to browse the content of your own site folder (/Users/username/Sites).
To enable PHP you need to edit Apache’s protected configuration file, which can be done using the terminal or an editor application like the free TextWrangler.
- Using the Terminal, you want to backup the file before editing:
sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bup
- Let’s verify that the PHP extension mappings are available:
sudo cat /etc/apache2/other/php5.conf
- Finallly, lets edit the httpd.conf file:
sudo pico /etc/apache2/httpd.conf
Scroll down to this line:
#LoadModule php5_module libexec/apache2/libphp5.so
and remove to ‘#’ character before saving and exiting the editor with ctrl-O following by ctrl-x
- To read the configuration again, Apache needs the be restarted. Go back into the System Preferences – Sharing and un-check and then re-checkWeb Sharing checkbox.
Apache With PHP
To see it all working, create a new text file in your site folder (/Users/username/Sites) and name it phpinfo.php
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 TRANSITIONAL//EN”>
<?php echo phpinfo(); ?>
.. and browse to http://127.0.0.1/~username/phpinfo.php
Enable PHP Code In HTML
Enabling PHP code interpretation/pre-processing in HTML files, has a minor performance impact on your server, but allows you to write PHP code inside of regular HTML pages (htm or html extension).
To enable this, we simply have to add this line to the PHP extension mappings, whose availability we had checked earlier.
Back in terminal enter:
sudo pico /etc/apache2/other/php5.conf
Before saving with Ctrl-O, the configuration file should look something like this:
AddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php