Wednesday, January 6, 2010

Installing Oracle, PHP, and Apache on Linux

Let's walk through the steps required to install the Oracle Database, Apache HTTP Server, and PHP as an Apache module on Linux. Check out the OTN Linux Technology Center to see what versions of Linux are certified for use with the Oracle Database. We will be using Oracle Enterpise Linux for this example.

Software Requirements:

Software Version URL
Oracle Database 10g Express Edition 10.2 http://www.oracle.com/technology/products/database/xe/
Apache HTTP Server 2.0 http://httpd.apache.org/download.cgi
PHP Hypertext Processor 5.2 http://www.php.net/downloads/

Installing Oracle

You have a choice here. You may either install the database locally on this Linux machine, or you may decide to use an Oracle server located on another machine on your network. If your database is remote, jump to the article on Installing PHP and the Oracle Instant Client for Linux and Windows.

Otherwise, if this is your first time with Oracle, installing the Oracle Database 10g Express Edition only takes a few minutes. Download the Express Edition (commonly known as "XE") RPM package, log in as root and run:

    # rpm -ivh oracle-xe-univ-10.2.0.1-1.0.i386.rpm

After the software package is installed on the machine, configure a database by running this and answering its four questions:

    # /etc/init.d/oracle-xe configure

For Debian users, a .deb file is also available.

Starting and Stopping Oracle

Oracle XE will be running after installation. You can test it by opening your browser to the Database home page http://localhost:8080/apex. Use the username "SYSTEM" and the password you chose during installation.

Note: You may need to replace "localhost" with the IP address 127.0.0.1 or your machine's DNS name if you are behind a firewall or if localhost does not resolve for some other reason.

If you need to restart the database at any time use the Start Database and Stop Database items on the operating system's "Oracle Database 10g Express Edition" menu. To run these you will need to add yourself to the operating system "dba" group and re-login to the machine.

Alternatively you can call the oracle-xe script as the root user:

    # /etc/init.d/oracle-xe stop

To restart:

    # /etc/init.d/oracle-xe start

Installing Apache HTTP Server

Now that the Oracle Database is installed, you should install Apache. You must install Apache before you can install PHP, since PHP will be installed into Apache.

If you don't want to mess about compiling Apache or PHP, Oracle has pre-built PHP RPM packages that use the default Apache package.

If your operating system doesn't already have Apache, download httpd-2.0.63.tar.bz2 from the Apache web site, log in as the root user and execute these commands:
    # tar -jxvf httpd-2.0.58.tar.bz2

# cd httpd-2.0.58
# ./configure --prefix=/usr/local/apache --enable-module=so
# make
# make install

When configuring the web server, the option "--enable-module=so" allows PHP to be compiled as a Dynamic Shared Object (DSO). Also, the "--prefix=" option sets where Apache will be installed during the command "make install"

If you are familiar with the tar command on UNIX systems, you may be wondering why we did not need to invoke bunzip2 to extract the tar file. Linux includes the GNU version of tar which has a new 'j' flag to automatically uncompress a bzipped tar file. If you downloaded the gzipped file you could have used the 'z' flag instead.

Note: With Apache 2 you should use the default pre-fork MPM ("Multi-Processing Module") because many of the PHP extentions are not known to be thread-safe.

Starting and Stopping Apache

Start and stop Apache with the apachectl script:

    # /usr/local/apache/bin/apachectl start

You should test that Apache is up and running on your machine by opening your web browser to http://localhost/.

Now stop Apache so it can be configured for PHP:

    # /usr/local/apache/bin/apachectl stop

On Oracle Enterprise Linux, the apachectl script for the default Apache package is /usr/sbin/apachectl.

Installing PHP

To build PHP yourself, download the file php-5.2.9.tar.bz2 from the PHP downloads page.

Installation Steps

  1. Log in as the root user and execute these commands:

        # tar -jxvf php-5.2.9.tar.bz2
    
    # cd php-5.2.9
    # export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
    # ./configure \
    --with-oci8=$ORACLE_HOME \
    --with-apxs2=/usr/local/apache/bin/apxs \
    --with-config-file-path=/usr/local/apache/conf \
    --enable-sigchild
    # make
    # make install

    Note: if you are behind a firewall, you may need to set the environment variable http_proxy to your proxy server before running make install. This enables PHP's PEAR components to be installed.

  2. Copy PHP's supplied initialization file:

        # cp php.ini-recommended /usr/local/apache/conf/php.ini
    

    For testing it is helpful to edit php.ini and set display_errors to On so you see any problems in your code.

  3. Edit Apache's configuration file /usr/local/apache/conf/httpd.conf and add the following lines:

        #
    
    # This next section will call PHP for .php, .phtml, and .phps files
    #
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php .phtml
    AddType application/x-httpd-php-source .phps

    #
    # This is the directory containing php.ini
    #
    PHPIniDir "/usr/local/apache/conf"

    If a LoadModule line was not already inserted by the PHP install, add it too:

        LoadModule php5_module modules/libphp5.so
    

Restart the Apache HTTP Server

You must now restart the Apache Server so that you can test your PHP installation.

    # /usr/local/apache/bin/apachectl start

Note: If you are using Oracle 10.2 but not the Express Edition, you must give the "nobody" user access to the Oracle directory. With Oracle 10.2.0.2 there is a script $ORACLE_HOME/install/changePerm.sh to do this.

If there are errors, they will display on your screen. They may also be recorded in /usr/local/apache/logs/error_log. If you have problems, double check your httpd.conf and php.ini, and make corrections.

When you start Apache, you must at least have ORACLE_HOME defined. Any other required Oracle environment variables must be set before Apache starts too. These are the same variables set by the $ORACLE_HOME/bin/oracle_env.sh or the /usr/local/bin/oraenv scripts.

To simplify things, you may create a script to start Apache. I did this and named it start_apache:

    #!/bin/sh


ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
echo "Oracle Home: $ORACLE_HOME"
echo Starting Apache
/usr/local/apache/bin/apachectl start
Testing Apache and PHP with Oracle

Testing PHP with Oracle is easy. You simply need to place a PHP file into your htdocs directory; normally /usr/local/apache/htdocs.

Here are two files, the first is used to test basic PHP installation. Open it in a browser with http://localhost/phpinfo.php. If PHP is installed you should see a large page full of PHP configuration information.

phpinfo.php
   

Check there is a section titled "oci8".

oci8test.php

The second file will display name and salary columns from the EMPLOYEES table owned by the HR user. This requires the HR schema be installed otherwise you will need to modify the script. The HR schema comes with Oracle XE. You can unlock access and set a password using the Adminstration section of the Database home page.

For Oracle XE the database connection string is simply "127.0.0.1/XE". If you are not using Oracle XE then change the connection string (third parameter) to the Oracle Net entry for your database.

  Oracle PHP Test";

echo "

Oracle PHP Test


";
echo "\n\n";
echo "\n\n\n";

for ($i = 0; $i < $nrows; $i++ ) { echo "\n";
echo "";
echo "";
echo "\n";
}

echo "
NameSalary
" . $results["LAST_NAME"][$i] . "$" . number_format($results["SALARY"][$i], 2). "
Number of Rows: $nrows
";
echo "
If you see data, then it works!
\n";

?>

No comments:

Post a Comment