Apache+mod_wsgi+django在windows下的部署

python版本2.7

mod_wsgi http://code.google.com/p/modwsgi/downloads/list

apache http://httpd.apache.org/ 

挑选For 2.7的版本下载,改名成mod_wsgi.so,然后扔到apache的modules目录就可以了。
在apache的conf文件中加入:

LoadModule wsgi_module modules/mod_wsgi.so

  

django 和mysql  http://www.lfd.uci.edu/~gohlke/pythonlibs/python 的包的exe安装版本

1)我们通过python-admin.py创建一个project:
在D:\dgwww目录执行:

python D:\Python27\Lib\site-packages\django\bin\django-admin.py startproject mysite
#我的python安在D盘

  然后在D:\dgwww下分别创建js,media,conf三个目录,在conf下创建文件setting.wsgi,内容如下:

import os 
import sys 
sys.stdout = sys.stderr 
from os.path import abspath, dirname, join 
from django.core.handlers.wsgi import WSGIHandler 
sys.path.insert(0, abspath(join(dirname(__file__), "../"))) 
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" #your settings module 
application = WSGIHandler()

  

至此,整体目录结构如下:

/dgwww/ 
|~conf/ 
| `-setting.wsgi 
|~js/ 
|~media/ 
`~mysite/ 
  |-__init__.py 
  |-__init__.pyc 
  |-manage.py 
  |-settings.py 
  |-settings.pyc 
  |-urls.py 
  `-urls.pyc

2)修改setting.py、urls.py

ADMIN_MEDIA_PREFIX = '/admin_media/' 
MEDIA_URL = '/site_media/'

  

在setting.py中修改:

并去掉django.contrib.admin的注释


 可选的操作 如果出现问题再改也不迟
 注释掉
 'django.contrib.sites', #和打不开admin界面有关
 'django.contrib.staticfiles',#和admin界面的CSS界面有关

在urls.py中:
去掉如下注释: 

from django.contrib import admin 
admin.autodiscover() 
(r'^admin/', include(admin.site.urls)),

  

3)修改httpd.conf
添加NameVirtualHost:

NameVirtualHost *

  添加虚拟域名,并配置如下:

<VirtualHost *:80> 
    ServerName dante.qq.com 
    WSGIScriptAlias / D:/dgwww/conf/setting.wsgi 
    <Directory /> 
        Options FollowSymLinks 
        AllowOverride 
        Order allow,deny 
        Allow from all 
    </Directory> 
    <Directory "D:\dgwww\mysite"> 
        Order Deny,Allow 
        Deny from all 
    </Directory> 
    Alias /admin_media "D:\Python27\Lib\site-packages\django\contrib\admin\media" 
    <Directory "D:\Python27\Lib\site-packages\django\contrib\admin\media"> 
        Order allow,deny 
        Options Indexes 
        Allow from all 
        IndexOptions FancyIndexing 
    </Directory> 
    #AliasMatch /js/(.*\.(js)) D:/dgwww/js/$1 
    Alias /js D:/dgwww/js/ 
    <Directory "D:/dgwww/js/"> 
        Order allow,deny 
        Options Indexes 
        Allow from all 
        IndexOptions FancyIndexing 
    </Directory> 
 
    #AliasMatch /site_media/(.*\.(css|gif|png|jpg|jpeg)) D:/dgwww/media/$1 
    Alias /site_media D:/dgwww/media/ 
    <Directory "D:/dgwww/media/"> 
        Order allow,deny 
        Options Indexes 
        Allow from all 
        IndexOptions FancyIndexing 
    </Directory> 
</VirtualHost>

  设置数据库:settings.py中修改

DATABASES = {
‘default’: {
‘ENGINE’: ‘mysql’, # Add ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ‘sqlite3′ or ‘oracle’.
‘NAME’: ‘apathon_site’, # Or path to database file if using sqlite3.
‘USER’: ‘root’, # Not used with sqlite3.
‘PASSWORD’: ‘sa’, # Not used with sqlite3.
‘HOST’: ”, # Set to empty string for localhost. Not used with sqlite3.
‘PORT’: ’3306′, # Set to empty string for default. Not used with sqlite3.
}
}

  我用的是mysql 具体服务器配置详细请参考http://djangobook.py3k.cn/2.0/chapter05/

在mysite目录下执行

python manage.py syncdb

  

以上是我参考别人博客和各方教程的基础上加上这两天自己实践总结的版本  如果有什么不对的地方希望指出 主要是像给初学者一个方便的指导 也为自己留下点记录 以免以后忘记

posted @ 2011-10-27 22:05  Crazy_yiner  阅读(560)  评论(0编辑  收藏  举报