当前位置: 首页 > 知识库问答 >
问题:

Python 2.7 Apache mod_wsgi

王建华
2023-03-14

我试图配置Apache来运行我的sample Flask应用程序,但遇到了一个无法解决的问题。以下是我遵循的步骤(均为root用户,以避免任何权限问题):

  1. 在CentOS上安装了httpd(yum install httpd)
  2. 使用pip安装mod\u wsgi(yum install mod\u wsgi)
  3. 在/var/www/FlaskApp/
  4. VirtualEnv内安装的烧瓶(pip安装烧瓶)
  5. 在/var/www/FlaskApp/app下创建了一个示例应用程序。py
  6. 在/var/www/FlaskApp/wsgi下创建了一个wsgi文件。py
  7. 在Apache上配置VirtualHost(httpd.conf)
  8. 重新启动Apache

wsgi文件:

activate_this = '/var/www/FlaskApp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import site

site.addsitedir('/var/www/FlaskApp/venv/lib/python2.7/site-packages')
site.addsitedir('/var/www/FlaskApp/venv/lib64/python2.7/site-packages')

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

from FlaskApp import app as application

app.py:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

httpd.conf:

<VirtualHost *:80>

     ServerName localhost

     WSGIDaemonProcess FlaskApp user=ec2-user group=ec2-user threads=2

     WSGIScriptAlias / /var/www/FlaskApp/wsgi.py

     <Directory /var/www/FlaskApp>
         Allow from all
     </Directory>

</VirtualHost>

目录结构:(/var/www/FlaskApp/)

-rw-r--r-- 1 root root  154 Dec  8 23:04 app.py
drwxr-xr-x 7 root root 4096 Dec  8 23:11 venv
-rw-r--r-- 1 root root    0 Dec  8 23:36 __init__.py
-rw-r--r-- 1 root root  356 Dec  8 23:41 wsgi.py

httpd Error\u日志中出错:

Apache/2.2.34 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.9 configured -- resuming normal operations
[client 127.0.0.1] mod_wsgi (pid=15119): Target WSGI script '/var/www/FlaskApp/wsgi.py' cannot be loaded as Python module.
[client 127.0.0.1] mod_wsgi (pid=15119): Exception occurred processing WSGI script '/var/www/FlaskApp/wsgi.py'.
[client 127.0.0.1] Traceback (most recent call last):
[client 127.0.0.1]   File "/var/www/FlaskApp/wsgi.py", line 12, in <module>
[client 127.0.0.1]     from FlaskApp import app as application
[client 127.0.0.1]   File "/var/www/FlaskApp/app.py", line 1, in <module>
[client 127.0.0.1]     from flask import Flask
[client 127.0.0.1]   File "/var/www/FlaskApp/venv/lib/python2.7/site-packages/flask/__init__.py", line 16, in <module>
[client 127.0.0.1]     from werkzeug.exceptions import abort
[client 127.0.0.1]   File "/var/www/FlaskApp/venv/lib/python2.7/site-packages/werkzeug/__init__.py", line 32
[client 127.0.0.1]      self._origin = {item: mod for mod, items in available.items() for item in items}
[client 127.0.0.1]                                  ^
[client 127.0.0.1]  SyntaxError: invalid syntax

有人能帮我解决这个问题吗?谢谢

共有1个答案

程阳平
2023-03-14

根据错误日志的第一行,您使用的是Python 2.6.9,但您的虚拟环境有Python 2.7的包。语法错误是Python 2.7中引入的判决理解。

mod_wsgi文档在这里讨论这个问题:https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version

正如furas所说,Python 2.7即将结束,因此您应该考虑在整个项目中升级到Python 3。

 类似资料:

相关问答

相关文章

相关阅读