mod_wsgi 和 apache2 的服务器内部错误

server internal error with mod_wsgi and apache2

我一直在尝试在具有 mod_wsgi 和 apache2 的服务器上部署 django-oscar 的沙箱网站。

问题是,每当我打开该网站的地址时,我都会在日志中收到 500 错误和以下内容:

[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200] mod_wsgi (pid=28331): Exception occurred processing WSGI script '/tmp/mod_wsgi-localhost:8001:1000/htdocs/en-gb'.
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200] Traceback (most recent call last):
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/local/lib/python2.7/dist-packages/mod_wsgi/server/__init__.py", line 1299, in handle_request
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     return self.application(environ, start_response)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/wsgi.py", line 24, in application
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     return _application(environ, start_response)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     response = self.get_response(request)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 199, in get_response
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 231, in handle_uncaught_exception
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     'request': request
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 1166, in error
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     self._log(ERROR, msg, args, **kwargs)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 1258, in _log
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     self.handle(record)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 1268, in handle
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     self.callHandlers(record)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 1308, in callHandlers
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     hdlr.handle(record)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 748, in handle
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     self.emit(record)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 929, in emit
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     self.stream = self._open()
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/local/lib/python2.7/dist-packages/oscar/core/logging/handlers.py", line 26, in _open
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     return BaseFileHandler._open(self)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]   File "/usr/lib/python2.7/logging/__init__.py", line 916, in _open
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200]     stream = open(self.baseFilename, self.mode)
[Fri May 22 16:53:48 2015] [error] [client 2.190.60.200] IOError: [Errno 13] Permission denied: '/var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/logs/errors.log'

现在我不知道我做错了什么,因为我在这方面很陌生。我知道同时有两件事是错误的!一是脚本出现异常,二是权限问题无法写入日志文件!

现在这是我的 wsgi.py 脚本:

import os
import sys
import site
import urllib

sys.stdout = sys.stderr

# Project root
root = '/var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox'
sys.path.insert(0, root)

# Packages from virtualenv
activate_this = '/var/www/setak/setakenv/setakmain/django-oscar/oscar/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

# Set environmental variable for Django and fire WSGI handler
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
#import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()

def application(environ, start_response):
    environ['PATH_INFO'] = urllib.unquote(environ['REQUEST_URI'].split('?')[0])
    return _application(environ, start_response)

这是我的 apache2 配置:

<VirtualHost *:8080>
        WSGIDaemonProcess setak python-path=/var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox:/var/www/setak/setakenv/lib/python2.7/site-packages
        WSGIProcessGroup setak
        WSGIScriptAlias / /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/wsgi.py
        ServerAdmin admin@setakshop.ir
        ServerName shop.setakshop.ir

        Alias /robots.txt /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/static/robots.txt

        Alias /media/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media/
        Alias /static/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static/

        <Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static>
                Order allow,deny
                allow from all
        </Directory>

        <Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media>
                Order allow,deny
                allow from all
        </Directory>

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

我也运行这个项目用命令:

mod_wsgi-express start-server wsgi.py --port=8001     --user www-data --group www-data

还有更简单的命令:

mod_wsgi-express start-server wsgi.py --port=8001

但同样的事情发生了。

我不知道自己做错了什么,如有任何帮助,我们将不胜感激。提前致谢。

尝试将权限更改为 777。但不推荐这样做。(尝试更改所有者是个好方法。)

sudo chmod 777 /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/logs/errors.log