Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, January 6, 2010

Enabling the PHP OCI8 Extension on Linux

To install on Linux, PHP generally needs to be recompiled. For users of Oracle Enterprise Linux, pre-built RPM packages of PHP and OCI8 are available from oss.oracle.com, or via ULN updates.

To build PHP and OCI8:

1.

Download the Apache HTTPD Server if you decide not to use your default package. Download the PHP 5.2 source code.

Install PHP and Apache following Installation on Unix systems in the PHP manual.

At this stage, don't configure the OCI8 extension.

Check that PHP is working before continuing.
2.

Download the Basic and the SDK Instant Client packages from the OTN Instant Client page. Either the zip file or RPMs can be used.

Install the RPMs as the root user:

rpm -Uvh oracle-instantclient11.1-basic-11.1.0.7.0-1.i386.rpm
rpm -Uvh oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm

The first RPM puts the Oracle libraries in /usr/lib/oracle/11.1/client/lib and the second creates headers in /usr/include/oracle/11.1/client

If using the zip files, the SDK should unzipped to the same directory as the basic package, and a symbolic link created:

ln -s libclntsh.so.11.1 libclntsh.so

3.

The latest OCI8 1.3 extension from PECL supercedes the default version in the PHP 5.2 source code. This can be downloaded manually and installed with:

pecl install oci8-1.3.5.tgz

Or the latest production package can be automatically downloaded and installed:

pecl install oci8

This gives:

downloading oci8-1.3.5.tgz ...
Starting to download oci8-1.3.5.tgz (137,987 bytes)
.....done: 137,987 bytes
10 source files, building
running: phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
1. Please provide the path to the ORACLE_HOME directory.
Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : autodetect

1-1, 'all', 'abort', or Enter to continue:

If you have the Instant Client RPMs, hit Enter and PECL will automatically locate the RPMs and build and install an oci8.so shared library.

If you have the Instant Client zip files, or have multiple versions of the Instant Client RPMs installed then explicitly give the path:
*

Enter 1 to update the first (and only) setting. The OCI8 configuration prompt will then be shown:

Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib'
if you're compiling with Oracle Instant Client [autodetect] :

*

Enter the path, such as:

instantclient,/usr/lib/oracle/11.1/client/lib

or:

instantclient,/your/path/to/instantclient_11_1

Do not use any environment variable names, because the PECL installer doesn't expand variables.
*

You will get reprompted 1-1, 'all', 'abort', or Enter to continue. As it says, press Enter to continue. PECL will build and install an oci8.so shared library.
4.

Edit php.ini and confirm extension_dir points to the directory the oci8.so file was installed into.

Also in php.ini, enable the OCI8 extension with:

extension=oci8.so

5.

Add the directory with Instant Client to /etc/ld.so.conf, or manually set LD_LIBRARY_PATH to /usr/lib/oracle/11.1/client/lib and restart Apache.

It is important to set all Oracle environment variables before starting Apache. A script helps do that:

#!/bin/sh

LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client/lib:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

echo Starting Apache
/usr/sbin/apachectl start

This script can contain desired Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.

Verifying the Extension is Installed

To check the extension is configured, create a simple PHP script phpinfo.php in the Apache document root:



Load the script into a browser using the appropriate URL, e.g. "http://localhost/phpinfo.php". The browser page will contain an "oci8" section saying "OCI8 Support enabled".

The OCI8 options that can be configured in your php.ini file are shown.
Connecting to Oracle

Oracle authentication and database information is passed to oci_connect() to create a connection. Tools linked with Instant Client are always "remote" from any database server and an Oracle Net connection identifier must be used along with a username and password. The connection information is likely to be well known for established Oracle databases. With new systems the information is given by the Oracle installation program when the database is set up. The installer should have configured Oracle Net and created a service name.
There are several ways to pass the connection information to PHP. This example uses Oracle's Easy Connect syntax to connect to the HR schema in the MYDB database service running on mymachine. No tnsnames.ora or other Oracle Network file is needed:

$c = oci_connect('hr', 'hr_password', '//mymachine.mydomain/MYDB');

See Oracle's Using the Easy Connect Naming Method documentation for the Easy Connect syntax.

In new databases the demonstration schemas such as the HR user may need to be unlocked and given a password. This may be done in SQL*Plus by connecting as the SYSTEM user and executing the statement:

ALTER USER username IDENTIFIED BY new_password ACCOUNT UNLOCK;

Using Oracle

Try out a simple script, testoci.php. Modify the connection details to suit your database and load it in a browser. This example lists all tables owned by the user HR:

\n";
}

oci_free_statement($stid);
oci_close($conn);

?>

Troubleshooting

Check the Apache error log file for startup errors.

Temporarily set display_error=On in php.ini so script errors are displayed.

Chapter 9 of The Underground PHP and Oracle Manual contains some common connection errors.

Oracle's SQL*Plus command line tool can be downloaded from the Instant Client page to help resolve environment and connection problems.

Check the environment used by SQL*Plus is the same as shown by phpinfo.php.

Windows Help

If the phpinfo.php script does not produce an "oci8" section saying "OCI8 Support enabled", verify that extension=php_oci8_11g.dll is uncommented in php.ini.

If PATH is set incorrectly or the Oracle libraries cannot be found, starting Apache will give an alert: "The dynamic link library OCI.dll could not be found in the specified path." The Environment section of the phpinfo() page will show the values of PATH and the Oracle variables actually being used by PHP.

If php.ini's extension_dir directive is not correct, Apache startup will give an alert: "PHP Startup: Unable to load dynamic library php_oci8.dll."

Linux Help

If using Instant Client zip files, make sure the two packages are unzipped at the same time. Make sure a symbolic link libclntsh.so points to libclntsh.so.11.1

Set all required Oracle environment variables in the shell that starts Apache.

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";

?>