讨厌的Windows 下apache+Django+python+mod_wsgi配置

昨天折腾了一天在Windows下配置apache+mod_wsgi 没搞定,今儿终于弄通了!话说这玩意在linux上挺简单的吧 windows。。。说多了都是泪


环境准备:

系统:Win7 64位 环境变量略,注意要配置 PYTHONHOME

软件:安装过程略

Apache 2.4.9 64位

python 2.7.6 64位

Django 1.6.4

mod_wsgi mod_wsgi-3.5.ap24.win-amd64-py2.7.zip(注意对应版本)

注:Apache 必须选择2.4版本,2.2版本我折磨一天没通,欢迎小白鼠继续测试...


配置apahce

把 mod_wsgi-3.5.ap24.win-amd64-py2.7.zip解压丢到apahce工作目录下的modules目录

如: C:\Apache24\modules                                                                                                                


修改 apahce 配置文件 httpd.conf

# Example:

# LoadModule foo_module modules/mod_foo.so

#

后添加

LoadModule wsgi_module modules/mod_wsgi.so


启动测试,查看error.log没有问题继续

c:\Apache24\bin\httpd.exe -w -n "Apache2.4" -k restart


配置 Django项目

现在 设你的Django项目主目录为 F:/mysite

 apahce 配置文件 httpd.conf最后面加上

Include "f:/mysite/apache/apache_django_wsgi.conf"


在 项目 主目录 里增加apache文件夹

1. 增加文件apache_django_wsgi.conf

内容如下,按实际情况修改:

# 设置django admin静态资源的访问路径  
Alias /static/ "F:/mysite/static/"  
<Directory "F:/mysite/static">  
Options All
AllowOverride All
Require all granted
</Directory>  
# 设置root,不要使用"^/"  
WSGIScriptAlias / "F:/mysite/apache/django.wsgi"  
WSGIPythonPath F:/mysite/apache
<Directory "F:/mysite/apache">  
Options All
AllowOverride All
Require all granted
</Directory>  
<Directory "F:/mysite">  
Options All
AllowOverride All
Require all granted
</Directory>


2. 增加文件django.wsgi

内容如下,按实际情况修改:

import os  
import sys  
  
#Calculate the path based on the location of the WSGI script.  
apache_configuration= os.path.dirname(__file__)  
project = os.path.dirname(apache_configuration)  
workspace = os.path.dirname(project)  

sys.path.append(project) #这个路径是项目主目录,如F:/mysite,一定要加上

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'  
import django.core.handlers.wsgi  
application = django.core.handlers.wsgi.WSGIHandler()


完成测试

再次启动测试

c:\Apache24\bin\httpd.exe -w -n "Apache2.4" -k restart


浏览器输入:localhost

It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [appname].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!



---------------------------END------------------------------

你可能感兴趣的:(apache,windows,django,mod_wsgi)