Your Daily Source for Apache News and Information |
Breaking News | Preferences | Contribute | Triggers | Link Us | Search | About |
\n'); } if ( plugin ) { document.write(' '); } else if (!(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0)){ document.write(''); } //-->
|
By The technology that supports the Web continues to evolve, and one of the latest mutations involves capitalising on its very user-driven interactivity. The days of all-static content are past; the Web has evolved to a point at which many sites actually remember personal preferences for each of their (potentially millions) of visitors. News sites may display stories in only those categories you find interesting; online music stores can provide you with listings of new works sorted in order by your favourite artists; Web search engines can learn to implicitly restrict the types of content they'll list for you. The possibilities are endless, and the key is generating a unique presentation for each viewer. There are a number of ways of accomplishing this, from the primitive fly-swatter capabilities provided by "server-side includes" to the tactical nuke Extra Strength features found in application servers. The PHP scripting language falls somewhere into the middle ground, supplying phenomenal capabilities for free. What is PHP?PHP is a scripting language, with a particular affinity for and emphasis on enhancing Web pages. It has a syntax very similar to C (with a smattering of Perl and shell), and includes lots and lots of functions for things like database access, dealing with CGI requests, and image creation and manipulation. When PHP is used as an Apache module, and the language elements are embedded in the document pages themselves, the HTML file might look something like the following: <head> <? // // Preload all the functions and other definitions we need. // include("$DOCUMENT_ROOT/scripts/definitions.php3"); $pdetails = lookup_visitor(); echo " <title>WhizBang Products: Welcome back, " . $pdetails["first_name"] . "!</title>\n"; ?> </head> <body bgcolor="#ffffff"> <h1 align="center">Super-Duper Whizbang Products</h1> <h2 align="center">Welcome back, <? echo $pdetails["first_name"] . " " . $pdetails["last_name"]; ?></h2> When a Web client requests a PHP-enabled page, the <head> <title>WhizBang Products: Welcome back, Ken!<title> </head> <body bgcolor="#ffffff"> <h1 align="center">Super-Duper Whizbang Products</h1> <h2 align="center">Welcome back, Ken Coar</h2> Notice how all the stuff between " Assumptions in this ArticleIt's a good idea to maintain the PHP source in a different directory from your Apache source tree; since they're from separate projects, maintaining the separation in where the software lives avoids confusion. For the rest of this article, I'm going to make the following assumptions:
All of the Since you'll need to compile the PHP software yourself (few binaries are readily available, and those are feature-limited and built with very few extensons), and since it's an Apache module, you need to have the complete Apache source tree available. If you install the Apache software using a package provided as part of your Linux distribution, it's possible you won't have the source tree, so you'll need to download it and unpack it. See the "Building Apache at Lightspeed" appendix at the end of this article. How to Get PHPPHP is open-source software, which means (among other things) that there are source code as well as binary versions available. As with a lot of open-source software, development on PHP is rapid enough that any attempt to put it on a CD or otherwise package it on a long-term distribution medium suffers from the "instantly stale" syndrome. In other words, the place to get the PHP software is the Internet itself. There are multiple Web sites devoted to the PHP project, but the main one is http://www.php.net/, and most of the others are reachable from there. There are two ways of keeping up with the PHP project as its development continues:
In order to use PHP, you're going to need to build it, which means being familiar with the usual software development tools: a shell, The currently stable version of PHP is version 3. Its successor, version 4, is currently under development and there have been a few beta releases already. However, the version used in this column is the more wide-spread V3, and the instructions are specific to that version; they may or may not work with V4. Getting the Most Recent ReleaseIf you're afraid of warts and glitches that might lurk in the latest development version, updating only when a stable release is made is probably best. Of course, there are disadvantages inherent in playing it safe--such as not getting the latest bug fixes or feature additions, or the added pain of having to build the new version in a separate directory tree and then switch over from the old one to the new. The easiest way to download a release tarball to your Linux system is to use a Web browser running on that system and visit http://www.php.net/downloads.php3, choose the appropriate package and save it. (It's not readily available in any other way; for instance, it's not accessible from the PHP site using FTP.) Keeping Up with the Bleeding EdgeTo really keep up with the very latest bug fixes (and bugs) in the latest development version, you'll need to have access to the Internet, a CVS client, and some development tools like % cvs -d :pserver:cvsread@cvs.php.net:/repository login (Logging in to cvsread@cvs.php.net) CVS password: use "phpfi" as the password % cd ./php % cvs -d :pserver:cvsread@cvs.php.net:/repository checkout php3 This will extract the latest versions of everything into the tree starting at Since this method involves grabbing the sources themselves rather than a prepared package, take at least this one extra step before you build: run % cd ./php/php3/ % ./buildconf % autoconf and you're ready to build. To follow this path, subscribe to the PHP ExtensionsThe PHP package is just brimful of capabilities that you can enable if you have the appropriate extra pieces installed on your system. For instance, one of PHP's strong points is its simple and easy interface to numerous different database system such as Oracle, MySQL, mSQL, DB2, and so on. You enable these extensions by letting the % ./configure --with-mysql --with-xml To see a list of the extensions that PHP can support, invoke the script as % ./configure --help Building PHP for CGI UseLike Perl, PHP can be used in standalone scripts as well as embedded in Web pages. Unfortunately, to do both you will need to build it twice: once for each type of use. (Version 4 of PHP supposedly allows a single build to provide both the standalone script engine and the Apache module.) Building the PHP script interpreter is very simple: % cd ./php/php3/ % rm -f config.status config.cache % make clean % ./configure --enable-discard-path other-options % make % make install (Performing the The Building PHP as an Apache ModuleTo use embedded PHP code in Web pages, build it in one of two different ways, depending upon whether it is to be loaded dynamically or built permanently into the Apache server. The only difference between the interpreter and dynamic module builds is the In order to allow Apache to recognise PHP-enabled HTML files as being grist for AddType application/x-httpd-php3 .php3 Then the Web server will know to invoke the PHP module to process the file. Linking it StaticallyStatic modules are linked into the Apache server itself, and cannot be removed without recompiling. This means that they can consume resources, such as memory, even if not used or activated. It also means rebuilding the entire server any time a single module is altered (such as upgrading the PHP installation). The Apache Group now strongly encourages the use of DSOs (dynamic shared objects) in preference to static modules because of their flexibility. While static modules are far from deprecated, I'm including the steps here only for completeness; Linux has no problem with dynamic modulesuse the dynamic-module method instead of the static. In order to build % # % # Preconfigure Apache so that PHP knows where things are % # % cd ./apache-1.3 % ./configure --prefix=/usr/local/web/apache % # % # Now configure and build PHP as a module % # % cd ../php/php3 % rm -f config.status config.cache % ./configure --with-apache=../../apache-1.3 other-switches % make % make install % # % # Now *really* configure Apache, and build it % # % cd ../../apache-1.3 % ./configure --prefix=/usr/local/web/apache \ > --activate-module=src/modules/php3/libphp3.a % make % # % # Install the resulting httpd application % # % /usr/local/web/apache/bin/apachectl stop % cp src/httpd /usr/local/web/apache/bin % cd ../php/php3 % cp php3.ini-dist /usr/local/lib/php3.ini % /usr/local/web/apache/bin/apachectl start (You will probably have to execute the last five commands as See how complicated this is? Repeat for every PHP module rebuild. Building mod_php as a Dynamically Loaded ObjectTo build PHP as a dynamic Apache module, build and install the Apache server itself using the APACI method (i.e., with the To build PHP as a dynamic shared object (DSO), use the following sequence of commands: % cd ./php/php3/ % rm -f config.status config.cache % make clean % ./configure --with-apxs=/usr/local/web/apache/bin/apxs other-options % make % /usr/local/web/apache/bin/apachectl stop # shut down Apache entirely % make install % /usr/local/web/apache/bin/apachectl start # restart Apache When that's all done, your Apache server should be capable of handling PHP CGI scripts and PHP-enabled Web pages. And to upgrade just PHP from a new version, just repeat the process--without touching any other part of Apache. For building PHP on Red Hat Linux 6.1 and using the Apache RPMs, fix a broken file provided by them before executing the above commands. See the "Fixing Red Hat 6.1's apxs Script" appendix in this article for the details. Testing Your InstallationIf you built PHP as a script interpreter, verify that it is working correctly by creating and executing a test script such as the following: % cat <<EOP > script-test.php3 > #!/usr/local/bin/php -q > <? echo "PHP script interpreter is OK!\n"; ?> > EOP % chmod 755 script-test.php3 % ./script-test.php3 PHP script interpreter is OK! % The ' If PHP was built as an Apache module, the simplest way to test it is to create a file in the <? phpinfo(); ?> and then fetch it in a browser. It should display a long and detailed Web page describing the PHP module, the extensions that were included when it was built, and the Apache environment as well. Going FurtherWith PHP installed and working, what do you do then? Well, that's really up to you--but for a couple of examples of what's possible, check out the PHP site itself and the ApacheCon 2000 site. The PHP site is totally driven by the software; as you browse through the online documentation, you can see that not only is it 'live,' meaning that you can comment on it or make suggestions for improvements, but you can also see what other people have suggested. The ApacheCon 2000 site uses PHP to display the very latest information on sessions, speakers, sponsors, and other aspects of the conference, building each page when it's requested from the data, stored in a MySQL database. The documentation link at the PHP site is invaluable. While it doesn't give a lot of guidance on how to accomplish things, it's very complete when it comes to descriptions of the PHP functions. For more, check USENET or the PHP mailing lists. One final trick, which is illustrated at the PHP site, is to add the 'show me the source' feature to your server. Add a line like this to your AddType application/x-httpd-php3-source .phps and then soft-link % ln -s some-php-file.php3 some-php-file.phps Requesting the In Conclusion If running the Apache Web server and wanting to make your Web pages more interactive, responsive, personalised or otherwise to "spice them up," PHP is an easy and excellent way to do it. The software is under constant intense scrutiny by dozens of developers, so problems are fixed quite quickly. Got a Topic You Want Covered?If you have a particular Apache-related topic that you'd like covered in a future article in this column, please let me know at <>. I do read and answer my email, usually within a few hours (although a few days may pass if I'm travelling or my mail volume is way up). If I don't respond within what seems to be a reasonable amount of time, ping me again! Appendix A: Building Apache at LightspeedTo build Apache from source (e.g., your Linux distribution package didn't provide the pieces necessary to add PHP), use the following commands as a quick-start. Download the latest released version of the Apache tarball and unpack it into a working directory. The top-level directory will then be % cd ./apache-1.3 % env CC=gcc CFLAGS="-O2 -Wall" \ > ./configure --enable-shared=max --enable-module=most \ > --with-layout=Apache --prefix=/usr/local/web/apache \ > --with-port=80 Configuring for Apache, Version 1.3.10-dev + using installation path layout: Apache (config.layout) Creating Makefile Creating Configuration.apaci in src [more configuration output] % make [lots of compilation output] % make install [lots more output describing file placement] % /usr/local/web/apache/bin/apachectl start If there are no errors, you should now have a working Apache installation in the location that matches assumption #2 described earlier. It was written to work with dynamic modules rather than static ones, so build PHP as a dynamic module. It's far beyond the scope of this article to give any more information about building Apache; it is about PHP, after all. If you'd like to see an article in this column about the details of building Apache, let me know. Appendix B: Apache Source Components Provided by Linux Distributions
Appendix C: Fixing Red Hat 6.1's apxs ScriptThe my $CFG_LIBEXECDIR = 'modules'; then change my $CFG_LD_SHLIB = 'gcc'; my $CFG_LDFLAGS_SHLIB = q(-shared); If the lines in your |
|
|
About Triggers | Media Kit | Security | Triggers | Login |
All times are recorded in UTC. Linux is a trademark of Linus Torvalds. Powered by Linux 2.4, Apache 1.3, and PHP 4 Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy. |