I recently had to work on a project that was written in PHP and was getting data from an Oracle database. Installing
php on Ubuntu is pretty straightforward but connecting to the database unfortunately required quite a few gymnastics!
In this article we will look at how to compile PHP extensions on Ubuntu 18.04 and above, specifically the pdo_oci
extension. Note that in order for this extension to work, the oracle drivers must be already installed. See this article for instructions on how to setup Oracle drivers on Ubuntu.
The general procedure to install extensions is as follows:
- Download the PHP source with same version that you have installed and unzip it. In my case it was PHP 7.4.3.
-
Change to the directory php-YOUR-VERSION/ext/EXTENSION which in my case was
php-7.4.3/ext/pdo_oci
. - Run
phpize
-
Configure the extension, which in my case was
./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient_18_3
- Compile the extension via
make
- Install it system-wide
sudo make install
-
Update the PHP configuration to use the newly minted extension. In my case, I needed to update the following files
to add/uncomment the following line
extension=pdo_oci.so
:/etc/php/7.4/fpm/php.ini
/etc/php/7.4/cli/php.ini
-
php -m
should now list PDO_OCI as an enabled module. -
Finally restart either your webserver and/or the php-fpm service:
sudo service php7.4-fpm restart
This above method installs a pdo_oci.so
in the PHP extensions folder, without the need for recompiling
all of PHP. Note that in my case, the project in question was using the
PDO OCI driver which is actually experimental and not
recommended. The officially approved Oracle driver is OCI and can be installed via pecl install oci8
(provide the instant client path when prompted
instantclient,/opt/oracle/instantclient_18_3
). Note that if this does not work further instructions can be
found here.
Note that if you use Apache as your webserver rather than Nginx then the pdo_oci
module needs to be separately enabled in Apache as well. Create /etc/php/7.4/mods-available/pdo_oci.ini
with the following:
; priority=20
extension=pdo_oci.so
Finally sudo phpenmod pdo_oci
and sudo service apache2 restart
to enable the extension with Apache.
phpinfo()
should now show all the extensions that have been enabled and you can always check your webserver error logs for any issues. Happy coding!