Have you ever wanted to add a guest book to your web site? Or show a
web counter? Well, PHP is
just the ticket to get started. PHP (short for PHP:
Hypertext Processor, yet another geeky recursive name) is
a widely used scripting language that is designed from the ground up
to be used in web pages. When you include PHP code in a page, it is
interpreted by the web server when the page is requested. The code
itself doesn't make it to the user, but the result
of it does. This allows you to put all sorts of dynamic content on
your web site.
There are lots of online guides and books out there to teach you how
to use PHP, but to get started, you must have PHP. Most guides start
out by telling you to download and compile the PHP distribution.
Well, there's no need for that, because
it's already built into Mac OS X. All you need to do
is hunt down two lines in your Apache configuration and uncomment
them. These are the two lines to look for in
/etc/httpd/httpd.conf:
# LoadModule php4_module libexec/httpd/libphp4.so
...
# AddModule mod_php4.c
Since these lines are commented out by default,
we'll have to uncomment them in order to make PHP
functional. Do so, and the lines should now look like this:
LoadModule php4_module libexec/httpd/libphp4.so
...
AddModule mod_php4.c
Once you make this change, the following configuration block in your
httpd.conf file takes care of the rest of the
necessary configuration, including setting the php
file type handler and adding index.php
to the list of index pages used:
<IfModule mod_php4.c>
# If php is turned on, we repsect .php and .phps files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# Since most users will want index.php to work we
# also automatically enable index.php
<IfModule mod_dir.c>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
Save the Apache configuration file, and restart the web server:
$ sudo apachectl restart
httpd restarted
Let's take a look at our Apache error log for a second to
illustrate a simple yet helpful bit of information. Each time you
start Apache, it spits out a single line that tells you everything
started successfully. With a plain-vanilla Apache server, the line
usually looks something like this:
[Wed Apr 14 23:53:56 2004] [notice] Apache/1.3.29 (Darwin) configured -- resuming
normal operations
When you add a third-party module or feature (such as PHP,
mod_perl, mod_ssl, etc.),
Apache graciously makes mention of it in this startup line. If you
just restarted the Apache web server now, take a look at the error
log by typing the following command:
$ tail /var/log/httpd/error_log
You should see Apache wax poetic with this line:
[Sat Apr 17 00:50:20 2004] [notice] Apache/1.3.29 (Darwin) PHP/4.3.2 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:
<? phpinfo( )?>
When you load index.php
in
your browser (http://127.0.0.1/~morbus/index.php, for
example, replacing morbus with your short
username), you should see a long page full of PHP diagnostic
information. PHP has been successfully configured for use.
Now that you have enabled PHP, you're ready to dive
in and start playing. The
PHP web site (http://www.php.net) is a great place to
start.
—Kevin Hemenway and James Duncan
Davidson