Nathan Nathan - 3 months ago 19
PHP Question

Update LD_LIBRARY_PATH system wide for ODBC connection via PHP7.0-FPM

I am trying to connect to NetSuite over ODBC from an Ubuntu Linux machine. I've got everything setup. It works via the shell, and PHP command line. But when I try and make this connection via a web request to PHP, it fails with this error:

Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Can't open lib '/opt/netsuite/odbcclient/lib64/ivoa25.so' : file not found, SQL state 01000 in SQLConnect in


In order for it to work from the shell, I had to add this to my
.profile
(via the
readme.txt
that came with the NetSuite driver)

ODBCINI="/opt/netsuite/odbcclient/odbc64.ini"
OASDK_ODBC_HOME="/opt/netsuite/odbcclient/lib64"
LD_LIBRARY_PATH="/opt/netsuite/odbcclient/lib64${LD_LIBRARY_PATH:+":"}${LD_LIBRARY_PATH:-""}"
export ODBCINI
export OASDK_ODBC_HOME
export LD_LIBRARY_PATH


The problem seems to be with setting the
LD_LIBRARY_PATH
. I noticed on this page about half way down the page it says

"Note: You can only set this environment variable inside an interactive shell."


The NetSuite driver requires this update to the
LD_LIBRARY_PATH
, so are there any other options to making a web request to execute my
odbc_connect()
statement?

I tried and failed with


  • putenv()

  • adding
    . /etc/odbcinst.ini
    to the
    /etc/init.d/php7.0-fpm
    script

  • adding the required statements & exports to
    /etc/environment
    (even though it said it wouldn't work)

  • added
    /opt/netsuite/odbcclient/lib64
    to a new file
    /etc/ld.so.conf.d/netsuite.conf



When I did the last one from the above list, I ran
sudo ldconfig
successfully but got this error when trying to connect & query (bunch of garbage, but different error!):

[unixODBC]�pV�,8�(8��|D�x������H�R��ut�
(8eow�ut�(8�����|`gx�0x�pV�K����������M0�0�N��M0� ������4�
�0d��Ryy

Answer

So the solution ended up being simpler than expected. I essentially created a bash script that exports the environment variables I need then run the PHP script. Here's the entire contents of my netsuite-odbc.sh script. I can now connect and run my queries.

ODBCINI="/opt/netsuite/odbcclient/odbc64.ini"
OASDK_ODBC_HOME="/opt/netsuite/odbcclient/lib64"
LD_LIBRARY_PATH="/opt/netsuite/odbcclient/lib64${LD_LIBRARY_PATH:+":"}${LD_LIBRARY_PATH:-""}"
export ODBCINI
export OASDK_ODBC_HOME
export LD_LIBRARY_PATH

/usr/bin/php /home/user/netsuite-odbc.php
Comments