在Apache上部署Django?

时间:2015-11-24 09:48:25

标签: django apache django-admin virtualhost

我使用Django 1.8创建项目。现在,我想将它部署到服务器。当我在Ubuntu中运行这个命令时,每个工作的东西都找不到。

python manage.py runserver

接下来,我在Apache中创建了vertuelhost和enbal key.com网站。

vertualhost文件代码。 (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>

下图显示了我的项目文件夹结构。 enter image description here enter image description here

现在,我运行,key.com网址,它提供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'

3 个答案:

答案 0 :(得分:0)

正如我在评论中所说,你没有做任何事情来将你的项目添加到PYTHONPATH,以便WSGI应用程序可以实际找到它。

从你的目录结构看,你正在使用virtualenv;您需要在.wsgi文件或Apache配置本身中激活它。 Django文档recommend the latter

答案 1 :(得分:0)

最后我可以成功部署我的项目。我早期启用了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开发者有所帮助。感谢所有帮助我的开发人员。

答案 2 :(得分:-2)

在你的wsgi.py中尝试更改

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

由于两个文件都在同一个目录/模块中。