apache mod_wsgi django在windows下的部署
以下文字转自:
http://leven.co/blog/read/apache-install-wsgi-public-python
最近因为python升级到了2.7导致mod_python不能用,所以自己电脑上的apache一直启动不起来,趁着周末有时间,抓紧搞一下。
在网上搜了一下,发现mod_python已经停止维护了,而替代他的是mod_wsgi,下载路径如下:
http://code.google.com/p/modwsgi/downloads/list
挑选For 2.7的版本下载,改名成mod_wsgi.so,然后扔到apache的modules目录就可以了。
在apache的conf文件中加入:
1 |
LoadModule wsgi_module modules/mod_wsgi.so |
OK,但是我们真正的安装才刚刚开始。
刚才说过由于python升级到2.7,之前所有的库都删掉了,所以不得不重新安装,需要准备好如下库。
- 1.setuptools(http://pypi.python.org/pypi/setuptools#files)
- 2.MySQL for Python(http://sourceforge.net/projects/mysql-python/。当然,前提是你机器上已经安装了mysql)
- 3.django(http://www.djangoproject.com/download/)
setuptools和django都好说,关键出在了MySQL for Python上,官网上的版本是不支持python2.7的。即使你拿他的source回来在mingw下编译(python setup.py build –compiler=mingw32),也会报如下错误而无法继续:
gcc: /Zl: No such file or directory
不过万幸的事,居然有好心人帮我们把这些库都已经编译好了,页面如下:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
不仅有MySqlPython For python2.7,还有很多其他的类库,是个很不错的地方~~
一切安装完毕之后,就是django的配置了。
1)我们通过python-admin.py创建一个project:
在D:dgwww目录执行:
1 |
python C:Python27Libsite-packagesdjangobindjango-admin.py startproject mysite |
然后在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
在setting.py中修改:
ADMIN_MEDIA_PREFIX = '/admin_media/' MEDIA_URL = '/site_media/'
并去掉django.contrib.admin的注释
在urls.py中:
去掉如下注释:
from django.contrib import admin admin.autodiscover() (r'^admin/', include(admin.site.urls)),
并执行:
python manage.py syncdb
3)修改httpd.conf
添加NameVirtualHost:
1 |
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:dgwwwmysite"> Order Deny,Allow Deny from all </Directory> Alias /admin_media "C:Python27Libsite-packagesdjangocontribadminmedia" <Directory "C:Python27Libsite-packagesdjangocontribadminmedia"> 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>
这里有个地方让我迷惑了好久,即Alias的时候,前面的参数最后是不能带/的,如/admin_media,不能写成/admin_media/,否则会失效。
4)修改host文件:
127.0.0.1 dante.qq.com
访问dante.qq.com/admin,截图如下:
最后,附httpd.conf,setting.py,setting.wsgi下载。
——————————————————————————
linux下的部署
linux的mod_wsgi是需要编译的,命令如下:
1 2 |
./configure --with-apxs=/home/dantezhu/apache/bin/apxs --with-python=/usr/bin/python make && make install |
一个典型httpd.conf的配置如下:
NameVirtualHost *:9010 Listen 10.6.207.119:9010 <VirtualHost *:9010> ServerName fuload.qq.com WSGIScriptAlias / /home/dantezhu/htdocs/fuload/conf/setting.wsgi <Directory /> Options FollowSymLinks AllowOverride Order allow,deny Allow from all </Directory> <Directory "/home/dantezhu/htdocs/fuload/mysite"> Order Deny,Allow Deny from all </Directory> Alias /admin_media "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media" <Directory "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> #AliasMatch /js/(.*.(js)) /home/dantezhu/htdocs/fuload/js/$1 Alias /js /home/dantezhu/htdocs/fuload/js/ <Directory "/home/dantezhu/htdocs/fuload/js/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> #AliasMatch /site_media/(.*.(css|gif|png|jpg|jpeg)) /home/dantezhu/htdocs/fuload/media/$1 Alias /site_media /home/dantezhu/htdocs/fuload/media/ <Directory "/home/dantezhu/htdocs/fuload/media/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> </VirtualHost>
生成project的命令如下:
1 |
python /usr/local/lib/python2.7/site-packages/django/bin/django-admin.py startproject mysite |
附:linux下的httpd.conf下载
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.