I'm trying to host a simple Python Flask website that connects to an Oracle DB.
So far I've managed to install cx_Oracle (and the Oracle Client and SDK) on the Ubuntu host and host the Flask app successfully using Apache2 and mod_wsgi.
I followed this guide to install cx_Oracle: https://blogs.oracle.com/opal/entry/configuring_python_cx_oracle_and I followed this tutorial to host: http://blog.garethdwyer.co.za/2013/07/getting-simple-flask-app-running-on.html
The entire application functions perfectly when run locally from the instance. I've used x-forwarding and firefox to test this.
However, when I try to host publicly, running through wsgi, it can load the flask app, but fails when asked to access the Oracle database. I can't get error logs, since the Apache logs only show what I manually print out through Python. (If anyone has a fix for THAT, please tell).
I've spent far too much time trying to host - this should be much simpler than it is. Solutions?
My directory structure
->/var/www/SACK
->app.py
->app.wsgi
->satic/
->templates/
->/etc/apache2/sites-available
->sitename.com.config
->/opt/oracle/instant_client_12_1
->... Oracle client stuff. SDK included.
app.py
app = Flask(__name__)
...
def connectToDB():
ip = '...'
port = ...
SID = '...'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
global connection
connection = cx_Oracle.connect('...', '...', dsn_tns)
print "connection successful"
global cursor
cursor = connection.cursor()
def closeConnection():
cursor.close()
connection.close()
print "connection closed"
def main():
connectToDB()
app.run()
closeConnection()
if __name__ == '__main__':
main()
From those guides, here is how my app is set up for wsgi:
app.wsgi
import sys
sys.path.insert(0, '/var/www/SACK')
from app import app as application
Site Config:
sitename.com.config
<VirtualHost *:80>
WSGIDaemonProcess SACK
WSGIScriptAlias / /var/www/SACK/app.wsgi
<Directory /var/www/SACK>
WSGIProcessGroup SACK
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
LogLevel info
ErrorLog "/var/log/apache2/error.log"
CustomLog "/var/log/apache2/access.log" combine
</VirtualHost>
My wsgi is configured to only run the actual Flask app itself, aka, the code contained within app.run()
, therefore the connectToDB()
function never actually runs when wsgi creates the application.
Rather than fixing this in my wsgi config, I used flask's @app.before_request() and @app.teardown_request annotations to create and destroy the DB connection.
The changes in code:
@app.before_request()
def connectToDB():
....
@app.teardown_request()
def closeConnection(e):
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.