我安装了flask(在虚拟环境中)并设置了WSGISocketPrefix,终于让它工作了。我正在centos6.8上部署,使用虚拟环境中的python3.6。
这里提到的项目文件夹是存储实际django代码的地方。此处引用的项目公共文件夹由apache访问,并包含指向相关资源的符号链接,以及执行所需的.htaccess和.wsgi文件。
套接字前缀可能因操作系统和apache配置而异。如果有问题,权限可能会因apache版本而异,您可以进行更改:
Order allow,deny
Allow from all
to
Require all granted
这是我的mod_wsgi配置(httpd.conf):
LoadModule wsgi_module **path to venv**/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGIPythonHome **path to venv**
WSGIDaemonProcess flask user=**user name** group=**user group**
WSGIProcessGroup flask
WSGISocketPrefix /var/run/wsgi
<Directory **path-to-project-dir**>
Options ExecCGI MultiViews Indexes
MultiViewsMatch Handlers
AddHandler wsgi-script .py
AddHandler wsgi-script .wsgi
DirectoryIndex index.html index.php index.py app.wsgi
Order allow,deny
Allow from all
</Directory>
这是虚拟主机(httpd.conf)的配置文件。
<VirtualHost *:80>
DocumentRoot **project public folder**
ServerName **www.project.com**
ServerAlias **project.com**
WSGIScriptAlias / **project public folder**/site.wsgi
Alias /media/ **project public folder**/media/
<Directory **project public folder**/media>
Order allow,deny
Allow from all
</Directory>
Alias /static/ **project public folder**/static/
<Directory **project public folder**/static>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
这是site.wsgi文件。
import os
import sys
import site
site.addsitedir('**path to venv**/lib/python3.6/site-packages')
sys.path.append('**path to project folder containing manage.py**')
sys.path.append('**path to project folder containing settings.py**')
os.environ['DJANGO_SETTINGS_MODULE'] = '**project name**.settings'
activate_env=os.path.expanduser("**path to venv**/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我不得不将以下两行代码从wsgi.py移动到同一文件夹中的“
init.py”中,以解决“应用程序未准备好错误”。
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
这是虚拟环境中软件包的列表。
Click==7.0
Django==2.2.1
django-debug-toolbar==1.11
django-redis==4.10.0
django-tastypie==0.14.2
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
mod-wsgi==4.6.5
mysqlclient==1.4.2.post1
Pillow==6.0.0
pip==19.1.1
python-dateutil==2.8.0
python-mimeparse==1.6.0
pytz==2019.1
redis==3.2.1
setuptools==41.0.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
Werkzeug==0.15.4