在生产中尝试登录时出现Django“ 500内部服务器错误”

时间:2019-04-19 12:31:48

标签: django python-3.x apache2

我是django和Web设计的新手,但是我有一个在树莓派上运行的小型apache2网络服务器,我已经成功设置了与django 2.2一起使用的服务器,并且可以通过dataplicity虫洞和该网站将正确加载。但是,当我尝试通过dataplicity托管站点(管理页面或网站)登录时,出现“ 500-内部服务器错误”。

apache错误日志中没有任何内容,并且如果我通过python manage.py runserver运行服务器,则登录仅按预期运行时才出现问题。

这是我的apache2.conf:


DefaultRuntimeDir ${APACHE_RUN_DIR}

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 />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

WSGIScriptAlias / /home/pi/pi_site/pi_website/wsgi.py
WSGIPythonHome /home/pi/pi_site/myvenv
WSGIPythonPath /home/pi/pi_site

<Directory /home/pi/pi_site/pi_website>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /static/ /home/pi/pi_site/static/

<Directory /home/pi/pi_site/static>
    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

还有我的settings.py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = '*******'

DEBUG = True

ALLOWED_HOSTS = [".dataplicity.io", "127.0.0.1"]


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main_site',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'pi_website.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'pi_website.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LOGIN_REDIRECT_URL = '/'

LANGUAGE_CODE = 'en-uk'

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

修改: 我尝试设置ALLOWED_HOSTS = ["*"],但这不会影响500条消息。我还尝试删除并重新迁移数据库以创建新的超级用户,但再次没有影响

编辑-2 : 我只能在登录时收到500条消息,并且只有使用正确的凭据,如果我使用了错误的登录名/密码,页面就会正确响应

1 个答案:

答案 0 :(得分:1)

检查数据库文件的文件权限。也许Apache没有写权限。这将允许只需要读取但登录需要写入数据库的页面。通过runserver运行该文件时,它将以您的用户身份访问该文件,因此权限可以确定。