Windows 7中將Django部署到Apache Web Server

來源:互聯網
上載者:User

 本質上來說, Django 只不過是用 Python 編寫的一組類庫。用 Django 開發網站就是使用這些類庫編寫 Python 代碼。因此,學習 Django 的關鍵就是學習如何進行 Python 編程並理解 Django 類庫的運作方式。

如果你有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內部模組衝突了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.