This document attempts to give step-by-step instructions on how to configure Apache, PHP, and PostgreSQL so that they are not only aware of each other but as tightly integrated as possible. The steps described below begin with downloading the software packages, then configuring and installing them.
The end of the document covers how to ensure PHP can be run from the command line.
Except where noted, the instructions must be carried out by root.
Prerequisite Knowledge
- Basic Unix System Administration.
- Familiarity with the Apache Web Server.
Software Background
Apache is web server software. It is responsible for listening on a particular port (usually 80, 8000, 8080, or equivalent) for incoming requests for files (usually by a Web browser such as Netscape or Mosaic). Upon receiving a request for a file, Apache returns its contents to the Web
browser which is responsible for displaying the hypertext mark-up language (HTML) document contents (images, media, and so forth).
PHP is a hypertext preprocessor. When integrated with web server software
(such as Apache), it becomes a powerful mechanism for adding intelligent
scripts inside of HTML documents. Certain pages (whose file name typically
ends in .php, .php3, or .phtml) requested from the web browser are passed
through the PHP engine before being returned to the web browser. The PHP
engine examines the page for PHP script, executes the script, and returns an
HTML document to the browser. The power of the scripting language stems
from its tight integration with major databases (MySQL, PostgreSQL, and
others).
PostgreSQL is database software. As an open-source, fully featured database
it is a cost-effective means to store application information. PHP
and PostgreSQL can be tightly integrated. This allows Apache,
albeit indirectly through PHP, to get database queries for particular web
pages.
Download
If not done already, download the software packages in question. (See
the next section for a list of file names.)
- PostgreSQL from www.postgresql.org.
- Apache from www.apache.org.
- PHP from www.php.net.
Preparation
For the purposes of the examples, it will be presumed Apache 1.3.12,
PHP 4.0.2, and PostgreSQL 7.0.2 are being installed on a RedHat 6.2 system.
Substitute appropriate numbers for more recent versions; earlier versions than
those stated will probably not install using these instructions.
- If it does not exist, create /usr/local/src.
- Copy all related files to /usr/local/src. There should be:
apache_1.3.12.tar.gz
php-4.0.2.tar.gz
postgresql-7.0.2.base.tar.gz
postgresql-7.0.2.tar.gz
postgresql-7.0.2.test.tar.gz
- Extract all files:
for i in *.gz; do tar -zxf $i; done
If this doesn't work, manually extract all the files:
tar -zxf apache_1.3.12.tar.gz
tar -zxf php-4.0.2.tar.gz
etc.
Try using the Tab key to auto-complete file names.
If they don't already exist, create two new users:
www
postgres
PostgreSQL Configuring and Installation
Since integrating PHP with PostgreSQL requires the latter's libraries
to be available for compiling PHP, PostgreSQL must be installed first.
- Change to the PostgreSQL directory:
cd /usr/local/src/postgresql-7.0.2/
- Configure, make, and install PostgreSQL (go grab a drink):
./configure; make; make install
- Append the following line to /etc/ld.so.conf:
/usr/local/pgsql/lib
- Run the dynamic linker:
ldconfig -v
Apache Configuring for PHP
Apache must be configured before PHP can be configured.
- Change to the Apache directory:
cd /usr/local/src/apache_1.3.12
- Configure Apache (to be installed in /usr/local/apache)
./configure --prefix=/usr/local/apache
PHP Configuring and Installation
Now that Apache has been configured, PHP can be configured.
- Change to the PHP directory:
cd /usr/local/src/php-4.0.2
- Configure PHP with Apache and PostgreSQL in mind:
./configure --with-apache=/usr/local/src/apache_1.3.12
--with-pgsql=share
--enable-track-vars
- Make and install PHP (go grab a drink):
make; make install
- Copy PHP's ini file:
cp php.init-dist /usr/local/lib/php.ini
Apache Configuring and Installation
Now that PHP has been configured and installed, Apache must be reconfigured
to take PHP into account, then installed.
- Configure Apache with PHP in mind (libphp4.a is correct;
libmodphp4.a is incorrect):
./configure --prefix=/usr/local/apache
--activate-module=src/modules/php4/libphp4.a
- Make and install Apache (grab another drink):
make; make install
- Tell Apache to link PHP pages with PHP. Edit
/usr/local/apache/conf/httpd.conf. Add (or uncomment) the lines:
AddType application/x-httpd-php .php .php3 .phtml
AddType application/x-httpd-php-source .phps
Don't exit the editor just yet, as there's another line to change.
Allowing Database Access
Now that PHP, PostgreSQL and Apache have been installed, they must be hooked
up together. Apache's process is usually run as nobody by default.
Change this from nobody to the www account.
Setup
Apache and PostgreSQL still need additional pieces of information in order
to cooperate vicariously through PHP.
- Edit /usr/local/apache/conf/httpd.conf. Find the line:
User nobody
Change it to:
User www
- Login as postgres.
- Edit /home/postgres/.bashrc (presuming bash
is the default shell). Append the following lines:
PGDATA=/home/postgres/database
PATH="$PATH:/usr/local/pgsql/bin"
export PGDATA PATH
- Issue the following command to ensure the environment variables are set:
source /home/postgres/.bashrc
- Initialize the database with:
initdb
If all went well, the console should read:
This database system will be initialized with username "postgres".
This user will own all the data files and must also own the server process.
Creating database system directory /home/postgres/database
Creating database system directory /home/postgres/database/base
Creating database XLOG directory /home/postgres/database/pg_xlog
Creating template database in /home/postgres/database/base/template1
Creating global relations in /home/postgres/database/base
Adding template1 database to pg_database
Creating view pg_user.
Creating view pg_rules.
Creating view pg_views.
Creating view pg_tables.
Creating view pg_indexes.
Loading pg_description.
Vacuuming database.
Success. You can now start the database server using:
/usr/local/pgsql/bin/postmaster -D /home/postgres/database
or
/usr/local/pgsql/bin/pg_ctl -D /home/postgres/database start
In order for PHP to call upon the database from within HTML files served
to web browsers by Apache, PostgreSQL must be told to use the -i
option. The PostgreSQL database process would then be started using:
/usr/local/pgsql/bin/postmaster -i -D /home/postgres/database
Create a Database and Table (Relation)
For demonstration purposes, the database will be named AddressBook.
It will have one table in it named Addresses. While logged in as
postgres, apply the steps that follow.
- Start the database process:
postmaster -i -D $PGDATA
If all went well, the console should read similar to:
DEBUG: Data Base System is starting up at [Date/Time]
DEBUG: Data Base System is in production state at [Date/Time]
- Create the database called AddressBook:
createdb AddressBook
The console should show a successful reply:
CREATE DATABASE
- Create a table inside of AddressBook using psql's
interactive mode (the second line will be typed in at the prompt
AddressBook=*):
psql AddressBook
CREATE TABLE Addresses ("Name" text, "Phone" text, "Email" text);
The console should show a successful reply:
CREATE
Granting Table Access
In order for the Apache process, in coordination with PHP, to modify the
Addresses table, PostgreSQL must be told which user (in this
example www) has what rights on which table(s). While logged in
as postgres, apply the steps that follow.
- Create a user inside of AddressBook using psql's
interactive mode (same as before):
psql AddressBook
CREATE USER www NOCREATEDB NOCREATEUSER;
- While still at the AddressBook prompt, grant permissions:
GRANT all ON Addresses TO www;
The console should show a successful reply:
CHANGE
Restart Apache
Stop then start the Apache web server (do not restart)
using:
/usr/local/apache/bin/apachectl stop
/usr/local/apache/bin/apachectl start
PHP and PostgreSQL
Login as www. Copy and paste the following HTML code into
a file named /home/www/public_html/add.html, ensuring all
directories (and files) are world-readable:
Launch a web browser, and visit: http://localhost/~www/add.html.
The browser should display a form similar to:
Create another file called add-entry.php, copy and paste the
following:
";
echo "Phone = $phoneResult ";
echo "E-mail = $emailResult ";
pg_Close( $db );
?>
Add Okay!
Enter information at http://localhost/~www/add.html, select the
Add Entry button. The browser should display something similar to:
Name = John Doe
Phone = 1-250-555-1212
E-mail = test@somewhere.org
Add Okay!
At this point, the database AddressBook has a table named
Addresses. Inside the table (also called a relation) is
an entry with the information that was entered above. This simple example
shows just how easy it is to add and retrieve information from a database,
using PHP and PostgreSQL. This example leaves much to be desired,
such as ensuring that the information is valid, and a search form. Both
of those are beyond the scope of this document.
Command Line
From the command line, php does not know about PostgreSQL (although
with Apache, it does). This section guides the reader on how to configure
PHP so that it can find the PostgreSQL function calls. These steps should be
performed by root. If running PHP from the command line is not
a requirement, then these steps are optional.
- Create /usr/local/php/extensions:
mkdir /usr/local/php/extensions
- Move pgsql.so into the newly created directory:
mv /usr/local/src/php-4.0.2/modules/pgsql.so /usr/local/php/extensions
- Edit the file /usr/local/lib/php.init. Change
extension_dir to point to /usr/local/php/extensions. Then
add a new Dynamic Extension as follows:
extension=pgsql.so
Now PHP could should be runnable from a shell prompt, either by calling
php directly, or by making executable shell scripts.
Conclusion
At this point the system should have a complete and stable installation of
PHP, Apache, and PostgreSQL. If someone writes a shell script to automate
this installation, let me know and I'll include
it here. Good luck!
Copyright © 2000 by Dave Jarvis.
This information is provided AS IS and without warranty of any kind, either
expressed or implied. No warranty, either expressed or implied,
is made regarding the accuracy or utility of the information presented at
this site. The reader agrees to take full responsibility for any errors or
damages that may occur resulting from use or mis-use of the
information provided at this site.
Reprinted with the author's permission.
Comments and/or corrections are most welcome.
|