【问题标题】:Configuring django 1.8.4, python 2.76, wsgi, and apache2配置 django 1.8.4、python 2.76、wsgi 和 apache2
【发布时间】:2015-09-08 15:36:07
【问题描述】:

我的网站正在使用:

python manage.py runserver 192.168.1.128:8000

我现在正在尝试使用 wsgi 部署它。 我相信 wsgi 安装正确(它位于 /usr/lib/apache2/modules/mod_wsgi.so)

我已经设置了以下设置文件:

settings.py:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'I have no idea if this needs to be secret :-)'

# SECURITY WARNING: don't run with debug turned on in production!
# This is over-ridden in settings-production
DEBUG = True

ALLOWED_HOSTS = ['*']
ADMINS = (
    ('test', 'test@thompco.com'),
)

# Application definition

INSTALLED_APPS = (
  'django.contrib.staticfiles',
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.messages',
  'django.contrib.sessions',
  'tester',
)

MIDDLEWARE_CLASSES = (
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware',
  'django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.common.CommonMiddleware',
)

ROOT_URLCONF = 'tester.urls'

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    '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 = 'tester.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
# These values are over-ridden in settings-private
try:
  from settings_private import *
except:
  DATABASES = {
    'default': {
      'ENGINE':   'django.db.backends.mysql',
      'NAME':     'DATABASE_NAME',
      'USER':     'USER_NAME',
      'PASSWORD': 'USER_PASSWORD',
      'HOST':     'localhost',
      'PORT':     '3306',
    }
  }

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'

settings_production.py:

from settings import *

# Need the following to secure the final website (good candidate for a settings.production)
DEBUG = False
SECURE_CONTENT_TYPE_NOSNIFF =  True
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = True
X_FRAME_OPTIONS = "DENY"

settings_private.py:

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
  'default': {
    'ENGINE':   'django.db.backends.mysql',
    'NAME':     'mydatabase',
    'USER':     'myuser',
    'PASSWORD': 'mypassword',
    'HOST':     'localhost',
    'PORT':     '3306',
  }
}

如果我跑步:

python manage.py check --deploy --settings=settings_production

我明白了:

System check identified no issues (0 silenced).

所以我认为至少 django 的东西已经准备好了。现在到阿帕奇。这是我迷路的地方。

我的 django 应用程序位于 /home/jordan/django/tester(这是我正在处理的 django 文件所在的位置。)

我尝试将以下内容添加到我的 /etc/apache2/apache2.conf 中:

Alias /static/ /home/jordan/django/tester/static

<Directory /home/jordan/django/tester/static>
  Order deny,allow
  Allow from all
</Directory>

WSGIScriptAlias / /home/jordan/django/tester.wsgi

<Directory /home/jordan/django/tester>
  Order deny,allow
  Allow from all
</Directory>

我在该部分的 /etc/apache2/sites-enabled/000-default.conf 中添加了以下内容:

WSGIDaemonProcess tester
WSGIProcessGroup tester
WSGIScriptAlias / /home/jordan/django/tester/wsgi.py

然后我会这样做:

sudo /etc/init.d/apache2 restart

然后导航到 localhost/tester 并获得一个 403 禁止页面。

我很确定我在 apache 配置文件中遗漏了一些东西(可能很多!)。任何帮助都将受到欢迎。

【问题讨论】:

    标签: python django apache debian


    【解决方案1】:

    您的sites-enabled/000-default.conf 应如下所示:

    WSGIDaemonProcess tester python-path=/home/jordan/django:/path/to/venv/lib/python2.7/site-packages
    WSGIProcessGroup tester
    WSGIScriptAlias / /home/jordan/django/tester/wsgi.py
    
    
    <Directory /home/jordan/django/tester>
      <Files wsgi.py>
        Require all granted
      </Files>
    </Directory>
    
    Alias /static/ /home/jordan/django/tester/static
    
    <Directory /home/jordan/django/tester/static>
      Require all granted
    </Directory>
    

    如果您使用的是早于 2.4 的 apache,请将 Require all granted 替换为 2 行:

    Order deny,allow
    Allow from all
    

    还将 WSGIDaemonProcess 中的 /path/to/venv/ 替换为您的 virtualenv 的路径。如果不使用 virtualenv,请在此处仅保留 django 项目的路径(删除 virtualenv 和冒号的路径)。

    apache2.conf 应保留为默认值,因此请删除您添加到其中的所有内容。

    【讨论】:

    • 感谢您帮助 GwnBleidD。我没有使用任何虚拟环境,我需要对我的 WSGIDaemonProcess 进行任何不同的设置吗?
    • 另外,我在哪里浏览?我假设localhost/tester/admin 应该可以工作?
    【解决方案2】:

    原来存在多个问题。 GwynBleidD 解决了大部分问题,但我错过了拼图的最后一块:

    wsgi.py 文件应为:

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    

    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2012-04-20
      • 2021-06-30
      相关资源
      最近更新 更多