apache2.4+python2.7+mod wsgi部署django1.10项目

apache 安装参考前一章

将 mod_wsgi.so 拷贝到 apache的C:\Apache24\modules下面

django 项目使用 python manager.py startproject  lfctestdjango

目录:(创建了2个app python manager.py startapp  jlpt1 和 west)

static目录是从django 拷贝过来的

apache 是自己创建的

templates是模板html

west和jlpt1是apps


setting.py

"""
Django settings for lfctestdjango project.

Generated by 'django-admin startproject' using Django 1.10.2.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os
from os.path import dirname, abspath, normpath, join
# 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.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'svr7ae1ob47k5psbt@0s0s@l$)uw$ki&z9!abt=r!ny2d9on9o'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']


# Application definition

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

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 = 'lfctestdjango.urls'
TEMPLATES_FOLDER_NAME = 'jlpt1/templates'
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
print BASE_DIR
print normpath(join(BASE_DIR, 'templates/west/'))
print abspath(__file__)
print dirname(abspath(__file__))
print DJANGO_ROOT
print dirname(DJANGO_ROOT)
print normpath(join(DJANGO_ROOT, TEMPLATES_FOLDER_NAME))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [normpath(join(DJANGO_ROOT, TEMPLATES_FOLDER_NAME)),normpath(join(BASE_DIR, 'templates/west/'))],
        '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 = 'lfctestdjango.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'villa',
        'USER':'root',
        'HOST':'localhost',
        'PORT':'3306',
        'PASSWORD':'123456',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/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.10/howto/static-files/


STATIC_ROOT = 'E:/PythonWorkSpace/lfctestdjango/static'
STATIC_URL = '/static/'
STATIC_DIRS = (
    'E:/PythonWorkSpace/lfctestdjango/static',
)

url.py

"""
from django.conf.urls import url,include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
admin.autodiscover()
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^jlpt/', include('jlpt1.urls')),
    url(r'^west/', include('west.urls')),
    #url(r'^/west/', include('west.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


修改httpd.conf

ServerRoot "c:/Apache24"

Listen 8081

ServerName 127.0.0.1:8081

在文件结尾添加

#添加mod_wsgi.so 模块  
LoadModule wsgi_module modules/mod_wsgi.so  
Include E:/PythonWorkSpace/lfctestdjango/apache/apache_django_wsgi.conf

apache_django_wsgi.conf

#引入静态资源
Alias /static/admin E:/Python27/Lib/site-packages/Django-1.10.2-py2.7.egg/django/contrib/admin/static/admin   
<Directory E:/Python27/Lib/site-packages/Django-1.10.2-py2.7.egg/django/contrib/admin/static/admin>   
    AllowOverride None  
    Options None  
    Require all granted  
</Directory>  
      
Alias /static E:/PythonWorkSpace/lfctestdjango/static   
<Directory E:/PythonWorkSpace/lfctestdjango/static>   
    AllowOverride None  
    Options None  
    Require all granted  
</Directory>     
#指定myweb项目的django.py配置文件路径  
WSGIScriptAlias / E:/PythonWorkSpace/lfctestdjango/apache/django.py  
      
#指定项目路径  
WSGIPythonPath E:/PythonWorkSpace/lfctestdjango  
      
<Directory E:/PythonWorkSpace/lfctestdjango/apache>  
<Files django.py>  
     Require all granted  
</Files>  
</Directory>  



django.py

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

启动 apache服务

浏览器访问 localhost:8081

然后选择自己的url映射



至此,django项目部署成功。。。

PS:在配置Apache时,可以查看log文件,这样可以非常快的找出问题的原因,主要是两个log文件:access.log &error.log

注意apache  mod_wsgi 和 python 以及操作系统位数。。

你可能感兴趣的:(apache2.4+python2.7+mod wsgi部署django1.10项目)