Skip to content

Instantly share code, notes, and snippets.

@diegosparente
Last active May 11, 2022 11:33
Show Gist options
  • Save diegosparente/4d42c8d8c8ac51d7bf64d2c8fa716f35 to your computer and use it in GitHub Desktop.
Save diegosparente/4d42c8d8c8ac51d7bf64d2c8fa716f35 to your computer and use it in GitHub Desktop.
Configuration process for php to work with Oracle database.

How to configure ORACLE (oci + instantclient) for PHP Applications

by Tom Benevides

This text is intended to guide the installation and configuration of Oracle's InstantClient, responsible for mediating connections with Oracle databases. Below are the necessary steps::

This tutorial adopted version 8.1 of PHP. If your version is different, make the necessary adaptations.

The terminal adopted is the default one, so the configuration of environment variables is done in ~/.bashrc. If using ZSH, add the variables in ~/.zshrc

Step 1

Download Oracle Instant Client (Base Client and SDK Package):

mkdir ~/Downloads
cd ~/Downloads
sudo wget https://download.oracle.com/otn_software/linux/instantclient/216000/instantclient-basic-linux.x64-21.6.0.0.0dbru.zip
sudo wget https://download.oracle.com/otn_software/linux/instantclient/216000/instantclient-sdk-linux.x64-21.6.0.0.0dbru.zip

Step 2

Create a folder called oracle inside the address /opt/ and extract the files to it:

sudo mkdir /opt/oracle
cd /opt/oracle
sudo unzip ~/Downloads/instantclient-basic-linux.x64-21.6.0.0.0dbru.zip -d /opt/oracle
sudo unzip ~/Downloads/instantclient-sdk-linux.x64-21.6.0.0.0dbru.zip -d /opt/oracle

Rename the generated folder with the version to just instantclient, otherwise when updating the package version it will be necessary to redo the tutorial:

sudo mv instantclient_21_6 instantclient

Step 3

Add a folder to ldconfig and update the linked linker:

sudo -i
echo "/opt/oracle/instantclient" > /etc/ld.so.conf.d/oracle-instantclient
echo "/opt/oracle/instantclient" >> /etc/ld.so.conf.d/x86_64-linux-gnu.conf
ldconfig

Step 4

Instale as dependências do PHP que comunicam com o sdk da Oracle:

sudo apt install php8.1-dev php-pear build-essential libaio1

obs: In case of dependency failures in the installation of packages, run the command sudo apt-get -f install

Step 5

Get the oci8 library file and set the InstantClient location when prompted:

pecl install oci8-3.0.1
instantclient,/opt/oracle/instantclient
pecl channel-update pecl.php.net

Step 6

Add PHP extensions:

Extensões PHP:

echo "extension = oci8.so" >> /etc/php/8.1/fpm/php.ini
echo "extension = oci8.so" >> /etc/php/8.1/cli/php.ini
systemctl restart php8.1-fpm
systemctl restart apache2
exit

Test if the extension is enabled:

php -m | grep 'oci8'

obs: The result should be something like oci8

Step 7:

Configure the environment variables needed to link the InstantClient:

echo "export LD_LIBRARY_PATH=/opt/oracle/instantclient/" >> ~/.bashrc
echo "export ORACLE_HOME=/opt/oracle/instantclient/network/admin/" >> ~/.bashrc
echo "export TNS_ADMIN=/opt/oracle/instantclient/network/admin/" >> ~/.bashrc
source ~/.bashrc 

Or with ZSH

echo "export LD_LIBRARY_PATH=/opt/oracle/instantclient/" >> ~/.zshrc
echo "export ORACLE_HOME=/opt/oracle/instantclient/network/admin/" >> ~/.zshrc
echo "export TNS_ADMIN=/opt/oracle/instantclient/network/admin/" >> ~/.zshrc
source ~/.zshrc 

Ready. It should now be possible to connect to Oracle DBMS from your PHP applications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment