Installing and Configuring PHP

PHP can be installed as a module, or even as a CGI processor if your Web server does not support SAPI (Server Application Programming Interface) or ISAPI module types. In the following sections, you'll install PHP as a dynamic shared object (DSO) for Apache on Linux/UNIX, and as a SAPI module for Apache on Windows. While the Linux/UNIX installation requires actual compilation and building of files, the Windows installation consists only of placing files in particular places. Neither method is terribly difficult-if you've made it through the Apache installation sections, then you have the skills to install PHP as well.

Installing PHP on Linux/UNIX

In this section, you'll learn how to install PHP with Apache on Linux/Unix as a dynamic shared object (DSO). While you might be able to find pre-built versions of PHP for your system, compiling PHP from the source gives you greater control over the features built into your binary.

To download the PHP distribution files, go to the home of PHP, http://www.php.net/, and follow the link to the Downloads section. Grab the latest version of the source code-this example uses version 4.3.0. Your distribution will be named something like php-version.tar.gz, where version is the most recent release number. Keep this file in the directory reserved for source files, such as /usr/src/ or /usr/local/src/.

Next, unzip and untar the software by typing the following command at the prompt (#):

 # gunzip < php-4.3.0.tar.gz  |  tar xvf - 

You should now have a structure of directories, with the top-level directory named php-version. Change your current directory to this top-level directory to prepare for configuring the software.

For example:

 # cd php-4.3.0 

Like the Apache build method, PHP compiling follows the configure/make/make install sequence of events. Within your distribution directory you will use the configure script, which accepts command-line arguments to control the features that PHP will support. In these installation instructions, you will include the basic options you need to use to install PHP with Apache, and support for using MySQL for your database applications. For now, type the following command at the prompt:

 # ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs 

Once the configure script has run, and after you've received informational notes from the PHP Group, you will be returned to the prompt. Unless the configure script errors, simply take the PHP Group notes as sound advice, and then issue the make command, followed by the make install command. These commands should end the process of PHP compilation and installation and return you to your prompt.

 ... Installing build environment:     /usr/local/php/lib/php/build/ Installing header files:          /usr/local/php/include/php/ Installing helper programs:       /usr/local/php/bin/   program: phpize   program: php-config   program: phpextdist # 

Next, you will need to ensure that two very important files are copied to their correct locations. First, issue the following command to copy the distributed version of php.ini to its default location (the php.ini file is the configuration file for PHP, and you'll learn more about it later in this chapter):

 # cp php.ini-dist /usr/local/php/lib/php.ini 

Next, copy the PHP shared object file to its proper place in the Apache installation directory, if it has not already been placed there by the installation process:

 # cp libs/libphp4.so /usr/local/apache2/modules/ 

To ensure that PHP and Apache get along with one another, you need to check for-and potentially add-a few items to the httpd.conf configuration file. First, look for a line like the following:

 LoadModule php4_module              modules/libphp4.so 

If this line is not present, or only appears with a # sign at the beginning of the line, you must add the line or remove the # sign. This line tells Apache to use the PHP shared object file (libphp4.so) that was created by the PHP build process.

Next, look for this section:

 # # AddType allows you to add to or override the MIME configuration # file mime.types for specific file types. # 

and add the following line:

 AddType application/x-httpd-php .php 

This ensures that the PHP engine will parse files that end with the .php extension. Your selection of filenames may differ, and you may wish to add .html as an extension, which would parse every .html as PHP as well. When you're through adding your extensions, save the file and restart Apache. When you look in your error_log, found in the logs subdirectory of your Apache installation directory, you should see something like the following line:

 [Tue Jan 21 09:48:44 2003] [notice] Apache/2.0.44 (Unix) PHP/4.3.0 configured 

Congratulations are in order, as PHP is now part of the Apache Web server. If you want to learn how to install PHP on a Windows platform, keep reading. Otherwise, you can skip ahead to the "Testing Your Installation" section later in this chapter.

Installing PHP on Windows

Unlike building and installing PHP on Linux/UNIX, installing PHP on Windows requires nothing more than downloading the distribution and moving a few files around. To download the PHP distribution files, go to the home of PHP, http://www.php.net/, and follow the link to the Downloads section. Grab the latest version of the Windows binaries-this example uses version 4.3.0. Your distribution will be named something like php-version.zip, where version is the most recent release number.

Once the file is downloaded to your system, double-click on it to launch your unzipper. The distribution is packed up with pathnames already in place, so if you extract the files to the root of your drive, it will create a directory called php-version-Win32 and place all the files and subdirectories under that new directory.

Now that you have all the basic PHP distribution files, you just need to move a few of them around:

  1. In the PHP installation directory, find the php.ini-dist file and rename it php.ini.

  2. Move the php.ini file to C:\WINDOWS\ or wherever you usually put your *.ini files.

  3. Move the php4ts.dll file to C:\WINDOWS\SYSTEM\ or wherever you usually put your *.dll files.

To get a basic version of PHP working with Apache, you'll need to make a few minor modifications to the Apache configuration file. First, find a section that looks something like this:

 # Example: # LoadModule foo_module modules/mod_foo.so # LoadModule access_module modules/mod_access.so LoadModule actions_module modules/mod_actions.so LoadModule alias_module modules/mod_alias.so LoadModule asis_module modules/mod_asis.so LoadModule auth_module modules/mod_auth.so #LoadModule auth_anon_module modules/mod_auth_anon.so #LoadModule auth_dbm_module modules/mod_auth_dbm.so #LoadModule auth_digest_module modules/mod_auth_digest.so LoadModule autoindex_module modules/mod_autoindex.so #LoadModule cern_meta_module modules/mod_cern_meta.so LoadModule cgi_module modules/mod_cgi.so #LoadModule dav_module modules/mod_dav.so #LoadModule dav_fs_module modules/mod_dav_fs.so LoadModule dir_module modules/mod_dir.so LoadModule env_module modules/mod_env.so #LoadModule expires_module modules/mod_expires.so #LoadModule file_cache_module modules/mod_file_cache.so #LoadModule headers_module modules/mod_headers.so 

At the end of this section, add the following:

 LoadModule php4_module c:/php-version/sapi/php4apache2.dll 

Next, look for this section:

 # # AddType allows you to add to or override the MIME configuration # file mime.types for specific file types. # 

Add the following line:

 AddType application/x-httpd-php .php 

This ensures that the PHP engine will parse files that end with the .php extension. Your selection of filenames may differ, and you may wish to add .html as an extension, which would parse every .html as PHP as well. When you're through adding your extensions, save the file and restart Apache. When you look in your error_log, found in the logs subdirectory of your Apache installation directory, you should see something like the following line:

 [Tue Jan 21 10:24:44 2003] [notice] Apache/2.0.44 (Win32) PHP/4.3.0 configured 

Now that PHP is happily cohabitating with Apache, you can breathe easy and move on to bigger and better things.

Modifications with php.ini

Even after installing PHP, you can still change its behavior through the php.ini file. Directives in the php.ini file come in two forms: values and flags. Value directives take the form of a directive name and a value separated by an equals sign (=). Possible values vary from directive to directive. Flag directives take the form of a directive name and a positive or negative term separated by an equals sign. Positive terms include 1, On, Yes, and True. Negative terms include 0, Off, No, and False.

You can change your php.ini settings at any time, but after you do, be sure to restart the server so that the changes can take effect. At some point, take time to read through the php.ini file on your own, to see the types of things that can be configured.

Adding Database Support to PHP

One of the selling points of PHP is its ability to interface with numerous databases. This ability makes PHP a logical choice for generating database-driven dynamic content, as well as developing e-commerce applications, project and document management applications, and virtually any other application you can imagine that might use a database.

The examples of database usage in this book, in the context of application development, use MySQL as the database. However, the basic PHP functions for database connectivity with other popular database systems are located in Chapter 3, "Working with Databases." The other databases covered in Chapter 3 are PostgreSQL, Oracle, and Microsoft SQL Server.

In order to use the functions for these particular databases, you must enable their functionality when compiling PHP (on Linux/UNIX) or through the php.ini file (on Windows). During the installation instructions for Linux/UNIX, earlier in this chapter, the configuration directive --with-mysql was used. This directive told the compiler to include the MySQL-related functions as part of PHP. In the Windows installation, nothing additional was needed to enable MySQL functions-they are enabled by default.

Enabling Other Database Support on Linux/UNIX

To enable support for other databases on Linux/UNIX, configuration flags similar to --with-mysql are used at configuration time. For example:

  • --with-pgsql=DIR. Enables PostgreSQL support. DIR is the base director of PostgreSQL, which defaults to /usr/local/pgsql

  • --with-oci8=DIR. Enables Oracle support.DIR defaults to the ORACLE_HOME environment variable.

  • --with-mssql=DIR. Enables Microsoft SQL Server support. DIR is the base director of the FreeTDS library, which defaults to /usr/local/freetds

Each of these directives assumes the database is already installed on your system. For additional notes regarding installation and configuration of PHP with these database types, please see their respective pages in the PHP Manual.

  • PostgreSQL information can be found at http://www.php.net/manual/en/ref.oci8.php.

  • Oracle 8 information can be found at http://www.php.net/manual/en/ref.oci8.php.

  • Microsoft SQL Server information can be found at http://www.php.net/manual/en/ref.oci8.php.

Enabli ng Other Database Support on Windows

To enable support for other databases on Windows, you must activate extensions through the php.ini file. Extensions ship with the PHP distribution, and are found in the extensions directory within your PHP installation directory.

Activating an extension simply means that you must uncomment the line in php.ini that refers to the extension you wish to use. There is a section in php.ini that looks something like this:

 ;Windows Extensions ;Note that MySQL and ODBC support is now built in, so no dll is needed for it. ; ;extension=php_bz2.dll ;extension=php_cpdf.dll ;extension=php_crack.dll ;extension=php_curl.dll ;extension=php_db.dll ;extension=php_dba.dll ;extension=php_dbase.dll ... 

To uncomment an extension, simply remove the semicolon from the front of the line.

For example:

  • extension=php_pgsql.dll will activate PostgreSQL support.

  • extension=php_oci8.dll will activate Oracle 8 support.

  • extension=php_msssql.dll will activate Microsoft SQL Server support.

After you have activated the appropriate extensions, save the php.ini file and restart your server.

For additional notes regarding installation and configuration of PHP with these database types, please see their respective pages in the PHP Manual.

  • PostgreSQL information can be found at http://www.php.net/manual/en/ref.oci8.php.

  • Oracle 8 information can be found at http://www.php.net/manual/en/ref.oci8.php.

  • Microsoft SQL Server information can be found at http://www.php.net/manual/en/ref.oci8.php.

Testing Your Installation

The simplest way to test your PHP installation is to create a small test script using the phpinfo() function. This function will produce a long list of configuration information.

Open a text editor and type the following line:

 <?  phpinfo(); ?> 

Save this file as phpinfo.php and place it in the document root of your Web server-the htdocs subdirectory of your Apache installation. Access this file via your Web browser; you should see something like what's shown in Figure 1.5.

click to expand
Figure 1.5: The results of phpinfo() on a Linux/UNIX system.

Getting Installation Help

Should you hit a brick wall during your installation attempt, your first recourse should be to the official PHP site, at http://www.php.net/ (particularly the annotated manual at http://www.php.net/manual/).

If you still can't find your answer, both the PHP site and the mailing list archives at http://www.php.net/search.php are searchable.

If you still can't figure out what's wrong, you may do the PHP community a service (and possibly get your solution) by explaining the problem you're having. You can join the PHP mailing lists at http://www.php.net/support.php. Although these lists often have high volume (and you may not want hundreds of extra e-mails in your inbox) you can learn a lot from them. If you are serious about PHP scripting, you should certainly subscribe to at least one digest list. Once you've subscribed to the list that matches your concerns, you might consider posting your problem.



PHP Essentials
PHP Essentials, 2nd Edition
ISBN: 1931841349
EAN: 2147483647
Year: 2002
Pages: 74

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