zoukankan      html  css  js  c++  java
  • Django部署到Apache Web Server

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

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

     

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

     

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

     

    django.wsgi:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <span style="font-size: 14px;">#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()
    </span>

     

    apache_django_wsgi.conf:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <span style="font-size: 14px;">#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>
    </span>

     

    目录结构如下:

     

        

     

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

     

    中间加上一句:

     

    LoadModule wsgi_module modules/mod_wsgi.so

     

    文件结尾新增下列配置:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <span style="font-size: 14px;">Alias /static D:/OPSAPP/appops/static     #这是为了可以通过url来访问static文件
    <Location "/static/">
           SetHandler None
    </Location></span><br><span style="font-size: 14px;">
     
    <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>
    </span>

     

     

     

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

     

    常见错误                                                     

     

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

     

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

     

    1
    2
    3
    4
    5
    6
    <span style="font-size: 14px;"><Directory  D:/OPSAPP/appops/static/ >
    Order deny,allow
    Allow from all
    IndexOptions FancyIndexing
    </Directory>
    </span>

     

    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

     

     

     

     

     

     

     

     

     

     
    标签: PythonDjango

  • 相关阅读:
    rust 宏
    umi 调试
    rust 编译器工作流
    rust 神奇的特质
    rust 生命周期2
    rust 函数-生命周期
    rust 九九乘法表
    rust 代码生成选项
    rust 编码模式
    恢复到版本并销毁之后的git提交记录
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3589417.html
Copyright © 2011-2022 走看看