- Installing PHP and the Oracle Instant Client for Linux and Windows
- Software Requirements
- Enabling the PHP OCI8 Extension on Windows
- Enabling the PHP OCI8 Extension on Linux
- Verifying the PHP OCI8 Extension is Installed
- Connecting to an Oracle Database
- Using PHP OCI8 and Oracle
- Troubleshooting
- Conclusion
- PHP Packages for Oracle Linux
- Available PHP Releases
- Installation on Oracle Linux 7
- Set up Required Repositories
- Installing PHP
- Updating PHP
- Connecting PHP to Oracle Database with the PHP OCI8 Extension
- Installing PHP OCI8
- About Oracle Instant Client
- Installing PHP on Oracle Linux 8 via AppStream Modules
- Installing PHP OCI8 on Oracle Linux 8
- Installation on Oracle Linux 9
- PHP in Containers
- Getting Help
Installing PHP and the Oracle Instant Client for Linux and Windows
The easiest way to configure PHP to access a remote Oracle Database is to use Oracle Instant Client libraries. This note describes how to install PHP with the OCI8 Extension and Oracle Instant Client on Windows and Linux. The free The Underground PHP and Oracle Manual explains other installation options and contains more detail.
OCI8 is the PHP extension for connecting to Oracle Database. OCI8 is open source and included with PHP. The name is derived from Oracle’s C «call interface» API first introduced in version 8 of Oracle Database. OCI8 links with Oracle client libraries, such as Oracle Instant Client.
Oracle Instant Client is a free set of easily installed libraries that allow programs to connect to local or remote Oracle Database instances. To use Instant Client an existing database is needed — Instant Client does not include one. Typically the database will be on another machine. If the database is local then Instant Client, although convenient and still usable, is generally not needed because OCI8 can directly use the database libraries.
When using Instant Client 11g, PHP OCI8 connects to all editions of Oracle 9.2, 10.x, and 11.x databases.
Software Requirements
Software | Notes |
Oracle Instant Client | Download the «Basic» package. On Linux, also download the «SDK» or «devel» package. If space is at a premium, the Basic Lite package can be used instead of Basic. |
Apache HTTP Server | Version 2.2 |
PHP | Version 5.4 |
Enabling the PHP OCI8 Extension on Windows
The Instant Client binaries complement PHP’s pre-built binaries for Windows.
Install «for All Users, on Port 80». Do a typical install into the default destination folder: C:\Program Files\Apache Software Foundation\Apache2.2 .
LoadModule fcgid_module modules/mod_fcgid.so
. Options Indexes FollowSymLinks ExecCGI .
- Add a timezone line like: date.timezone = America/Los_Angeles Use your local timezone name.
- Add the line: extension_dir = C:\php-5.4.0\ext This is the directory containing the PHP extensions.
- Remove the semicolon from the beginning of the line: extension=php_oci8_11g.dll
FcgidInitialEnv PHPRC "c:/php-5.4.0" AddHandler fcgid-script .php FcgidWrapper "c:/php-5.4.0/php-cgi.exe" .php
Enabling the PHP OCI8 Extension on Linux
On Linux, PHP is generally manually compiled because the bundled version nevers seems to be up to date. However, if you don’t wish to recompile PHP, more recent, unsupported RPM packages for Oracle Linux are available from oss.oracle.com, or via Unbreakable Linux Network updates. If a supported PHP environment is desired use Zend Server. These all have the OCI8 extension pre-built.
To build PHP and OCI8 from source code:
rpm -Uvh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm rpm -Uvh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
downloading oci8-1.4.7.tgz . Starting to download oci8-1.4.7.tgz (Unknown size) . done: 168,584 bytes 10 source files, building running: phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 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] :
If you have the Instant Client RPMs, hit Enter and PECL will automatically build and install an oci8.so shared library. If you have the Instant Client zip files, or want a specific version of Instant Client used, then explicitly give the appropriate path after » instantclient, «: instantclient,/usr/lib/oracle/11.2/client64/lib Use an explicit, absolute path since PECL does not expand environment variables. If you don’t have the pecl program, you can alternatively download the OCI8 package in a browser and then install it with:
tar -xzf oci8-1.4.7.tgz cd oci8-1.4.7 phpize ./configure --with-oci8=instantclient,/usr/lib/oracle/11.2/client64/lib make install
It is important to set all Oracle environment variables before starting Apache so that the OCI8 process environment is correctly initialized. Setting environment variables in PHP scripts can lead to obvious or non-obvious problems. On Oracle Linux, export environment variables in /etc/sysconfig/httpd . On Debian-based machines set them in /etc/apache2/envvars .
Restart Apache, for example:
Verifying the PHP OCI8 Extension is Installed
To check OCI8 configuration, 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» and listing the OCI8 options that can be configured.
Connecting to an Oracle Database
To create a connection, Oracle username and password credentials are passed as two parameters of oci_connect() . An Oracle Database name connection identifier must be used for the third parameter because programs linked with Instant Client are always considered «remote» from any database server and need to be told which database instance to connect to. The connection string 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 Network and created a service name such as orcl for you.
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 orcl database service running on mymachine. No tnsnames.ora or other Oracle Network file is needed:
$conn = oci_connect(‘hr’, ‘hr_password’, ‘mymachine.mydomain/orcl’);
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 will 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 PHP OCI8 and Oracle
Try out a simple script, testoci.php Modify the connection credentials to suit your database and load it in a browser. This example lists all tables owned by the user HR:
\n"; while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) < echo "\n"; foreach ($row as $item) < echo " ".($item !== null ? htmlentities($item, ENT_QUOTES) : " ")." \n"; > echo " \n"; > echo "\n"; ?>
Troubleshooting
Check the Apache error log file for startup errors.
Temporarily set display_error=On in php.ini so script errors are displayed. Switch it back off when finished for security reasons.
Chapter 9 of The Underground PHP and Oracle Manual contains information about common connection errors and discusses alternative ways to set environment variables.
Oracle’s SQL*Plus command line tool can be downloaded from the Instant Client page to help resolve environment and connection problems. Check SQL*Plus can connect and then ensure the Environment section (not the Apache Environment section) of phpinfo.php shows the equivalent environment settings.
Windows Specific 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 php.ini’s extension_dir directive does not contain the directory with php_oci8_11g.dll then Apache startup will give an alert: «PHP Startup: Unable to load dynamic library php_oci8_11g.dll.»
If PATH is set incorrectly or the Oracle libraries cannot be found at all, 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 there are multiple versions of Oracle libraries on the machine then version clashes are likely. For some discussion on setting variables refer to Using PHP OCI8 with 32-bit PHP on Windows 64-bit.
Linux Specific Help
If using Instant Client ZIP files, make sure the two packages are unzipped to the same location. 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.
Conclusion
Using Oracle Instant Client and installing PHP OCI8 from PECL provide maximum flexibility, allowing components to be easily installed and upgraded.
Questions and suggestions can be posted on the OTN PHP
The PHP Developer Center contains links to useful background material.
PHP Packages for Oracle Linux
This page describes PHP and related packages for current releases of Oracle Linux. For Oracle Linux 8, the packages are provided via AppStream modules and are covered under Oracle Linux support. Consult the Application Stream Module Life Cycle appendix in the Managing Software in Oracle Linux manual for support timelines. For Oracle Linux 7, these packages are published in developer repositories, for which support is limited to package installation assistance only.
Available PHP Releases
PHP Version | Oracle Linux Release | Repository/Module Name |
---|---|---|
8.0 | Oracle Linux 9 | OL9 AppStream / php:8.0 |
8.0 | Oracle Linux 8 | OL8 AppStream / php:8.0 |
7.4 | Oracle Linux 8 | OL8 AppStream / php:7.4 |
7.3 | Oracle Linux 8 | OL8 AppStream / php:7.3 |
7.2 | Oracle Linux 8 | OL8 AppStream / php:7.2 |
7.4 | Oracle Linux 7 | OL7 Developer / php74 |
Installation on Oracle Linux 7
These are the steps to install PHP.
Set up Required Repositories
To configure the yum repositories shown above on your system, install the repository setup packageoracle-php-release-el7. After installing this package, the default enabled repository is that for the latest available PHP release. To install the repository setup package for Oracle Linux 7:
$ sudo yum install -y oracle-php-release-el7
Installing PHP
Then, to install the latest available release of PHP on Oracle Linux 7:
Updating PHP
If you have PHP already installed, and a newer release has been published, make sure you update oracle-php-release-el7 first.
$ sudo yum update oracle-php-release-el7
$ sudo yum update php
Connecting PHP to Oracle Database with the PHP OCI8 Extension
The PHP OCI8 database extention enables access to Oracle Database with support for SQL, PL/SQL, bind variables, large object (LOB) types as well connection pooling and result caching features of Oracle Database. Read this blog post for a complete example connecting a PHP 7.4 application to Oracle Database.
Installing PHP OCI8
Each PHP release has its own repository on Oracle Linux yum server and each of those repositories also contains a corresponding RPM for the OCI8 extension, built for a specific major release of Oracle Instant Client. These RPMs follow this naming scheme: php-oci8 . For example, on Oracle Linux 7, the PHP OCI8 RPM for PHP 7.4 is built with Instant Client 19c and is named: php-oci8-19c. To install it:
$ sudo yum install php-oci8-19c
About Oracle Instant Client
PHP OCI8 depends on Oracle Instant Client. Starting with Oracle Instant Client 21c, yum/dnf configuration files are delivered via the packages oracle-instantclient-release-el8 and oracle-instantclient-release-el7, respectively. Oracle Instant Client packages up to release 19c are in the ol7_oracle_instantclient and ol8_oracle_instantclient repositories, configured with release packages oracle-release-el7 and oracle-release-el8. Assuming you have enabled the repository for Oracle Instant Client appropriate for your Oracle Linux release, it will be installed as a dependency. More details about Oracle Instant Client on this yum server.
Installing PHP on Oracle Linux 8 via AppStream Modules
Oracle Linux 8 introduces the concept of Application Streams, where multiple versions of user-space components can be delivered and updated more frequently than the core operating system packages in RPM format or groups of related RPMs called Modules. Oracle Linux 8 offers several independently updated versions of PHP via Module Streams. For mode details, please refer to the documentation.
To list available Module Streams for PHP:
Installing PHP OCI8 on Oracle Linux 8
To install the PHP OCI8 on Oracle Linux 8, enable both the php:7.4 and the php-oci8 modules.
$ sudo dnf install oraclelinux-developer-release-el8 oracle-instantclient-release-el8
$ sudo dnf module enable php:7.4 php-oci8
$ sudo dnf install php-oci8-21c
Installation on Oracle Linux 9
To install the latest available release of PHP on Oracle Linux 9:
PHP in Containers
- PHP Command Line Interface tool (PHP CLI)
- PHP via FastCGI Process Manager (FPM)
- PHP via Apache HTTP Server with mod_php
- Variants of each of the above with Oracle Database connectivity via PHP-OCI8
Getting Help
For help with PHP on Oracle Linux, please visit the PHP space in the Oracle Developer Community.