Write for OTN
Promote your technical skills by writing a technical article for Oracle Technology Network.
Learn more
Stay Connected
OTN Architect Community
OTN ArchBeat Blog Facebook Twitter YouTube Podcast Icon

Installing PHP on Oracle HTTP Server 12c

by Christopher Jones

April 2014

Downloads
download-icon13-1Oracle HTTP Server

Introduction

This article shows how to install PHP on Oracle HTTP Server 12c (OHS). PHP is a hugely popular, interpreted scripting language commonly used for web applications. OHS is the web server component for Oracle Fusion Middleware. It is based on the Apache HTTP Server.

OHS includes a FastCGI module which can easily be configured to use PHP's bundled FastCGI Process Manager ("PHP-FPM"). PHP-FPM has become a standard way of installing PHP. It is an alternative FastCGI interface for PHP with extra features such as advanced process management. For more information, see the PHP documentation and php-fpm.org.

Install Oracle Linux

This article uses 64-bit Oracle Linux 6, which is freely available from http://public-yum.oracle.com/.

Follow the instructions on that site to install the operating system.

Install Oracle HTTP Server

For this article, OHS was installed as a Standalone Domain with default settings:

  1. Create a user oracle with home directory of /home/oracle

  2. Download Oracle HTTP Server 12.1.2 from oracle.com and extract it:

    $ cd /home/oracle
    $ unzip ofm_ohs_linux_12.1.2.0.0_64_disk1_1of1.zip
    
  3. Run the installer

    $ ./ohs_121200_linux64.bin
    $ cd /home/oracle/Oracle/Middleware/Oracle_Home/common/bin
    $ ./config.sh
    

Refer to the OHS documentation for installation detail.

Install Oracle Instant Client 12c

The free Oracle Instant Client is needed for the PHP OCI8 extension.

  1. Download the Instant Client 'basic' RPM from OTN

    The 'devel' package is also needed if you are building PHP from source code.

  2. Install the RPMs as the root user:

    # rpm -ivh oracle-instantclient12.1-basic-12.1.0.1.0-1.x86_64.rpm
    # rpm -ivh oracle-instantclient12.1-devel-12.1.0.1.0-1.x86_64.rpm
    

Do not add the new Instant Client library directory to ld.so.conf since this will cause conflicts for software using the older Oracle libraries included in OHS.

Install PHP

If pre-supplied PHP packages for your system include PHP-FPM, these can be installed. Alternatively PHP can be compiled from source code.

Installing PHP Packages

PHP binaries on https://oss.oracle.com/projects/php/ can be used for testing on 64-bit Oracle Linux 6 or RHEL 6.

  1. For this article, download and install the php55, php55-cli, php55-common, php55-fpm and php55-oci8-12cR1 RPMs:

    # rpm -ivh php55*.rpm
    
  2. Run command-line PHP to check the PHP OCI8 extension is available:

    # php --ri oci8
    
    oci8
    
    OCI8 Support => enabled
    OCI8 DTrace Support => disabled
    OCI8 Version => 2.0.8
    Revision => $Id: f04114d4d67cffea4cdc2ed3b7f0229c2caa5016 $
    Oracle Run-time Client Library Version => 12.1.0.1.0
    Oracle Compile-time Instant Client Version => 12.1
    
    Directive => Local Value => Master Value
    oci8.max_persistent => -1 => -1
    oci8.persistent_timeout => -1 => -1
    oci8.ping_interval => 60 => 60
    oci8.privileged_connect => Off => Off
    oci8.statement_cache_size => 20 => 20
    oci8.default_prefetch => 100 => 100
    oci8.old_oci_close_semantics => Off => Off
    oci8.connection_class => no value => no value
    oci8.events => Off => Off
    
    Statistics =>  
    Active Persistent Connections => 0
    Active Connections => 0
    

Building PHP Source Code

If PHP binaries with the PHP-FPM component are not available for your system, you can build PHP from source code. Steps to build PHP and install it in /opt are below. Various other methods of installing PHP are discussed in the free Underground PHP and Oracle Manual.

  1. Download PHP 5.5 from php.net and extract it:

    # tar -jxf php-5.5.11.tar.bz2
    
  2. Configure PHP with the PHP-FPM module:

    # cd php-5.5.11
    # ./configure --prefix=/opt/php55 --enable-fpm
    

    Other extensions can be added as desired.

  3. Build and Install PHP:

    # make
    # make install
    
  4. Create the PHP configuration file:

    # cp php.ini-development /opt/php55/lib/php.ini
    

    Edit php.ini and set the timezone, for example:

    date.timezone = America/Los_Angeles
    
  5. Create the PHP-FPM configuration file.

    # cp /opt/php55/etc/php-fpm.conf.default /opt/php55/etc/php-fpm.conf
    

Install the Latest PHP OCI8 extension for Oracle Database

Although the version of PHP OCI8 included with PHP's source could have been configured when building PHP, the PHP PECL repository always has the most up to date copy.

  1. If you are behind a firewall, configure PHP's package manager to traverse it:

    # /opt/php55/bin/pear config-set http_proxy http://example.com:80/
    
  2. Install PHP OCI8:

    # /opt/php55/bin/pecl install oci8
    

    When prompted for the ORACLE_HOME, press Enter to allow auto-detection of the Instant Client RPMs:

    downloading oci8-2.0.8.tar ...
    Starting to download oci8-2.0.8.tar (Unknown size)
    ......done: 1,608,704 bytes
    11 source files, building
    running: phpize
    Configuring for:
    PHP Api Version:         20121113
    Zend Module Api No:      20121212
    Zend Extension Api No:   220121212
    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] : 
    ...
    

    Ignore any heuristic warning about a mismatched php_suffix.

    If you had installed Instant Client from ZIP files, for example into /home/oracle/instantclient_12_1, then answer the prompt for ORACLE_HOME with this instead:

    instantclient,/home/oracle/instantclient_12_1
    

    Use an absolute path for the directory, since variables won't be expanded.

    With the ZIP Instant Client, make sure the shared client library symbolic link is created before installing PHP OCI8:

    # cd /home/oracle/instantclient_12_1
    # ln -s libclntsh.so.12.1 libclntsh.so
    
  3. Edit the PHP configuration file /opt/php55/lib/php.ini and add the line:

    extension = oci8.so
    
  4. Run command-line PHP to check the PHP OCI8 extension is available:

    $ /opt/php55/bin/php --ri oci8
    
    oci8
    
    OCI8 Support => enabled
    OCI8 DTrace Support => disabled
    OCI8 Version => 2.0.8
    Revision => $Id: f04114d4d67cffea4cdc2ed3b7f0229c2caa5016 $
    Oracle Run-time Client Library Version => 12.1.0.1.0
    Oracle Compile-time Instant Client Version => 12.1
    
    Directive => Local Value => Master Value
    oci8.max_persistent => -1 => -1
    oci8.persistent_timeout => -1 => -1
    oci8.ping_interval => 60 => 60
    oci8.privileged_connect => Off => Off
    oci8.statement_cache_size => 20 => 20
    oci8.default_prefetch => 100 => 100
    oci8.old_oci_close_semantics => Off => Off
    oci8.connection_class => no value => no value
    oci8.events => Off => Off
    
    Statistics =>  
    Active Persistent Connections => 0
    Active Connections => 0
    

Configure PHP-FPM

The configuration options in /opt/php55/etc/php-fpm.conf (or /etc/php-fpm.conf if you had installed PHP packages) give a nice summary of the power of PHP-FPM. You can see, among others, settings for listening, monitoring, pooling, and for logging.

For this article on Oracle Linux, the default PHP-FPM configuration is fine.

If you want to set Oracle environment variables, add them to php-fpm.conf. For example, if you wanted to change the Oracle globalization settings to a French locale, add:

# Use a globalization environment for France
env[NLS_LANG] = FRENCH_FRANCE.AL32UTF8

# Set the Oracle decimal and group separators to avoid PHP
# string-to-number conversion issues (NLS_NUMERIC_CHARACTERS is only
# read if NLS_LANG is also set)
env[NLS_NUMERIC_CHARACTERS] = .,

On some platforms you may also need to set LD_LIBRARY_PATH to the Instant Client library directory, for example:

env[LD_LIBRARY_PATH] = /home/oracle/instantclient_12_1:$LD_LIBRARY_PATH

Configure OHS

The final piece of configuration is to update OHS to forward PHP requests to PHP-FPM. The exact method of configuration in a production environment depends on your topology and security goals. The example below creates a basic virtual host listening on port 7890 with a document root of /var/www/html. Any request for a PHP resource in that directory will be processed by PHP.

Create a new configuration file $DOMAIN_HOME/config/fmwconfig/components/OHS/ohs1/moduleconf/php.conf where DOMAIN_HOME is /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain

The php.conf file for this example contains:

LoadModule fastcgi_module "${PRODUCT_HOME}/modules/mod_fastcgi.so"

<IfModule mod_fastcgi.c>

Listen 7890

<VirtualHost *:7890>
    ServerName fpmtest.localhost

    DocumentRoot /var/www/html

    FastCGIExternalServer /var/www/html/php-fpm -host 127.0.0.1:9000

    <Directory /var/www/html>
        Order allow,deny
        Allow from all
        <FilesMatch \.php$>
            SetHandler application/x-httpd-fastphp5
        </FilesMatch>
        Action application/x-httpd-fastphp5 /php-fpm
    </Directory>
</VirtualHost>

</IfModule>

The FastCGI server host and socket 127.0.0.1:9000 matches PHP-FPM's default values in php-fpm.conf.

Start PHP-FPM

PHP-FPM can be started with:

# /opt/php55/sbin/php-fpm

If you installed PHP packages you can start PHP-FPM as a service instead:

# service php-fpm start

PHP-FPM can be stopped by killing its process, or stopping the service.

Start OHS

As the oracle user, start the Node Manager and OHS:

$ export DOMAIN_HOME=/home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain
$ nohup $DOMAIN_HOME/bin/startNodeManager.sh > nm.out &
$ $DOMAIN_HOME/bin/startComponent.sh ohs1

You will need to provide the password created during OHS installation.

OHS can be stopped with:

$ $DOMAIN_HOME/bin/stopComponent.sh ohs1

The Node Manager can be stopped by killing its process.

Test PHP

Showing PHP Configuration

Create /var/www/html/pi.php containing:

<?php
    phpinfo();
?>

Load http://localhost:7890/pi.php in a browser.

The PHP configuration will be shown:

jones-php-ohs-fig01
Figure 1: Top of phpinfo() output showing system details, PHP configuration, PHP settings etc.

Executing a Query

To test an Oracle Database script, create a new file /var/www/html/oci8.php containing:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$c = oci_connect("hr", "welcome", "localhost/XE");
if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}

$s = oci_parse($c, "select * from cat");
$r = oci_execute($s);
echo "<table border='1'>\n";
while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>";
    foreach ($row as $item) {
        echo "<td>";
        echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):"&nbsp;";
        echo "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

?>

Edit the user credentials and connection string. Load http://localhost:7890/oci8.php in a browser. A list of available schema objects is displayed:

jones-php-ohs-fig02
Figure 2: Table of HR schema object names and types]

Conclusion

PHP-FPM is a convenient and powerful to way combine PHP and Oracle Fusion Middleware. It allows extensive control over PHP's topology, allowing great flexibility for high-traffic web applications. Unlike installing PHP as an Apache module, PHP-FPM also keeps PHP separate from OHS's default Oracle libraries. This allows the PHP OCI8 extension to be cleanly linked with the latest Instant Client libraries, or with any specific version required by your PHP applications.

If this article helped you, you may be interested in the free Underground PHP and Oracle Manual which contains a wealth of information about using PHP OCI8 with Oracle Database.

About the Author

Christopher Jones is a Senior Principal Product Manager at Oracle.
Twitter Blog LinkedIn