当前位置: win7下载网 > win7教程 > win7系统Django部署到Apache Web Server的操作方法

win7系统Django部署到Apache Web Server的操作方法

时间:2021-09-30 10:51:39 来源:www.cnomit.cn 作者:佚名 浏览次数:

 大家对电脑都爱不释手,但偶尔会碰见对win7系统Django部署到Apache Web Server进行设置的问题,相信大家都是第一次面对win7系统Django部署到Apache Web Server的问题,那么怎样快速对win7系统Django部署到Apache Web Server的设置方法非常简单,只需要1、先去网上下载个名叫这个的东西:mod_wsgi-3.4.ap22.win32-py2.7,里面有个文件是mod_wsgi.so,然后把这个copy到apache安装目录的modules文件下(默认安装目录是:C:Program Files (x86)Apache Software FoundationApache2.2modules) 下面两个配置中涉及路径的很容易搞错,如果是绝对路径的话要检查自己是否正确。 2、在Django项目更目录下新建两个文件:的顺序来就搞定了,现在我们就一同详细的学习一下win7系统Django部署到Apache Web Server具体的设置方法:

如果你有Python开发经验,在学习过程中应该不会有任何问题,基本上,Django的代码并 没有使用一些黑色魔法(例如代码中的欺骗行为,某个实现解释或者理解起来十分困难)。 对你来说,学习Django就是学习她的命名规则和API。

配置思路

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 FoundationApache2.2modules)

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

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 FoundationApache2.2confhttpd.conf)

中间加上一句:

LoadModule wsgi_module modules/mod_wsgi.so

文件结尾新增下列配置:

Alias /static D:/OPSAPP/appops/static     #这是为了可以通过url来访问static文件

<Location "/static/">

       SetHandler None

</Location><br>

 

<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 FoundationApache2.2logs),

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内部模块冲突了

相关教程