PHP works on almost all Unix platforms and with Microsoft Windows 32-bit environments. In Unix environments, PHP can be used to develop three different types of software:
PHP was designed for the third alternative. To use it in this environment, a web server is required. We focus on integrating MySQL with the web environment in this chapter.
PHP can be used for web scripting by integrating it as a module of a supported web server such as Apache, Microsoft Internet Information Server (IIS), or iPlanet. For web servers that do not support module integration—those that do not support SAPI or ISAPI direct module interfacing—the PHP executable can be used as a CGI processor. The Common Gateway Interface (CGI) allows a web server to run any executable, but this is a much slower method of running the PHP engine than integrating it as a server module. In either case, to use PHP to access the MySQL DBMS, it must be compiled with MySQL support.
Before installing PHP, you should check whether you already have a web server that supports PHP and whether your PHP engine supports MySQL. For example, many Linux distributions now include the Apache web server with preconfigured PHP support for accessing the MySQL DBMS.
The simplest way to check if your web server supports PHP, and if the PHP module supports MySQL, is to create a file phpinfo.php in the web server document root. Create this file so that it has only one line with the following contents:
<?php phpinfo(); ?>
For most systems that are running the Apache web server, the document root is the directory: /usr/local/apache/htdocs/.
After creating the file, run the script by requesting the following URL with a web browser that is running on the same machine as the web server: http://localhost/phpinfo.php.
If you see output—not the script contents—then your web server has PHP support. The Configure Command section near the top of the output will include the option --with-mysql if MySQL support is built-in; scrolling down through the output should also show a section headed mysql.
If this checks out, you're ready to start with PHP and MySQL. If not, then additional configuration is required. Suggested steps are included in this section.
Here are three possible ways to get started with a web server, PHP, and MySQL support:
The easiest approach is to obtain an installation package. NuSphere has a free, downloadable installation package for Linux, Sun Solaris, and Microsoft Windows environments. They also sell integrated Apache, PHP, and MySQL bundles with software support. The NuSphere web site is http://www.nusphere.com/.
Another easy approach is to add packages to your system using, for example, the RPM package manager for RedHat Linux, the Debian package manager for Debian Linux, or Fink for Mac OS X. For Linux installs, the Apache package usually includes the PHP module, which usually has preconfigured MySQL support.
Obtain the source code to Apache, PHP, and MySQL and compile the source. This is the most complex option, but it allows you to choose the libraries you include in PHP and how PHP is loaded as a module into the web server. Apache and PHP must be installed after MySQL, and PHP must be built with the --with-mysql=/usr/local/mysql option (assuming MySQL is installed in the directory /usr/local/mysql). Concise instructions for installing PHP and Apache can be found at http://www.php.net/manual. Instructions for installing MySQL can be found in Chapter 2. An excellent, in-depth guide to installing from source can be found at http://www.delouw.ch/linux/apache.phtml.
If you have a MySQL installation that you're happy with, but don't have a web server and PHP, then the easiest approach is to install a web server and PHP package. The best choice is the Apache web server, because it's popular, free, and flexible in its integration with PHP. We focus on Apache in this section.
If you have an Apache web server that appears not to support PHP, it's worth double-checking. There are three common problems that can give the wrong impression:
Your PHP test files don't have the extension .php. If this is the case, your web server will deliver the source code and not run the scripts.
Your web server isn't configured to run the PHP engine when a file with the .php extension is requested. In Apache, this is controlled by a line in the httpd.conf file usually found in the directory /usr/local/apache/conf:
AddType application/x-httpd-php .php
If this line is commented, uncomment it, and restart the web server. Apache can usually be restarted by running apachectl restart. The apachectl script is usually found in /usr/local/apache/bin/.
Your PHP module is a DSO (DSOs are explained below), but the module isn't being loaded by Apache. Check that the following line is present and not commented out in the httpd.conf file:
LoadModule php4_module libexec/libphp4.so
If you change the httpd.conf file, restart the web server.
If you have Apache and PHP, but not MySQL support, you need to find out whether your PHP module is a web server dynamic shared object (DSO). To check if your PHP is a DSO, take a look at the output of the phpinfo.php script you created in the previous section. If the Configure Command section contains the string --with-apache=, your PHP is not a DSO. If it contains the string --with-apxs=, your PHP is a DSO.
If your PHP module isn't a DSO, you need to rebuild your PHP and Apache to include MySQL support. This is the disadvantage of having a statically linked PHP module. The advantage of static linking is that the module is preloaded when the web server starts up, and a static-linked module is faster. Instructions for rebuilding a static-linked module version can be found at: http://www.php.net/manual/en/install.unix.php. As discussed earlier, ensure that you include the --with-mysql= directive in the PHP build process and that the directive includes the path to your MySQL installation.
If your PHP is a DSO, the upgrade process is simpler. This is the primary advantage of a DSO. You only need to rebuild the PHP module by rerunning the configure script with the addition of the --with-mysql= directive; again, make sure you include the path to MySQL with the --with-mysql= directive. Run make and make install to complete the process. You then need to restart your web server so that the new PHP module is loaded.
The easiest approach to getting started in a Microsoft Windows environment is to use an installation package that includes Apache, PHP, and MySQL. NuSphere sells an install package with support and offers a free package without support. Their web site is http://www.nusphere.com/. Another excellent, free installation package is PHP Triad for Windows, available from http://sourceforge.net/projects/phptriad/.
To install without an installation package, instructions can be found in the installation section of the PHP manual at http://www.php.net/manual/.
Copyright © 2003 O'Reilly & Associates. All rights reserved.