Hack 93 Turning on PHP

figs/expert.giffigs/hack93.gif

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

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 the .php extension should be processed by the PHP module we just enabled. As we'll see soon enough, Mac OS X (Versions 10.1 and above) comes preinstalled with PHP 4, so go ahead and uncomment the two lines for PHP 4.x, like so:

# 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

Save the Apache configuration file, and restart the web server:

% sudo apachectl restart httpd restarted

We're going to return to our Apache error log for a second to illustrate a simple, yet helpful, bit of information. Each time you start Apache, it will spit out a single line telling you that everything has started successfully. With a plain-vanilla Apache server, it usually looks something like this:

[notice] Apache/1.3.20 (Darwin) configured -- resuming normal operations

When you add a third-party module or feature (like PHP, mod_perl, mod_ssl, etc.), Apache will graciously make mention of it in its startup line. If you just restarted the Apache web server now, take a look at the error log by typing:

% tail /var/log/httpd/error_log

You should see Apache wax poetic with:

[notice] Apache/1.3.20 (Darwin) PHP/4.0.6 configured -- resuming normal operations

Apache tells us that PHP is enabled, but how do we really know for sure? Rather easily, actually. Create a file named index.php in your Sites directory using the following as its contents:

<html> <body> <h1>Gleefully Served By Mac OS X</h1> <? phpinfo(  )?> </body> </html>

Load index.php in your browser (http://127.0.0.1/~morbus/index.php for me) and you should see a long page full of PHP diagnostic information. PHP has been successfully configured for use.



Mac OS X Hacks
Mac OS X Hacks: 100 Industrial-Strength Tips & Tricks
ISBN: 0596004605
EAN: 2147483647
Year: 2006
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net