Installing OCI8 not only on Ubuntu

Installing OCI8 not only on Ubuntu

This post was translated using the DeepL translator, so I apologize for any incomprehensibility. In this article you can read more about my decision.

If we want to work in PHP with Oracle database, we must first compile and install the OCI8 extension.

Installing Oracle Instant Client and SDK

First we need to download Instant Client and SDK from (Oracle website) to which we also need to register on the Oracle website. We are looking for the files instantclient-basic-linux.x64-[version].zip and instantclient-sdk-linux.x64-[version].zip. Once the files are downloaded, we create a folder in /opt, for example mkdir /opt/oracle. The files files are then moved into the folder.

1mv instantclient-basic-linux.x64-[verze].zip /opt/oracle/instantclient-basic-linux.x64-[verze].zip
2mv instantclient-sdk-linux.x64-[verze].zip /opt/oracle/instantclient-sdk-linux.x64-[verze].zip

Moved files need to be unzipped.

1cd /opt/oracle
2unzip instantclient-basic-linux.x64-[verze].zip
3unzip instantclient-sdk-linux.x64-[verze].zip

Then we create symbolic links to the .so files. Your version of Instant Client and SDK may differ, so double-check the folder name and the name of the .so files.

1ln -s /opt/oracle/instantclient_19_5/libclntsh.so.19.5 /opt/oracle/instantclient_19_5/libclntsh.so
2ln -s /opt/oracle/instantclient_19_5/libocci.so.19.5 /opt/oracle/instantclient_19_5/libocci.so

Finally, you still need to add the folder to the ldconfig configuration. The folder name will be different

1echo /opt/oracle/instantclient_19_5 > /etc/ld.so.conf.d/oracle-instantclient.conf

And update ldconfig, using the ldconfig command.

Installing extensions to PHP

In order to compile oci8 extensions, it is usually necessary to install a few more additional libraries. Typically, these are:

1apt install php-dev php-pear build-essential libaio1

After successful installation we can finally start compiling the OCI8 extension. In the command `echo' command, we will specify the path to the instant client installation folder, because if we we didn't specify it, the installation would ask us for it anyway.

1echo "instantclient,/opt/oracle/instantclient_12_2" | pecl install oci8

After compilation and installation, you still need to modify the PHP configuration so that the extension OCI8 to load. Therefore, we send the following set of commands:

1echo "extension=oci8.so" >> /etc/php/7.3/mods-available/oci8.ini
2ln -s /etc/php/7.3/mods-available/oci8.ini /etc/php/7.3/cli/conf.d/20-oci8.ini
3ln -s /etc/php/7.3/mods-available/oci8.ini /etc/php/7.3/fpm/conf.d/20-oci8.ini
4ln -s /etc/php/7.3/mods-available/oci8.ini /etc/php/7.3/apache2/conf.d/20-oci8.ini

The first line creates the .ini file in PHP 7.3 mods. If you have a different version of PHP, or you you are not sure, check it first with php --version. The remaining three lines will create a symbolic link to the CLI, FPM and Apache2 to load the extension in all environments. However, if you are not using FPM or Apache2, you can safely omit one of these lines. Finally, we'll check to see if the extension is actually enabled.

1php -m | grep oci8
2php-fpm7.3 -m | grep oci8

If you see oci8, we're almost done. We still need to restart the FPM client and Apache2.

1service php7.3-fpm restart
2## nebo
3service apache2 restart