在Ubuntu上使用Flask在Apache上运行

时间:2016-09-03 00:17:49

标签: python apache ubuntu flask mod-wsgi

无法让Flask在Apache上运行。按照指示here设置mod_wsgi。我将我想要的python代码放入/var/www/TDAA_reminder/TDAA_reminder/__init__.py。如果我通过sudo python __init__.py手动运行代码并使用ngrok它会收到一个twilio帖子,所以我知道代码正在运行。但不适用于mod_wsgi。系统已自动生成__init__.pyc,所以我觉得它可能有点工作,但我仍然坚持下一步调试的地方。我已经多次重新加载并重新启动了apache。回到ngrok,以确保我没有破坏任何东西。谢谢!

教程中的目录结构有点偏离我的预期:

|--------TDAA_reminder
|----------------TDAA_reminder
|-----------------------static
|-----------------------templates
|-----------------------TDAAenv
|-----------------------__init__.py
|----------------flaskapp.wsgi

twilio中的错误是11200 HTTP retrieval failure

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
        <title>500 Internal Server Error</title>
    </head>
    <body>
        <h1>Internal Server Error</h1>
        <p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
        <p>Please contact the server administrator at 
 admin@andrewtclaus.com to inform them of the time this error occurred,
 and the actions you performed just before this error.</p>
        <p>More information about this error may be available
in the server error log.</p>
        <hr>
            <address>Apache/2.4.7 (Ubuntu) Server at andrewtclaus.com Port 80</address>
        </body>
    </html>

我的钩子指向http://andrewtclaus.com/TDAA_reminder

摘自/var/www/TDAA_reminder/TDAA_reminder/__init__.py

app = Flask(__name__)

@app.route('/TDAA_reminder', methods=['POST'])
def TDAA_reminder():


/etc/apache2/sites-available/TDAA_reminder.conf

<VirtualHost *:80>
                ServerName AndrewTClaus.com
                ServerAdmin admin@andrewtclaus.com
                WSGIScriptAlias / /var/www/TDAA_reminder/TDAA_reminder.wsgi
                <Directory /var/www/TDAA_reminder/TDAA_reminder/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/TDAA_reminder/TDAA_reminder/static
                <Directory /var/www/TDAA_reminder/TDAA_reminder/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


/var/www/TDAA_reminder/TDAA_reminder.wsgi

#!/usr/bin/python

activate_this = '/var/www/TDAA_reminder/TDAA_reminder/TDAAenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/TDAA_reminder/")

from TDAA_reminder import app as application


Apache错误代码:

[Sat Sep 03 01:53:23.598479 2016] [:error] [pid 17691] [client 192.0.101.226:16488] mod_wsgi (pid=17691): Target WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi' cannot be loaded as Py$
[Sat Sep 03 01:53:23.598508 2016] [:error] [pid 17691] [client 192.0.101.226:16488] mod_wsgi (pid=17691): Exception occurred processing WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi'.
[Sat Sep 03 01:53:23.598526 2016] [:error] [pid 17691] [client 192.0.101.226:16488] Traceback (most recent call last):
[Sat Sep 03 01:53:23.598542 2016] [:error] [pid 17691] [client 192.0.101.226:16488]   File "/var/www/TDAA_reminder/TDAA_reminder.wsgi", line 11, in <module>
[Sat Sep 03 01:53:23.598565 2016] [:error] [pid 17691] [client 192.0.101.226:16488]     from TDAA_reminder import app as application
[Sat Sep 03 01:53:23.598574 2016] [:error] [pid 17691] [client 192.0.101.226:16488]   File "/var/www/TDAA_reminder/TDAA_reminder/__init__.py", line 37, in <module>
[Sat Sep 03 01:53:23.598639 2016] [:error] [pid 17691] [client 192.0.101.226:16488]     flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
[Sat Sep 03 01:53:23.598658 2016] [:error] [pid 17691] [client 192.0.101.226:16488] AttributeError: 'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'
[Sat Sep 03 01:57:16.917248 2016] [:error] [pid 17688] [client 54.165.223.88:40658] mod_wsgi (pid=17688): Target WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi' cannot be loaded as Py$
[Sat Sep 03 01:57:16.917277 2016] [:error] [pid 17688] [client 54.165.223.88:40658] mod_wsgi (pid=17688): Exception occurred processing WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi'.
[Sat Sep 03 01:57:16.917308 2016] [:error] [pid 17688] [client 54.165.223.88:40658] Traceback (most recent call last):
[Sat Sep 03 01:57:16.917324 2016] [:error] [pid 17688] [client 54.165.223.88:40658]   File "/var/www/TDAA_reminder/TDAA_reminder.wsgi", line 11, in <module>
[Sat Sep 03 01:57:16.917346 2016] [:error] [pid 17688] [client 54.165.223.88:40658]     from TDAA_reminder import app as application
[Sat Sep 03 01:57:16.917370 2016] [:error] [pid 17688] [client 54.165.223.88:40658]   File "/var/www/TDAA_reminder/TDAA_reminder/__init__.py", line 37, in <module>
[Sat Sep 03 01:57:16.917384 2016] [:error] [pid 17688] [client 54.165.223.88:40658]     flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
[Sat Sep 03 01:57:16.917400 2016] [:error] [pid 17688] [client 54.165.223.88:40658] AttributeError: 'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'

与调试相关的摘录:

SCOPES = ('https://www.googleapis.com/auth/calendar')
store = file.Storage('cal_auth.json')
creds = store.get()

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))

1 个答案:

答案 0 :(得分:0)

通过以下讨论回答评论:

  

Apache错误日志文件中的错误是什么?如果指南说   有两次,指南在技术上是错误的。它一定要是   / var / www / TDAA_reminder因为它是WSGI脚本文件的目录   如果没有它,Apache就不应该为你的WSGI提供服务   申请,因为它是被禁止的。如果它无论如何都有效,   您在Apache配置中的其他位置安全性不高   让你的服务器有一点风险,允许访问任何东西   文件系统。

如果我读得正确,'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'但是当我做python __init__.py时,它可以工作,并且在使用ngrok隧道时我没有错误,所以我很困惑,为什么这是错误?

  

将我文件中的路径更改为绝对路径并且有效!