我正试图通过apache和mod_wsgi来托管我的python 3.4 flask应用程序。通过flask自己的服务器运行应用程序工作正常。该应用程序是在虚拟环境pyvenv-3.4中制作的。
但是,尝试在浏览器中连接到apache服务器时,会引发500 http错误。配置和日志附加。我认为这与使用pyvenv而不是virtualenv(来自pip)有关。 Flask文档告诉我使用此行激活虚拟环境
activate_this = '/path/to/env/bin/activate_this.py'
但是,由于文件不存在,会产生IOError。我尝试将它指向'activate'文件而不是激活。激活。所有文件都在deactivate行上生成SyntaxError。
如何使用我的virtualenv通过Apache运行此应用程序?
flaskapp.wsgi
#!/usr/bin/python
activate_this = '/var/www/FlaskApp/FlaskApp/bin/activate'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'some secret key'
Apache VirtualHost
<VirtualHost *:80>
ServerName example.org # my server name
ServerAlias gallifrey 192.168.0.84
ServerAdmin admin@example.org # my admin
WSGIScriptAlias /flask /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/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/FlaskApp
flaskapp.wsgi
FlaskApp/
bin/
activate
activate.csh
activate.fish
easy_install, easy_install-3.4
pip, pip3, pip3.4
python, python3, python3.4
include/
lib/
scripts/
static/
templates/
app.py
__init__.py
尝试打开网页时出现http 500错误:
Apache error.log
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] mod_wsgi (pid=31629): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] mod_wsgi (pid=31629): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] Traceback (most recent call last):
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] File "/var/www/FlaskApp/flaskapp.wsgi", line 3, in <module>
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] execfile(activate_this, dict(__file__=activate_this))
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] File "/var/www/FlaskApp/FlaskApp/bin/activate", line 4
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] deactivate () {
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] ^
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] SyntaxError: invalid syntax
答案 0 :(得分:1)
您可以关注VirtualEnvironments instructions,也可以复制virtualenv
activate_this.py
脚本执行的操作:
import sys
import os
old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if sys.platform == 'win32':
site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
你可以使这个功能更加通用:
import sys
import os
def activate_venv(path):
if sys.platform == 'win32':
bin_dir = os.path.join(path, 'Scripts')
site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
bin_dir = os.path.join(path, 'bin')
site_packages = os.path.join(BASE, 'lib', 'python%s' % sys.version[:3], 'site-packages')
os.environ['PATH'] = bin_dir + os.pathsep + os.environ['PATH']
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.prefix, sys.real_prefix = path, sys.prefix
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
将它放在默认Python模块搜索路径的模块中,导入activate_venv
并传入os.path.dirname(os.path.abspath(__file__))
的结果:
from somemodule import activate_venv
import os.path
activate_venv(os.path.dirname(os.path.abspath(__file__)))
答案 1 :(得分:1)
你有没有尝试过:
WSGIPythonHome /var/www/FlaskApp
只是让mod_wsgi处理它或者你设置它?
答案 2 :(得分:1)
当您使用Python虚拟环境时,使用Django或Flask等应用程序时,您必须确保Apache获取WSGI路径的位置。
如果你有一个Debian发行版并使用包管理器来安装Apache,你应该有一个配置文件的目录结构,如下所示: 的/ etc / apache2的/.
编辑“/etc/apache2/sites-available/AppName.conf”下的VirtualHost配置文件(例如AppName.conf),并按照以下示例获取Python v3.5下的Flask应用程序:
WSGIPythonPath /<PATH_OF_PYTHON_VIRTUAL_ENVIRONMENT>/lib/python3.5/site-packages
<VirtualHost *:80>
ServerName <HOST or IP_ADDRESS>
ServerAdmin your-email@domain.com
WSGIScriptAlias / /var/www/app_name/your_wsgi_app.wsgi
WSGIPassAuthorization On
<Directory /var/www/app_name/AppName/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/app_name/AppName/static
<Directory /var/www/app_name/AppName/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# e.g. LogLevel debug
LogLevel <info|warn|error|debug>
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
答案 3 :(得分:0)
我尝试在/etc/httpd/conf/httpd.conf中添加上面和外面的内容并且它有效。
WSGIPythonPath /your_virtualenv/lib/python2.7/site-package