Search code examples
pythonoracle-databaseflaskuwsgisystemd

Set oracle path for uwsgi app called by service file


I have deployed a flask application with uwsgi and nginx

The following is the .ini file for uwsgi

[uwsgi]
;module = name of file which contains the application object in this case wsgi.py
LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib
chdir=/home/ansible/apps/payment_handler
module = wsgi:application

;tell uWSGI (the service) to start in master mode and spawn 5 worker *processes* to serve requests
master = true
processes = 5

;a socket is much faster than a port, and since we will be using nginx to exppose the application this is better
socket = 0.0.0.0:8001

vaccum = true    
die-on-term = true

When I run this from the command line like so

uwsgi --ini payment_app.ini

It works !

However I would like to run the application using a service, the following is the service file

[Unit]
Description=uWSGI instance to serve service app
After=network.target

[Service]
User=root

WorkingDirectory=/home/ansible/apps/payment_handler
Environment="PATH=/home/ansible/apps/payment_handler/env/bin"
ExecStart=/home/ansible/apps/payment_handler/env/bin/uwsgi --ini payment_app.ini

[Install]
WantedBy=multi-user.target

However it does not work because it cannot find the libs for cx_oracle I have it set in my bashrc file

export LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib

However since the service file does not use this to load it's env variables it seems to not find it

Error log

Jun 17 09:58:06 mau-app-036 uwsgi: cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help

I have tried setting it in the .ini file (as seen above)

LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib

I have also tried setting it in my init.py file using the os module

os.environ['LD_LIBRARY_PATH'] = '/usr/lib/oracle/18.3/client64/lib'

Both to no avail, any help would be great thanks Centos 7 btw


Solution

  • Problems like this are why the Instant Client installation instructions recommend running:

      sudo sh -c "echo /usr/lib/oracle/18.3/client64/lib > \
          /etc/ld.so.conf.d/oracle-instantclient.conf"
      sudo ldconfig
    

    This saves you having to work out how & where to set LD_LIBRARY_PATH.

    Note that the 19.3 Instant Client RPM packages automatically runs this for you. Some background is in the Instant Client 19c Linux x64 release announcement blog.