Turning on PHP

PHP is a fabulous scripting language for beginners to try their hands at serving up dynamic web content.

Much like server-side includes [Hack #94], PHP code is included and interpreted into the actual HTML of your web pages. Here, we’ll show you how to turn it on (it’s installed by default on OS X), as well as how to certify that it’s working properly.

As with the hacks on CGI and SSI, turning on PHP involves searching for the feature name (in this case, php) within the Apache configuration file [Hack #89]. The first entries we’ll run into are:

# LoadModule php4_module libexec/httpd/libphp4.so

...

# AddModule mod_php4.c

These two lines enable (or disable, if commented out with a #) the loading of PHP on Apache web-server startup. Since they’re commented out by default, we’ll have to uncomment them in order to have the modules loaded and PHP functional. Do so, and the lines should now look like this:

            LoadModule php4_module libexec/httpd/libphp4.so

...

AddModule mod_php4.c

Keep searching for php in the file and you’ll find:

# For example, the PHP 3.x module will typically use:
#
# AddType application/x-httpd-php3 .php3
# AddType application/x-httpd-php3-source .phps
#
# And for PHP 4.x, use:
#
# AddType application/x-httpd-php .php
# AddType application/x-httpd-php-source .phps

Tip

In some OS X installations (notably, 10.2 and higher), you won’t see the preceding lines. That’s alright; just add them in yourself.

In essence, these lines are saying that any file with ...

Get Mac OS X Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.