您的位置:首页 > 运维架构 > Apache

Windows环境下,将Django部署到Apache Web Server

2014-03-07 20:20 405 查看
在Windows上部署Django(用mod_wsgi)会出现各种奇怪的问题,现简单记录下配置过程及遇到的错误及解决方法。

环境搭建

windows 7

python 2.7.3 (注意最好是32位的,因为Apache目前对64位支持非常不友善,尝试了好多次,64位的太高大上,众多不兼容)

Apache2.2.10 (直接下载apache_2.2.10-win32-x86-no_ssl.msi就行)

Django-1.6.1 (官网下载)

[b]配置思路 [/b]

1、配置apache的httpd.conf文件

2、配置django相关配置文件

[b]配置过程 [/b]

其实配置生效针对不同的环境有不同的细节需要处理,网上的方案(包括本篇)都不是一定通用的,只是在某种环境下有效,但总体思路就是配置上述提及的两个配置文件。

部署django项目常用的两个方式是使用mod_python和mod_wsgi两种部署方式,这里我使用的是mod_wsgi。

1、先去网上下载个名叫这个的东西:mod_wsgi-3.4.ap22.win32-py2.7,里面有个文件是mod_wsgi.so,然后把这个copy到apache安装目录的modules文件下(默认安装目录是:C:\Program Files (x86)\Apache Software Foundation\Apache2.2\modules)

下面两个配置中涉及路径的很容易搞错,如果是绝对路径的话要检查自己是否正确。

2、在Django项目根目录下新建两个文件:

django.wsgi:

#coding=utf-8

import os
import sys
import django.core.handlers.wsgi

os.environ['DJANGO_SETTINGS_MODULE'] = 'appops.settings'
app_apth = "D:/OPSAPP/appops"
sys.path.append(app_apth)
application = django.core.handlers.wsgi.WSGIHandler()


apache_django_wsgi.conf:

#Alias / D:/OPSAPP/appops
Alias /favicon.jpg D:/OPSAPP/appops/static/images/favicon.jpg

#WSGIScriptAlias /api "D:/OPSAPP/appops/appapi/handler.py"  #注意,这里在httpd.conf中写过的话这里就不用写了。
WSGIScriptAlias / "D:/OPSAPP/django.wsgi"

WSGIPassAuthorization On

<Directory "D:/OPSAPP/appops/appops">
Order Deny,Allow
Allow from all
</Directory>

Alias /static/ D:/OPSAPP/appops/static/

<Directory  D:/OPSAPP/appops/static/ >
Order deny,allow
Allow from all
IndexOptions FancyIndexing
</Directory>

<Directory  D:/OPSAPP/appops/ >
Order deny,allow
Allow from all
IndexOptions FancyIndexing
</Directory>

<Directory "D:/OPSAPP">
Allow from all
</Directory>


目录结构如下:





3、编辑apache的配置文件httpd.conf(C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf)

中间加上一句:

LoadModule wsgi_module modules/mod_wsgi.so

文件结尾新增下列配置:

Alias /static D:/OPSAPP/appops/static     #这是为了可以通过url来访问static文件
<Location "/static/">
SetHandler None
</Location>

<VirtualHost *:80>                       #配置虚拟目录
ServerName app.ops.test.com
#ServerName 192.168.18.74

DocumentRoot D:/OPSAPP
WSGIScriptAlias / D:/OPSAPP/django.wsgi

<Directory />
Order deny,allow
Allow from all
</Directory>
<Directory /apache>
Allow from all
</Directory>
</VirtualHost>

<Directory "D:/OPSAPP/appops/static/">    #这个一定需要,不然网页样式错误,css没有起作用
Order Deny,Allow
Allow from all
</Directory>


重启下apache服务基本就OK了。

[b]常见错误 [/b]

访问失败时的错误会记录在apache日志里(C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs),

1、静态资源不能访问,如css样式错乱等,需要在httpd.conf文件里增加配置:

<Directory  D:/OPSAPP/appops/static/ >
Order deny,allow
Allow from all
IndexOptions FancyIndexing
</Directory>


2、出现找不到模块的错,如no module named XXX等,主要有两个原因:

1)、路径错了

2)、文件命名跟Django或python内部模块冲突了

参考:

/article/4594715.html

http://www.server110.com/apache/201309/1873.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: