在 Apache 上部署 Django?
Deploy Django on Apache?
我使用 Django 1.8 创建项目。现在,我想将它部署到 server.when 我 运行 这个命令在 Ubuntu 找到所有工作的东西。
python manage.py runserver
接下来,我在 Apache 中创建 vertuelhost 和 enbal key.com 站点。
虚拟主机文件代码。 ( key.com.conf )
<VirtualHost *:80>
ServerAdmin admin@key.com
ServerName key.com
ServerAlias www.key.com
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
下图显示了我的项目文件夹结构。
现在,我 运行,key.com url,它给出了 500 个服务器内部错误。我将检查 Apache 错误日志 fie。它包含以下内容。
[Tue Nov 24 15:16:50.317896 2015] [:error] [pid 12505] [client 127.0.0.1:37918] mod_wsgi (pid=12505): Exception occurred processing WSGI script '/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py'.
[Tue Nov 24 15:16:50.317982 2015] [:error] [pid 12505] [client 127.0.0.1:37918] Traceback (most recent call last):
[Tue Nov 24 15:16:50.318011 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Tue Nov 24 15:16:50.318117 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self.load_middleware()
[Tue Nov 24 15:16:50.318131 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[Tue Nov 24 15:16:50.318236 2015] [:error] [pid 12505] [client 127.0.0.1:37918] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue Nov 24 15:16:50.318249 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Tue Nov 24 15:16:50.318325 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self._setup(name)
[Tue Nov 24 15:16:50.318337 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Tue Nov 24 15:16:50.318355 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self._wrapped = Settings(settings_module)
[Tue Nov 24 15:16:50.318364 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Tue Nov 24 15:16:50.318380 2015] [:error] [pid 12505] [client 127.0.0.1:37918] % (self.SETTINGS_MODULE, e)
[Tue Nov 24 15:16:50.318399 2015] [:error] [pid 12505] [client 127.0.0.1:37918] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings
期待专家的帮助。
=======================wsgi.py============
"""
WSGI config for test2 project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os,sys
sys.path.append('/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
===================setting.py===================== =============
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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 = '-vvzx5(=sztv%*^ljnpkdc&!q+ltws0-%a76^v1e_z9g97-k2%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marcador',
'crispy_forms',
'bootstrap_pagination',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Berlin'
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/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
LOGIN_URL = 'mysite_login'
LOGOUT_URL = 'mysite_logout'
LOGIN_REDIRECT_URL = 'marcador_bookmark_list'
CRISPY_TEMPLATE_PACK = 'bootstrap3'
在您的 wsgi.py 中尝试更改
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
至
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
因为两个文件都在同一个 directory/module.
正如我在评论中所说,您没有做任何事情来将您的项目添加到 PYTHONPATH,以便 WSGI 应用程序可以实际找到它。
从你的目录结构看来,你正在使用一个virtualenv;您需要在 .wsgi 文件或 Apache 配置本身中激活它。 Django 文档 recommend the latter.
我终于可以尽早成功部署启用了 project.I 的 Apache 这个模块。
refer this link
----------------这是我的虚拟主机文件------------------------ --------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin@key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
----------------wsgi.py-------------------- --------------
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
我认为这会对其他 Django 有所帮助 developers.Thank 你们所有帮助过我的开发人员。
我使用 Django 1.8 创建项目。现在,我想将它部署到 server.when 我 运行 这个命令在 Ubuntu 找到所有工作的东西。
python manage.py runserver
接下来,我在 Apache 中创建 vertuelhost 和 enbal key.com 站点。
虚拟主机文件代码。 ( key.com.conf )
<VirtualHost *:80>
ServerAdmin admin@key.com
ServerName key.com
ServerAlias www.key.com
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
下图显示了我的项目文件夹结构。
现在,我 运行,key.com url,它给出了 500 个服务器内部错误。我将检查 Apache 错误日志 fie。它包含以下内容。
[Tue Nov 24 15:16:50.317896 2015] [:error] [pid 12505] [client 127.0.0.1:37918] mod_wsgi (pid=12505): Exception occurred processing WSGI script '/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py'.
[Tue Nov 24 15:16:50.317982 2015] [:error] [pid 12505] [client 127.0.0.1:37918] Traceback (most recent call last):
[Tue Nov 24 15:16:50.318011 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Tue Nov 24 15:16:50.318117 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self.load_middleware()
[Tue Nov 24 15:16:50.318131 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[Tue Nov 24 15:16:50.318236 2015] [:error] [pid 12505] [client 127.0.0.1:37918] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue Nov 24 15:16:50.318249 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Tue Nov 24 15:16:50.318325 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self._setup(name)
[Tue Nov 24 15:16:50.318337 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Tue Nov 24 15:16:50.318355 2015] [:error] [pid 12505] [client 127.0.0.1:37918] self._wrapped = Settings(settings_module)
[Tue Nov 24 15:16:50.318364 2015] [:error] [pid 12505] [client 127.0.0.1:37918] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Tue Nov 24 15:16:50.318380 2015] [:error] [pid 12505] [client 127.0.0.1:37918] % (self.SETTINGS_MODULE, e)
[Tue Nov 24 15:16:50.318399 2015] [:error] [pid 12505] [client 127.0.0.1:37918] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings
期待专家的帮助。
=======================wsgi.py============
"""
WSGI config for test2 project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os,sys
sys.path.append('/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
===================setting.py===================== =============
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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 = '-vvzx5(=sztv%*^ljnpkdc&!q+ltws0-%a76^v1e_z9g97-k2%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marcador',
'crispy_forms',
'bootstrap_pagination',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Berlin'
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/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
LOGIN_URL = 'mysite_login'
LOGOUT_URL = 'mysite_logout'
LOGIN_REDIRECT_URL = 'marcador_bookmark_list'
CRISPY_TEMPLATE_PACK = 'bootstrap3'
在您的 wsgi.py 中尝试更改
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
至
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
因为两个文件都在同一个 directory/module.
正如我在评论中所说,您没有做任何事情来将您的项目添加到 PYTHONPATH,以便 WSGI 应用程序可以实际找到它。
从你的目录结构看来,你正在使用一个virtualenv;您需要在 .wsgi 文件或 Apache 配置本身中激活它。 Django 文档 recommend the latter.
我终于可以尽早成功部署启用了 project.I 的 Apache 这个模块。
refer this link
----------------这是我的虚拟主机文件------------------------ --------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin@key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
----------------wsgi.py-------------------- --------------
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
我认为这会对其他 Django 有所帮助 developers.Thank 你们所有帮助过我的开发人员。