简体   繁体   English

使用cx_Oracle和mod_wsgi在AWS Ubuntu(EC2)上托管Python Flask应用

[英]Hosting a Python Flask App on AWS Ubuntu (EC2) using cx_Oracle and mod_wsgi

I'm trying to host a simple Python Flask website that connects to an Oracle DB. 我正在尝试托管一个连接到Oracle DB的简单Python Flask网站。

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. 到目前为止,我已经设法在Ubuntu主机上安装了cx_Oracle(以及Oracle Client和SDK),并使用Apache2和mod_wsgi成功托管了Flask应用程序。

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 我按照此指南安装了cx_Oracle: https : //blogs.oracle.com/opal/entry/configuring_python_cx_oracle_,并且我按照本教程进行了托管: 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. 我已经使用x-forwarding和firefox对此进行了测试。

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. 但是,当我尝试通过wsgi运行公共主机时,它可以加载flask应用程序,但是在要求访问Oracle数据库时失败。 I can't get error logs, since the Apache logs only show what I manually print out through Python. 我无法获取错误日志,因为Apache日志仅显示我通过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.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: 根据这些指南,这是为wsgi设置我的应用程序的方式:

app.wsgi app.wsgi

import sys
sys.path.insert(0, '/var/www/SACK')

from app import app as application

Site Config: 站点配置:

sitename.com.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. 我的wsgi配置为仅运行实际的Flask应用程序本身,也就是app.run()包含的代码,因此,当wsgi创建应用程序时, connectToDB()函数实际上不会运行。

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. 我没有在wsgi配置中解决此问题,而是使用flask的@ app.before_request()@ app.teardown_request批注创建和销毁了数据库连接。

The changes in code: 代码更改:

@app.before_request()
def connectToDB():
    ....

@app.teardown_request()
def closeConnection(e):
    ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM