X

News, tips, partners, and perspectives for the Oracle Linux operating system and upstream Linux kernel work

Connect PHP 7 to Oracle Database using packages from Oracle Linux Yum Server

Note: This post was updated to include the latest available release of PHP as well as simplified installation intstructions for Oracle Instant Client introduced starting with 19c.

 

We recently added PHP 7.4 to our repos on Oracle Linux yum server. These repos include also include the PHP OCI8 extenstion to connect your PHP applications to Oracle database.

In this post I describe the steps to install PHP 7.4, PHP OCI8 and Oracle Instant Client on Oracle Linux to connect PHP to Oracle Database. For this blog post, I used a free Autonomous Database included in Oracle Cloud Free Tier.

Install Oracle Instant Client

Oracle Instant Client RPMs are available on Oracle Linux yum server also. To access them, install the oracle-release-el7 package first to setup the appropriate repositories:

 

$ sudo yum -y install oracle-release-el7
$ sudo yum -y install oracle-instantclient19.5-basic 

If you want to be able to use SQL*Plus (this can come in handy for some sanity checks), install the SQL*Plus RPM also:

 
$ sudo yum -y install oracle-instantclient19.5-sqlplus 

Create a Schema and Install the HR Sample Objects (Optional)

You can use any schema you already have in your database. I’m going to use the HR schema from the Oracle Database Sample Schemas on github.com If you already have a schema with database objects to work with, you can skip this step.

 
$ yum -y install git 
$ git clone https://github.com/oracle/db-sample-schemas.git 
$ cd db-sample-schemas/human_resources 

As SYSTEM (or ADMIN, if you are using Autonomous Database), create a user PHPTEST

SQL> grant connect, resource, create view to phptest identified by <YOUR DATABASE PASSWORD>; 
SQL> alter user PHPTEST quota 5m on USERS; 

If you are using Autonomous Database like I am, change the tablespace above to DATA:

SQL> alter user phptest quota 5m on DATA; 

As the PHPTEST user, run the scripts hr_cre.sql and hr_popul.sql to create and populate the HR database objects

SQL> connect phptest/<YOUR DATABASE PASSWORD>@<YOUR CONNECT STRING>
SQL> @hr_cre.sql
SQL> @hr_popul.sql 

Install PHP and PHP OCI8

To install PHP 7.4, make sure you have the latest oracle-php-release-el7 package installed first.

 
$ sudo yum install -y oracle-php-release-el7

Next, install PHP and the PHP OCI8 extenstion corresponding to the Oracle Instant Client installed earlier:

 
$ sudo yum -y install php php-oci8-19c 

Running the following php code snippet should verify that we can connect PHP to the database and bring back data. Make sure you replace the schema and connect string as appropriate.

 

Create a file emp.php based on the code above.

Run it!

 
$ php emp.php 

This should produce the following:

King
Kochhar
De Haan
Hunold
Ernst
Austin
Pataballa
Lorentz
Greenberg
Faviet
Chen
Sciarra
...

 

Join the discussion

Comments ( 3 )
  • Yogesh Chaudhari Wednesday, September 12, 2018
    I don't know much about PHP, but I am able to follow your blog and able to access Oracle database from PHP code. Thanks!
  • Andrey Prozorov Monday, November 12, 2018
    Man!! you are a god!!! 4 days i have killed to run oci8 under php. and 5 minutes by your article. In whole internet any manuals missed `ldconfig` part. THANK YOU!
  • Charlie Thursday, March 19, 2020
    I found that, when building my own libphp7.so and installing it into the stock httpd, I had to run these commands for SELinux to permit any OCI calls:

    setsebool -P httpd_execmem 1
    setsebool -P httpd_can_network_connect 1
    setsebool -P httpd_can_network_connect_db 1

    I'm assuming that you are handling this in what we would now call a dnf "scriptlet."

    I will certainly give this a try on a pristine server. I wonder if I it will be easy to back out what I've done for my custom install in order to move to yours, as the maintenance seems much reduced.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.