首页 > mod_wsgi (Apache)使用虚拟环境出现问题

mod_wsgi (Apache)使用虚拟环境出现问题

在apache2上部署flask的时候参考了官方文档,这里附上链接
http://www.pythondoc.com/flask/deploying/mod_wsgi.html
打算使用虚拟环境,官方文档给出了下面的语句

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

但是execfile这个函数在python3.x中已经移除,我用下面的语句代替

exec(compile(open(activate_this, "rb").read(), activate_this, 'exec'), dict(__file__=activate_this))

不过apache访问的时候,出现下面的错误

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

我查看了apache的error日志,相关内容如下

[Fri Mar 25 15:36:31.472625 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292] mod_wsgi (pid=18269): Target WSGI script '/var/www/ams/manage.wsgi' cannot be loaded as Python module.
[Fri Mar 25 15:36:31.472798 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292] mod_wsgi (pid=18269): Exception occurred processing WSGI script '/var/www/ams/manage.wsgi'.
[Fri Mar 25 15:36:31.485358 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292] Traceback (most recent call last):
[Fri Mar 25 15:36:31.485563 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]   File "/var/www/ams/manage.wsgi", line 7, in <module>
[Fri Mar 25 15:36:31.485575 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]     from manage import app as application
[Fri Mar 25 15:36:31.485703 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]   File "/var/www/ams/manage.py", line 3, in <module>
[Fri Mar 25 15:36:31.485711 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]     from app import create_app
[Fri Mar 25 15:36:31.485810 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]   File "/var/www/ams/app/__init__.py", line 1, in <module>
[Fri Mar 25 15:36:31.485843 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292]     from flask import Flask
[Fri Mar 25 15:36:31.485873 2016] [:error] [pid 18269:tid 139683455108864] [remote 127.0.0.1:59292] ImportError: No module named 'flask'

内容中出现了ImportError: No module named 'flask'的错误,说明虚拟环境没起作用。我在终端venv模式下执行python app.py是能正常接受访问的。麻烦各位帮我看看python3如何替代python2.x中的execfile这个函数,或者我的步骤里有没有出现什么问题,多谢了。


上面的方法行不通后,我又在网上爬了些文章,无意中看到使用虚拟环境还有另外一种方式,就是在apache的配置文件中做出一些配置。下面贴下我的配置文件的相应内容:

<VirtualHost *:81>
    ServerName localhost

    ServerAdmin webmaster@localhost

    WSGIDaemonProcess ams user=www-data group=www-data threads=5 python-path=/var/www/ams/venv/lib/python3.5/site-packages
    WSGIScriptAlias / /var/www/ams/manage.wsgi

    <Directory /var/www/ams>
        WSGIProcessGroup ams
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

其中,python-path=/var/www/ams/venv/lib/python3.5/site-packages用到的就是虚拟环境中的path。
自问自答...主要是标记下,估计以后还会用到,也希望能帮到有同样问题的朋友。

【热门文章】
【热门文章】