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:
    /pub/redhat/linux/7.1/en/os/i386/SRPMS/apache-1.3.19-5.src.rpm
    /pub/redhat/linux/7.1/en/os/i386/SRPMS/php-4.0.4pl1-9.src.rpm

  2. Login as root and change directory to /tmp:
    $ su -
    $ cd /tmp

  3. Install the SRPMS:

    $ rpm -i apache-1.3.19-5.src.rpm
    $ rpm -i php-4.0.4pl1-9.src.rpm
  4. Extract Apache and PHP from the compressed tar files, which are part of the SRPMS, with the following commands:

    $ tar -xzvf /usr/src/redhat/SOURCES/apache_1.3.19.tar.gz
    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.0.4pl1.tar.gz
  5. After the files have been extracted, two directories are created under /tmp, namely apache_1.3.19 and php-4.0.4pl1, change directory to the Apache directory and configure Apache:

    $ cd /tmp/apache_1.3.19
    $ pwd  
      /tmp/apache_1.3.19
    $ ./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.19, with PostgreSQL support, without MySQL support. The resultant files will be placed in /usr/local/php4:

    $ cd /tmp/php-4.0.4pl1
    $ pwd
      /tmp/php-4.0.4pl1
    $ ./configure --with-apache=../apache_1.3.19 \
      --with-pgsql=/usr/include/pgsql --without-mysql \
      --prefix=/usr/local/php4
    $ make
    $ make install
  7. Change to the Apache directory and then 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:

    $ cd /tmp/apache_1.3.19
    $ ./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:

    /pub/redhat/linux/7.1/en/os/i386/SRPMS/php-4.0.4pl1-9.src.rpm
  2. Login as root and change directory to /tmp and install the SRPM:

    $ su -
    $ cd /tmp
    $ rpm -i php-4.0.4pl1-9.src.rpm
  3. Extract PHP from the compressed tar file with the following commands:

    $ tar -xzvf /usr/src/redhat/SOURCES/php-4.0.4pl1.tar.gz
  4. Change to the PHP directory, 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:

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

PHP Configuration

After PHP and Apache are built and installed, follow the instructions that follow. These steps have to be carried out whether PHP is installed as an Apache module or as a CGI interpreter.

  1. Change directory to the PHP directory and copy the php.ini file. This will enable you to edit /usr/local/lib/php.ini to set PHP options.

    $ cd /tmp/php-4.0.4pl1
    $ cp php.ini-dist /usr/local/lib/php.ini
  2. Edit httpd.conf, the configuration file for Apache:

    • If you have installed PHP as an Apache module, edit /usr/local/apache/conf/httpd.conf.

    • If you installed PHP as a CGI interpreter, edit /etc/httpd/conf/httpd.conf.

    Find the following lines and uncomment them:

    LoadModule php4_module libexec/libphp4.so
    AddModule mod_php4.c
    AddType application/x-httpd-php .php

Start or Restart the Apache Server

To start or restart the Apache daemon:

  • If you have Apache configured and built as described in the Install PHP as an Apache Module section, use the following command:
    $ /usr/local/bin/apachectl restart
     

  • If you have Apache configured and built as described in the Install PHP as a CGI Interpreter section, use the following command:
    $ /etc/rc.d/init.d/httpd restart
     

If Apache is already started by a previous installation, the configuration files are checked automatically before restarting.

Confirm that PHP is Working Properly

To confirm that PHP is working properly:

  1. Create a file called test.php, which has to be readable by all, in /var/www/html, the Apache default location for web pages. The contents of the file, depending on whether you installed PHP as a module or as a CGI interpreter, contain the following:

    • If PHP is installed as an Apache module:

      <HTML>
      <HEAD>
      Testing PHP</TITLE>
      </HEAD>
      <BODY>
      <?
        /*
         * Print PHP info
         */
        phpinfo();
      ?>
      </BODY>
      </HTML>
    • If PHP is installed as a CGI interpreter:

      # /usr/local/php4/bin/php
      <HTML>
      <HEAD>
      Testing PHP</TITLE>
      </HEAD>
      <BODY>
      <?php
        /*
         * Print PHP info
         */
        phpinfo();
      ?>
      </BODY>
      </HTML>
  2. Point to the following URL in your browser: http://hostname/test.php

If everything is working correctly, a page containing information about the PHP installation, configuration, and version number is displayed.