环境支持:
Python Django MySQL MySQLdb Apache libapache2-mod-wsgi
创建Django项目
django-admin.py startproject mysite
创建并编写wsgi程序 vim mysite/apache/django.wsgi
import os
import sys
sys.path.append('/var/www')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
设置settings.py文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'root',
'PASSWORD': '111111',
'HOST': '',
'PORT': '',
}
}
ADMIN_MEDIA_PREFIX = '/media/'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
)
设置urls.py文件
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
同步数据库
python manage.py syncdb
配置Apache虚拟主机
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName django.localhost
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
WSGIScriptAlias / /var/www/mysite/apache/django.wsgi
alias /media/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
重启Apache服务器即可访问Django自带后台程序