在阅读the django docs about apache and wsgi和Graham Dumpleton's posts后,我尝试安装django生产站点
我还花了几个小时在stackoverflow和其他论坛上解决了一个似乎很常见的错误(例如:this question seems similar),但是我所研究的解决方案都没有奏效。
我的apache服务器引发错误500,并且apache日志显示wsgi有一个ImportError
我已经用virtualenv安装了Django
Ubuntu 16.04.3
Python 3.6.3
virtualenv 16.0.0
Django 2.0.7
Apache2.4.18
mod_wsgi 3.5.1+
以下是apache日志的摘录:
[Thu Jul 12 22:42:31.354853 2018] [wsgi:error] [pid 26147:tid
140360491362048] [remote 127.0.0.1:46897] Traceback (most recent call
last):
[Thu Jul 12 22:42:31.354916 2018] [wsgi:error] [pid 26147:tid
140360491362048] [remote 127.0.0.1:46897] File
"/home/david/DjangoProjects/unflat/unflat/wsgi.py", line 29, in
<module>
[Thu Jul 12 22:42:31.380265 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] mod_wsgi (pid=26147): Target
WSGI script '/home/david/DjangoProjects/unflat/unflat/wsgi.py' cannot
be loaded as Python module.
[Thu Jul 12 22:42:31.380351 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] mod_wsgi (pid=26147):
Exception occurred processing WSGI script
'/home/david/DjangoProjects/unflat/unflat/wsgi.py'.
[Thu Jul 12 22:42:31.380514 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] Traceback (most recent call
last):
[Thu Jul 12 22:42:31.380570 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] File
"/home/david/DjangoProjects/unflat/unflat/wsgi.py", line 29, in
<module>
[Thu Jul 12 22:42:31.380587 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] from django.core.wsgi
import get_wsgi_application
[Thu Jul 12 22:42:31.3 80634 2018] [wsgi:error] [pid 26147:tid
140360373733120] [remote 127.0.0.1:46641] ImportError: No module named
'django'
这是我的wsgi.py文件:
import os
import time
import traceback
import signal
import sys
path='/home/david/DjangoProjects/unflat'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "unflat.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
和httpd.conf文件:
Alias /static/ /home/david/DjangoProjects/static/
<Directory /home/david/DjangoProjects/static>
Require all granted
</Directory>
WGIScriptAlias / /home/david/DjangoProjects/unflat/unflat/wsgi.py
WSGIPythonPath /home/david/DjangoProjects/unflat/
<Directory /home/david/DjangoProjects/unflat/unflat>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
至少这里是000-default.com:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/david/DjangoProjects/unflat/static
<Directory /home/david/DjangoProjects/unflat/static>
Require all granted
</Directory>
<Directory /home/david/DjangoProjects/unflat/unflat>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess unflat python-home=/home/david/PythonEnv/djangounflatenv python-path=/home/david/DjangoProjects/unflat
WSGIProcessGroup unflat
WSGIScriptAlias / /home/david/DjangoProjects/unflat/unflat/wsgi.py process-group=unflat application-group=%{GLOBAL}
</VirtualHost>
如果有人可以帮助我发现所有这些东西出了问题,那将是巨大的,并且非常感谢。
谢谢。
答案 0 :(得分:0)
由于此类问题可能有多个因素,并且取决于一组配置,因此我将展示现在可以解决的问题。我还添加了金枪鱼
django方面
staging_requirements.txt
-r base_requirements.txt
whitenoise>=3.3.1
mod_wsgi-httpd>=2.4.27.1
mod_wsgi>=4.6.4
gunicorn>=19.9.0
staging_settings.py
from .base_settings import *
DEBUG = False
ALLOWED_HOSTS += ['192.168.1/24','unflat.net','*']
FORCE_SCRIPT_NAME = '/unflat'
INSTALLED_APPS += ['mod_wsgi.server',]
MIDDLEWARE += [
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.BrokenLinkEmailsMiddleware',
]
WSGI_APPLICATION = 'unflat.wsgi.application'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
apache端
/etc/apache2/httpd.conf
LoadModule wsgi_module "/home/djangouser/DjangoProjects/unflatenv/lib/python3.6/site$
WSGIPythonHome "/home/djangouser/DjangoProjects/unflatenv/lib/python3.6"
/etc/apache2/apache2.conf
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
ServerName unflat.net
WSGISocketPrefix /var/run/wsgi
etc / apache2 / sites-enabled / unflat.conf
<VirtualHost *:80 *:8080>
ServerName unflat.net
ServerAdmin webmaster@unflat.net
DocumentRoot "/home/djangouser/DjangoProjects/unflat"
ErrorLog ${APACHE_LOG_DIR}/unflat-error.log
CustomLog ${APACHE_LOG_DIR}/unflat-access.log combined
Alias /static "/home/djangouser/DjangoProjects/unflat/static"
Alias /uploads "/home/djangouser/DjangoProjects/unflat/uploads"
<Directory "/home/djangouser/DjangoProjects/unflat">
Require all granted
</Directory>
<Directory "/home/djangouser/DjangoProjects/unflat/static">
Require all granted
</Directory>
ProxyPass /static !
ProxyPass /uploads !
ProxyPass /unflat http://localhost:8000
/etc/apache2/mods-available/wsgi.load
LoadModule wsgi_module /home/djangouser/DjangoProjects/unflatenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpyt$
...至少启动python virtualenv,gunicorn和apache以启动服务器:
source /home/djangouser/DjangoProjects/unflatenv/bin/activate
gunicorn unflat.wsgi --daemon
sudo systemctl start apache2
#sudo apachectl restart