Django 1.8 mod_wsgi无法写入日志文件

时间:2015-10-26 16:28:34

标签: django apache mod-wsgi file-permissions

我为在debian上运行的django服务器提供了以下设置:

Apache配置:

<VirtualHost *:8083>
    ServerAdmin <mail>
    ServerName  <ip>
    ServerAlias http://<ip>
    <Directory /var/www/backend/api/backend/ >
            AllowOverride All
            Require all granted
            Allow from all
    </Directory>
    WSGIScriptAlias / /var/www/backend/api/backend/apache/django.wsgi
    ErrorLog /var/www/backend/api/backend/error.log
</VirtualHost>

具有访问权限的后端文件夹:

drwxrwxr-x 7 www-data www-data   4096 Okt 26 16:23 .
drwxrwxr-x 3 www-data www-data   4096 Sep 29 19:58 ..
drwxrwxr-x 2 www-data www-data   4096 Sep 29 21:47 apache
drwxrwxr-x 9 www-data www-data   4096 Sep 30 16:13 archiv_backend
-rwxrwxr-x 1 www-data www-data  72704 Sep 29 21:56 archiv.db
-rwxrwxrwx 1 www-data www-data   1927 Okt  5 16:59 archiv.log
-rwxrwxr-x 1 www-data www-data 941822 Sep 29 21:26 archiv.log.2015-09-29
-rwxrwxr-x 1 www-data www-data 489231 Sep 30 15:07 archiv.log.2015-09-30
-rwxrwxr-x 1 www-data www-data   3805 Okt  2 23:14 archiv.log.2015-10-02
-rwxrwxr-x 1 www-data www-data  15555 Okt  3 22:40 archiv.log.2015-10-03
drwxrwxr-x 2 www-data www-data   4096 Sep 30 15:56 backend
-rwxrwxr-x 1 www-data www-data 255417 Okt 26 17:17 error.log
drwxrwxr-x 8 www-data www-data   4096 Okt 26 16:29 .git
drwxrwxr-x 3 www-data www-data   4096 Sep 29 19:58 .idea
-rwxrwxr-x 1 www-data www-data    250 Sep 29 19:58 manage.py
-rwxrwxr-x 1 www-data www-data    182 Sep 30 15:25 requirements.txt
-rwxrwxr-x 1 www-data www-data      0 Okt 26 16:29 wsgi.py

在apache2.conf中我添加了:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
<Directory /var/www/backend/api/backend/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

当我在端口8083上向服务器发送请求时,我总是在error.log中看到以下错误:

[Mon Oct 26 16:23:28.171069 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892] mod_wsgi (pid=13469): Target WSGI script '/var/www/backend/api/backend/apache/django.wsgi' cannot be loaded as Python module.
[Mon Oct 26 16:23:28.171113 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892] mod_wsgi (pid=13469): Exception occurred processing WSGI script '/var/www/backend/api/backend/apache/django.wsgi'.
[Mon Oct 26 16:23:28.171142 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892] Traceback (most recent call last):
[Mon Oct 26 16:23:28.171169 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/var/www/backend/api/backend/apache/django.wsgi", line 16, in <module>
[Mon Oct 26 16:23:28.171208 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     application = get_wsgi_application()
[Mon Oct 26 16:23:28.171278 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Oct 26 16:23:28.171381 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     django.setup()
[Mon Oct 26 16:23:28.171418 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
[Mon Oct 26 16:23:28.171443 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Oct 26 16:23:28.171478 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 86, in configure_logging
[Mon Oct 26 16:23:28.171513 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     logging_config_func(logging_settings)
[Mon Oct 26 16:23:28.171568 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
[Mon Oct 26 16:23:28.171591 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     dictConfigClass(config).configure()
[Mon Oct 26 16:23:28.171601 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]   File "/usr/lib/python2.7/logging/config.py", line 576, in configure
[Mon Oct 26 16:23:28.171615 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892]     '%r: %s' % (name, e))
[Mon Oct 26 16:23:28.171654 2015] [wsgi:error] [pid 13469:tid 139695967143680] [client <ip>:56892] ValueError: Unable to configure handler 'logfile': [Errno 13] Permission denied: '/archiv.log'
[Mon Oct 26 16:23:28.315954 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894] mod_wsgi (pid=13469): Target WSGI script '/var/www/backend/api/backend/apache/django.wsgi' cannot be loaded as Python module., referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.315978 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894] mod_wsgi (pid=13469): Exception occurred processing WSGI script '/var/www/backend/api/backend/apache/django.wsgi'., referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.315995 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894] Traceback (most recent call last):, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316013 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/var/www/backend/api/backend/apache/django.wsgi", line 16, in <module>, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316034 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     application = get_wsgi_application(), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316124 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316192 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     django.setup(), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316207 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316300 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316336 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 86, in configure_logging, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316357 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     logging_config_func(logging_settings), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316367 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316429 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     dictConfigClass(config).configure(), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316485 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]   File "/usr/lib/python2.7/logging/config.py", line 576, in configure, referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316523 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894]     '%r: %s' % (name, e)), referer: http://<ip>:8083/
[Mon Oct 26 16:23:28.316548 2015] [wsgi:error] [pid 13469:tid 139695958750976] [client <ip>:56894] ValueError: Unable to configure handler 'logfile': [Errno 13] Permission denied: '/archiv.log', referer: http://<ip>:8083/

这是我的WSGI文件:

import os
import sys

path = '/var/www/backend/api/backend/'
if path not in sys.path:
    sys.path.insert(0, '/var/www/backend/api/backend/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'backend.settings'
os.environ['ARCHIV_ENVIRONMENT'] = 'production'

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

我找到的一个解决方案是在/ var / www /目录中chown或chgrp文件夹或日志文件。正如您所看到的,现在拥有的所有文件以及wsgi应该使用的grp www-data。 但我仍然得到错误。我没有忘记重启apache。

1 个答案:

答案 0 :(得分:2)

根据错误消息,它正在尝试写入/archiv.log。仔细查看settings.py中的日志记录信息,了解它为什么要写入根目录。