Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

Configuring Python cx_Oracle and mod_wsgi on Oracle Linux

$
0
0

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.

  1. Download Instant Client Basic & SDK ZIP files from OTN. For cx_Oracle, use the ZIPs, not the RPMs.

  2. As root, unzip the files to the same directory, e.g. /opt/oracle/instantclient_12_1:

    mkdir /opt/oracle
    cd /opt/oracle
    unzip /tmp/instantclient-basic-linux.x64-12.1.0.2.0.zip
    unzip /tmp/instantclient-sdk-linux.x64-12.1.0.2.0.zip
  3. Configure Instant Client:

    cd /opt/oracle/instantclient_12_1
    ln -s libclntsh.so.12.1 libclntsh.so
  4. Install the pip package management tool for Python by following pip.readthedocs.org/en/latest/installing.html and downloading get-pip.py. Then run:

    python get-pip.py
  5. Install cx_Oracle:

    export LD_RUN_PATH=/opt/oracle/instantclient_12_1
    export ORACLE_HOME=/opt/oracle/instantclient_12_1
    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 configureldconfig for cx_Oracle to find the Instant Client libraries. On Oracle Linux 7, which has Apache 2.4, settingLD_LIBRARY_PATH isn't practical since suEXEC is used. Configuring ldconfig is commonly used but 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.

    The cx_Oracle installer overloads the meaning ofORACLE_HOME. This variable is not normally used for Instant Client installations.

    Neither ORACLE_HOME or LD_RUN_PATH needs to be set at runtime.

  6. Install mod_wsgi:

      yum install mod_wsgi
  7. Add this line to /etc/httpd/conf/httpd.conf:

       WSGIScriptAlias /wsgi_test /var/www/html/wsgi_test.py
  8. On Oracle Linux 7, start the web server with:

      systemctl start httpd.service

    On Oracle Linux 6 use:

      service httpd start
  9. Create a test file /var/www/html/wsgi_test.py that connects to your database:

        #-*- coding: utf-8 -*-
    
        def query():
    	import cx_Oracle
    	db = cx_Oracle.connect("hr", "welcome", "localhost/orcl")
    	cursor = db.cursor()
    	cursor.execute("select city from locations where location_id = 2200")
    	return cursor.fetchone()[0]
    
        def wsgi_test(environ, start_response):
    	output = query()
    
    	status = '200 OK'
    	headers = [('Content-type', 'text/plain'),
    		   ('Content-Length', str(len(output)))]
    	start_response(status, headers)
    	yield output
    
        application = wsgi_test
    
  10. Load 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.


Viewing all articles
Browse latest Browse all 19780

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>