Running Linux, 4th Ed.Running Linux, 4th Ed.Search this book

18.2. PHP

In order to complete our combo of Linux, Apache, PHP, and MySQL, we still need the PHP language interpreter. PHP is a recursive acronym that expands to PHP:Hypertext Preprocessor. It has been in development for several years now; the versions most commonly used are Version 3 and Version 4. We will use PHP4 for this chapter, as it was the most current stable version at the time of this writing.

18.2.1. Some Sample PHP

One of the nice things about PHP is that PHP code can be entered directly into HTML code. The web server will pass everything between <?php and ?> to the PHP module, which will interpret and execute the commands. Here is a very simple example for some PHP code in an HTML page; if you already have set up PHP, you could run this directly from your web server (if not, we'll tell you how to set up PHP below):

<html>
<body>
<?php
echo "Hi,";
?>
LAMP enthusiasts.
</body>
</html>

As you probably already have expected, your browser will output the following text:

Hi, LAMP enthusiasts.

This extremely simple example shows how Apache works together with the PHP interpreter: the code in <?php and ?> is passed to the PHP interpreter, which executes the echo command, which in turn outputs its parameters to the web browser. In addition to this, the line LAMP enthusiasts is simply added as ordinary HTML text (and since it doesn't have any markup, it doesn't look like HTML).

Of course, PHP can do much more. Like most programming languages, it can use variables and make decisions, as in the following script (we'll leave out the HTML framework here for brevity):

<?php
echo "Dear friends, today's date is: ";
echo date("F d, Y")."\n";
echo "<br>";
echo "We are in the ";

if ( date ("m") <= 6 ) {
        echo "first ";
        } else {
        echo "second ";
}
echo "half of the year ".date("Y");
?>

You have probably already guessed that this script bases its decision in the if statement depending on the current month. Notice that we have used an HTML tag (<br>) in the PHP output; this is completely acceptable and a very common technique when using PHP. Your web browser will receive the following data (of course, with other dates, unless your computer clock goes wrong or you were trapped in a time warp):

Dear friends, today's date is: May 04, 2002
<br>We are in the first half of the year of 2002

The web browser will then break the line at the position of the <br> tab.

In order to modularize your code, PHP also supports functions, as do most other programming languages. These functions enable you to execute a piece of code in many different places without having to rewrite it over and over again.

NOTE: PHP comes with very extensive function libraries, and you can download even more from the Net. In order to include a function library, you can use the include( ) and the require( ) statements, which differ only marginally.

If you want to program with PHP, you should familiarize yourself with the documentation of the function libraries that are shipped together with the PHP interpreter, since their use means you do not have to reinvent the wheel when performing common tasks.

Here is the definition of two simple functions — show_date, which outputs the current date in a hardcoded date format and appends a line break, and show_halfyear, which outputs first or second depending on the current month:

<?php

function show_date( )}

echo  date("F d, Y")."\n <br>";
{

function show_semester( ) {
if (date ("m")<=6){
        echo "first ";
        } else {
        echo "second ";
}

?>

Let's call this script functions.php and rewrite our initial script using these functions:

<?php

require(functions.php);
echo "Dear friends, the date today is: ";

show_date( );

echo "<br>";
echo "We are in the ";

show_semester( );

echo "semester of ".date("Y");
?>

The require( ) statement tells the PHP interpreter to load our function script and make the functions contained therein available to the current script.

Of course, we have only scratched the surface of what PHP can do. If this has whetted your appetite, you might want to look into Programming PHP by Rasmus Lerdorf, the original author of PHP, and Kevin Tatroe (O'Reilly).

Until PHP3, PHP was an interpreted language, the code of which was kept in a buffer. Loops and other often-run pieces of code were parsed over and over again before executing the code. Of course, this led to somewhat suboptimal performance.

PHP4 is a complete rewrite and consists of the language core (called "Zend") and the function modules (which are very flexible and extensible). Unlike PHP3, PHP4 can be used in multithreaded environments, which also makes it possible to use PHP as a module in various web servers.

In addition to running PHP4 as a module, you can also run it as a CGI program started by the web server, at the expense of some additional overhead. When running PHP as a CGI program, each new page that contains PHP code requires starting a new instance of the PHP interpreter, which in turn requires creating a new process and loading the PHP interpreter into this process. When the interpreter is finished creating the page, its process ends, the memory is freed, all file handles are closed, and all database connections are shut down.

As a web server module, the PHP interpreter becomes part of the web server and is always loaded into memory. In addition, it can keep resources like database connections alive across different pages, which can bring huge performance benefits.

All big-ticket PHP sites use PHP as a module, mostly because of the better performance it affords.

18.2.2. PHP4 as an Apache Module

As we have already said, running the PHP interpreter as a web server module is best for getting good performance. Today, most distributions (including Slackware, Debian, SuSE, and RedHat) ship both Apache and the PHP4 module for Apache, so it is generally not necessary to build the PHP4 module yourself. It may be a good idea to do so anyway, however.

Because of its vast amount of functionality, the PHP4 module needs quite a number of additional libraries or modules. If you install the module from your installation CDs, the installation program will have automatically installed the necessary modules. However, the modules shipped with distributions are typically loaded with functionality to satisfy all needs and tastes. The result can be a system that's heavier and slower than it needs to be.

Thus, the advantage of building PHP4 by yourself is that you can decide which functionality you want to go into the module. Check the documentation to see which additional libraries you might need to install.

Since we firmly believe that you should know what goes on behind the scenes, even if you use the more comfortable ready-made solutions, we will give you some hints regarding how to work from scratch and how the pieces work together.

In order to load the PHP4 module into the Apache web server at runtime, you need to have the Apache module mod_so. You can check whether this module is presented by issuing:

owl$ httpd -l
Compiled-in modules:
        http_core.c
        mod_so.c

If this module is not available, please check whether you may have missed installing some of the additional Apache packages in your distribution. If you have built Apache from scratch, follow the documentation to get this module.

It is also possible to compile the PHP4 module directly into Apache, but this requires some very intertwined building of both Apache and PHP4 and does not really give you a big advantage, so we won't cover this here.

Now we need to build PHP and make a Dynamic Shared Object (DSO) out of it. Luckily, this is not as involved as it sounds. Download PHP4 from http://www.php.net/download.php. You will end up with a package called php-4.1.1.tar.gz (the actual version number may differ slightly). Unpack the tar file and configure PHP with:

owl ./configure \
                 --with-mysql=/usr/lib/mysql\
                 --with-ldap=yes\
                 --with-gd=yes\
                 --with-zlib=yes\
                 --with-config-file-path=/etc/\
                 --with-apxs=/usr/lib/apache/apxs\
                 --enable-versioning\
                 --enable-track-vars\
                 --enable-thread-safety

You can read about numerous additional options in the extensive PHP documentation, but for starters, this will do. Note that you might need to replace some of the paths here with the actual locations on your system. After configure is finished, do a make and then a make install to install PHP (you may need to do the make install as root).

Next, edit the httpd.conf file, Apache's configuration file. If you have installed Apache from your installation CDs, chances are the following lines are already there and you just need to uncomment them. In any case, you should have the following lines in your httpd.conf:

LoadModule php4_module libexec/libphp4.so
AddModule mod_php4.c

AddType application/x-httpd-php .php

Now restart Apache:

owl$ apachectl restart

Once the server is restarted, you should test whether the PHP4 module can be loaded correctly. You can do this by writing a small PHP program, such as:

<?php
phpinfo( );
?>

Save this file as phpinfo.php in the htdocs directory of your Apache installation (often /usr/local/httpd/htdocs) and make it executable with chmod. Now you should be able to browse this file with your web browser by accessing http://localhost/info.php. If everything is OK, you should see the configuration of the PHP4 module.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.