WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Apache, PHP, and PostgreSQL on RedHat Linux
Categories : Apache, PHP, Databases, PostgreSQL, Linux
Dave Jarvis
Dave Jarvis
Date : 2000-09-30
Grade : 0 of 5 (graded 0 times)
Viewed : 16807
Search : More Articles by Dave Jarvis
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


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.)

  1. PostgreSQL from www.postgresql.org.
  2. Apache from www.apache.org.
  3. 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.

  1. If it does not exist, create /usr/local/src.
  2. 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
    
  3. 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.

  1. Change to the PostgreSQL directory:

    cd /usr/local/src/postgresql-7.0.2/
    
  2. Configure, make, and install PostgreSQL (go grab a drink):
    ./configure; make; make install
    
  3. Append the following line to /etc/ld.so.conf:

    /usr/local/pgsql/lib
    
  4. Run the dynamic linker:

    ldconfig -v
    

Apache Configuring for PHP

Apache must be configured before PHP can be configured.

  1. Change to the Apache directory:

    cd /usr/local/src/apache_1.3.12
    
  2. 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.

  1. Change to the PHP directory:

    cd /usr/local/src/php-4.0.2
    
  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
    
  3. Make and install PHP (go grab a drink):

    make; make install
    
  4. 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.

  1. 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
    
  2. Make and install Apache (grab another drink):
    make; make install
    
  3. 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.

  1. Edit /usr/local/apache/conf/httpd.conf. Find the line:

    User nobody
    

    Change it to:

    User www
    
  2. Login as postgres.
  3. 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
    
  4. Issue the following command to ensure the environment variables are set:

    source /home/postgres/.bashrc
    
  5. 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.

  1. 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]
    
  2. Create the database called AddressBook:

    createdb AddressBook
    
    The console should show a successful reply:

    CREATE DATABASE
    
  3. 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.

  1. Create a user inside of AddressBook using psql's interactive mode (same as before):

    psql AddressBook
    CREATE USER www NOCREATEDB NOCREATEUSER;
    
  2. 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:




Name
Phone
E-mail

Launch a web browser, and visit: http://localhost/~www/add.html. The browser should display a form similar to:

Name
Phone
E-mail

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.

  1. Create /usr/local/php/extensions:
    mkdir /usr/local/php/extensions
    
  2. Move pgsql.so into the newly created directory:
    mv /usr/local/src/php-4.0.2/modules/pgsql.so /usr/local/php/extensions
    
  3. 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.








Installing Apache Web Server and PHP 4 on Linux
Categories : PHP, Web Browsers, Apache, Linux
PHP and PostgreSQL
Categories : PHP, Databases, PostgreSQL
Simple Connection to PostgreSQL with PHP
Categories : PHP, PostgreSQL, Databases
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Creating Auto-incrementing ID Fields with PHP and Oracle
Categories : PHP, PHP options/info, Databases, Oracle
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Referer Statistics
Categories : PHP, MySQL, HTTP, Databases
Simple Connection to Sybase with PHP
Categories : PHP, Sybase, Databases
User identification using cookies in PHP and MySQL
Categories : PHP, Databases, MySQL, Cookies
Custom MySQL-functions
Categories : Databases, MySQL, PHP, PHP Functions
Simple ODBC Connections with PHP
Categories : PHP, ODBC, Databases
Multicolumn Output from a Database with PHP
Categories : PHP, Databases, HTML and PHP, MySQL
Setup and Install Apache with PHP4 as a Dynamic Module (DSO)
Categories : PHP, PHP Configuration, Apache