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

简介: 在Windows上部署Django(用mod_wsgi)会出现各种奇怪的问题,现简单记录下配置过程及遇到的错误及解决方法。 环境搭建                                                    windows 7 python 2.

在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 (官网下载)

配置思路                                                   

1、配置apache的httpd.conf文件

2、配置django相关配置文件

配置过程                                                   

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

部署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了。

常见错误                                                     

访问失败时的错误会记录在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内部模块冲突了

 

 

参考:

http://www.cnblogs.com/zhengyun_ustc/archive/2006/11/20/django_apache_win32.html

http://www.server110.com/apache/201309/1873.html

 

 

作者: zhoujie
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,不然我担心博客园找你算账
如果您觉得本文对你有帮助,请竖起您的大拇指右下角点推荐,也可以关注我
目录
相关文章
|
6月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
681 116
|
6月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
273 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
9月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
704 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
11月前
|
人工智能 安全 网络安全
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
528 4
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
|
10月前
|
Linux 数据库 数据安全/隐私保护
Python web Django快速入门手册全栈版,共2590字,短小精悍
本教程涵盖Django从安装到数据库模型创建的全流程。第一章介绍Windows、Linux及macOS下虚拟环境搭建与Django安装验证;第二章讲解项目创建、迁移与运行;第三章演示应用APP创建及项目汉化;第四章说明超级用户创建与后台登录;第五章深入数据库模型设计,包括类与表的对应关系及模型创建步骤。内容精炼实用,适合快速入门Django全栈开发。
507 1
|
11月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
215 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
759 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
847 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
6月前
|
安全 Unix 物联网
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
657 0
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 10 月更新)
|
7月前
|
安全 Unix 物联网
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 9 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 9 月更新)
1555 2

热门文章

最新文章

推荐镜像

更多