尝试使用apache和mod_wsgi部署应用程序时出现奇怪的语法错误

时间:2018-01-12 01:19:55

标签: python flask mod-wsgi apache2.4

我正在学习如何通过构建最简单的“Hello,Word!”在Ubuntu 16.04服务器上使用apache2.4和mod_wsgi来部署刻录应用程序。应用程序,但我遇到了与我的wsgi文件相关的奇怪的语法错误,我很难过。

Apache似乎运行正常但是当我点击应用程序的域时,我得到了apache的标准500错误,日志列出了以下内容:

[wsgi:error] [pid 28427:tid 140540431517440] mod_wsgi (pid=28427): Target WSGI script '/var/www/myapp.domain.com/myapp.wsgi' cannot be loaded as Python module.
[wsgi:error] [pid 28427:tid 140540431517440] mod_wsgi (pid=28427): Exception occurred processing WSGI script '/var/www/myapp.domain.com/myapp.wsgi'.
[wsgi:error] [pid 28427:tid 140540431517440] Traceback (most recent call last):
[wsgi:error] [pid 28427:tid 140540431517440]   File "/var/www/myapp.domain.com/myapp.wsgi", line 5, in <module>
[wsgi:error] [pid 28427:tid 140540431517440]     exec(file_.read(), dict(__file__=activate_this))
[wsgi:error] [pid 28427:tid 140540431517440]   File "<string>", line 4
[wsgi:error] [pid 28427:tid 140540431517440]     deactivate () {
[wsgi:error] [pid 28427:tid 140540431517440]                   ^
[wsgi:error] [pid 28427:tid 140540431517440] SyntaxError: invalid syntax

日志中引用的语法错误指向虚拟环境的激活脚本中的代码,该脚本位于./venv/bin/activate

我想知道我是否使用venv模块与virtualenv导致此问题

这是我的应用程序的wsgi文件的内容(测试是烧瓶被实例化的模块的名称):

#!/usr/bin/python3
activate_this = '/var/www/myapp.domain.com/venv/bin/activate'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, '/var/www/myapp.domain.com')

from test import app as application

3 个答案:

答案 0 :(得分:1)

即使您的名称正确,也不建议使用java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty 脚本。有关如何使用mod_wsgi的Python虚拟环境,请参阅以下文档:

确保您还使用mod_wsgi的守护进程模式。

该文档中的详细信息无法在此复制,您使用的方法取决于使用mod_wsgi的方式。

答案 1 :(得分:0)

我已经通过更改由python3 -m venv构建的虚拟环境解决了此问题。现在,我通过virtualenv创建venv,然后重新安装必要的模块。

原因是python3创建的venv没有 activate_this.py 脚本,该脚本在编写myapp.wsgi文件时很重要。

professional document中,原因如下:

    When needing to activate the Python virtual environment from within the 
    WSGI script file as described, it is preferred that you be using the either 
    virtualenv or virtualenvwrapper to create the Python virtual environment. 
    This is because they both provide the activate_this.py script file which 
    does all the work of setting up sys.path. When you use either pyvenv or 
    python -m venv with Python 3, no such activation script is provided.

祝你好运!

答案 2 :(得分:-1)

无论大括号(集合或字典显示或字典理解)如何,deactivate () {都不是合法的Python代码。当你试图运行它时,我没有看到任何奇怪或神秘的问题。