Chapter 9. Setting Up a Web Site that Uses PHP

PHP is a server-side web programming language that you can embed into HTML pages. When a user accesses a PHP-based page, PHP dynamically creates a web page that is then passed to the browser.

PHP offers built-in database integration for several commercial and non-commercial database management systems. It also has the ability to perform many useful Web-related tasks using a large set of built-in functions. As the PHP syntax is similar to that of C, if you are familiar with C, Perl, or any other C-like language, the learning curve for PHP will be minimal. PHP is simple and flexible, thus allowing for short development cycles. Being an open source product, the source code is available and everyone can use it, modify it, and redistribute it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation (FSF).

PHP and Red Hat Database packages work together seamlessly, so you can easily build complex web applications using PHP as a front-end communicating with Red Hat Database server as the backend.

You can install PHP as an Apache module or as a CGI interpreter. Red Hat recommends the former as it does not involve the spawning of additional processes, which results in more efficient access. The most common use of PHP is as a replacement for CGI scripts.

This chapter guides you through the configuration and installation of Apache with PHP as an Apache module or as a CGI interpreter and provides a sample application to show how PHP communicates with Red Hat Database.

PHP Installation

To have PHP work with Red Hat Database, you need to configure PHP to include PostgreSQL support (which is not included in the standard RPM) and then install it as an Apache module or as a CGI interpreter. See either the Section called Install PHP as an Apache Module or the Section called Install PHP as a CGI Interpreter.

Install PHP as an Apache Module

To install PHP as an Apache module:

  1. Download the Apache and PHP source RPMs and save them under the /tmp directory. From the ftp://ftp.redhat.com site, get the files.

    For Red Hat Linux 7.3:

    /pub/redhat/linux/7.3/en/os/i386/SRPMS/apache-1.3.23-11.src.rpm
    /pub/redhat/linux/7.3/en/os/i386/SRPMS/php-4.1.2-7.src.rpm

    For Red Hat Linux Advanced Server 2.1:

    /pub/redhat/linux/enterprise/2.1AS/en/os/i386/SRPMS/apache-1.3.23-10.src.rpm
    /pub/redhat/linux/enterprise/2.1AS/en/os/i386/SRPMS/php-4.0.6-16.src.rpm
  2. Login as root and change directory to /tmp:

    $ su -
    $ cd /tmp
  3. Install the SRPMS.

    For Red Hat Linux 7.3:

    $ rpm -i apache-1.3.23-11.src.rpm
    $ rpm -i php-4.1.2-7.src.rpm

    For Red Hat Linux Advanced Server 2.1:

    $ rpm -i apache-1.3.23-10.src.rpm
    $ rpm -i php-4.0.6-16.src.rpm
  4. Extract Apache and PHP from the compressed tar files, which are part of the SRPMS, with the following commands.

    For Red Hat Linux 7.3:

    $ tar -xzvf /usr/src/redhat/SOURCES/apache_1.3.23.tar.gz
    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.1.2.tar.gz

    For Red Hat Linux Advanced Server 2.1:

    $ tar -xzvf /usr/src/redhat/SOURCES/apache_1.3.23.tar.gz
    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.0.6.tar.gz
  5. After the files have been extracted, two directories are created under /tmp. For Red Hat Linux 7.3 these are apache_1.3.23 and php-4.1.2; for Red Hat Linux Advanced Server 2.1 these are the apache_1.3.23 and php-4.0.6 directories. Change directory to the Apache directory and configure Apache. For both Red Hat Linux 7.3 and Red Hat Linux Advanced Server 2.1, use:

    $ cd /tmp/apache_1.3.23
    $ ./configure --prefix=/usr/local/apache
  6. Change directory to the PHP directory, configure and build PHP with PostgreSQL support. (The following is based on a standard Red Hat Database installation with the PostgreSQL include files under /usr/include/pgsql). This configuration specifies building PHP as Apache module where the top-level Apache build directory is ../apache_1.3.23, with PostgreSQL support, without MySQL support. The resultant files will be placed in the /usr/local/php4 directory.

    For Red Hat Linux 7.3, use:

    $ cd /tmp/php-4.1.2
    $ ./configure --with-apache=../apache_1.3.23 \
      --with-pgsql=/usr/include/pgsql --without-mysql \
      --prefix=/usr/local/php4
    $ make
    $ make install

    For Red Hat Linux Advanced Server 2.1, use:

    $ cd /tmp/php-4.0.6
    $ ./configure --with-apache=../apache_1.3.23 \
      --with-pgsql=/usr/include/pgsql --without-mysql \
      --prefix=/usr/local/php4
    $ make
    $ make install
  7. Change to the Apache directory:

    $ cd /tmp/apache_1.3.23
  8. Reconfigure Apache so that PHP is installed as an Apache module. This configuration specifies building Apache with the PHP4 module enabled and activated, and resultant files placed into /usr/local/apache, configuration files into /usr/local/apache/conf, include files into /usr/local/apache/include, program executables into /usr/local/apache/libexec, and read-only document files into /var/www/html:

    $ ./configure --prefix=/usr/local/apache \
      --activate-module=src/modules/php4/libphp4.a  \
      --sysconfdir=/usr/local/apache/conf \
      --includedir=/usr/local/apache/include \
      --libexecdir=/usr/local/apache/libexec \
      --htdocsdir=/var/www/html --enable-module=php4 \
      --enable-shared=max
    $ make
    $ make install

PHP is now installed. To learn how to configure PHP, see the Section called PHP Configuration.

Install PHP as a CGI Interpreter

To install PHP as a CGI interpreter:

  1. Download the PHP source, save it under /tmp. From the ftp://ftp.redhat.com site, get the files. For Red Hat Linux 7.3:
    /pub/redhat/linux/7.3/en/os/i386/SRPMS/php-4.1.2-7.src.rpm

    For Red Hat Linux Advanced Server 2.1:
    /pub/redhat/linux/enterprise/2.1AS/en/os/i386/SRPMS/php-4.0.6-16.src.rpm

  2. Login as root and change directory to /tmp and install the SRPM. For Red Hat Linux 7.3:
    $ su -
    $ cd /tmp
    $ rpm -i php-4.1.2-7.src.rpm

    For Red Hat Linux Advanced Server 2.1:
    $ su -
    $ cd /tmp
    $ rpm -i php-4.0.6-16.src.rpm

  3. Extract PHP from the compressed tar file. For Red Hat Linux 7.3:

    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.1.2-7.tar.gz

    For Red Hat Linux Advanced Server 2.1:

    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.0.6-16.tar.gz
  4. Change to the PHP directory. For Red Hat Linux 7.3:
    $ cd /tmp/php-4.1.2

    For Red Hat Linux Advanced Server 2.1: php-4.0.6

  5. Configure PHP with PostgreSQL support where the PostgreSQL header files directory is /usr/include/pgsql, install directory is /usr/local/php4, build and install PHP:

    $ ./configure --with-pgsql=/usr/include/pgsql \
      --prefix=/usr/local/php4
    $ make
    $ make install