The Web Server Gateway Interface (WSGI) is a standardized interface between web servers and Python web frameworks or applications. Many frameworks including Django support WSGI.
This post is a brief how-to about configuring Apache's mod_wsgi with Python's cx_Oracle driver for Oracle Database. The steps are for Oracle Linux.
Download Instant Client Basic & SDK ZIP files from OTN. For cx_Oracle 5.1, use the ZIPs, not the RPMs.
As root, unzip the files to the same directory, e.g.
Configure Instant Client:
ln -s libclntsh.so.12.1 libclntsh.so
Install the pip package management tool for Python by following pip.readthedocs.org/en/latest/installing.html and downloading
get-pip.py. Then run:
pip install cx_Oracle
The key here is the use of
LD_RUN_PATH. This obviates the need to later set
LD_LIBRARY_PATH or configure
ldconfig for cx_Oracle to find the Instant Client libraries. There is a cx_Oracle-specific variable
FORCE_RPATH which has the same effect.
Note the cx_Oracle installer overloads the meaning of
ORACLE_HOME. This variable is not normally used with Instant Client.
LD_RUN_PATH need to be set at runtime.
If you don't use
FORCE_RPATH during installation, you will need to make
LD_LIBRARY_PATH available to the Apache process or use
ldconfig to add Instant Client to the system wide library search path.
ldconfig is an effective and commonly used solution. However it has a potential problem that if multiple Oracle products exist, with possibly differing versions of Oracle libraries on the same machine, then there might be library clashes. If you wish to use it, create a file
Then update the linker cache by running:
LD_LIBRARY_PATH in Apache's environment file,
/etc/sysconfig/httpd. In Oracle Linux 6 use:
In Oracle Linux 7 use:
In Oracle Linux 7, don't reference variables on the right-hand side of the equals sign since they won't be expanded.
[The Apache environment configuration file location varies between Linux distributions. On OpenSUSE see
/etc/sysconfig/apache2. On Debian-based distributions look at
Set any other Oracle environment variables in the Apache environment configuration file
/etc/sysconfig/httpd. For example:
(Prefix any variable setting with
export in Oracle Linux 6)
yum install mod_wsgi
Add this line to
WSGIScriptAlias /wsgi_test /var/www/html/wsgi_test.py
On Oracle Linux 6, start the web server with:
service httpd start
On Oracle Linux 7 use:
systemctl start httpd.service
Create a test file
/var/www/html/wsgi_test.py that connects to your database:
#-*- coding: utf-8 -*-
db = cx_Oracle.connect("hr", "welcome", "localhost/orcl")
cursor = db.cursor()
cursor.execute("select city from locations where location_id = 2200")
def wsgi_test(environ, start_response):
output = query()
status = '200 OK'
headers = [('Content-type', 'text/plain'),
application = wsgi_test
http://localhost/wsgi_test in a browser. The city of the queried location id will be displayed.
That's it. Let me know how it works for you.
Information on cx_Oracle can be found here.
Information on Oracle Linux can be found here.
Information on Oracle Database can be found here.