1. 程式人生 > >windows + Apache + mod_wsgi 部署釋出Django專案

windows + Apache + mod_wsgi 部署釋出Django專案

一、環境         windows 2008 r2 + python 2.7.10(32bit) + django 1.5.1 + Apache 2.4.27(32bit) + mod_wsgi 二、安裝配置Python + Django         參考Django框架學習筆記本下的:Windows 環境下搭建python + Django 開發環境 三、安裝配置Apache + mod_wsgi     注意:Apache是32位,所以mod_wsgi也應該選擇32位
  1. 下載Apache
  1. 下載mod_wsgi
  1. 安裝Apache
        解壓下載檔案至指定碟符下(F:/apache檔案下)
  1. 安裝mod_wsgi
        解壓之後的檔案為“mod_wsgi.so”檔案。我們拷貝它到apache安裝目錄下面的modules資料夾
  1. 配置mod_wsgi
        光拷貝mod_wsgi.so檔案是不行的,我們還需要修改apache的配置檔案,讓apache知道mod_wsgi的存在。apache的配置檔案在apache安裝目錄下的conf資料夾中的httpd.conf檔案。         在檔案最後新增: LoadModule wsgi_module modules/mod_wsgi.so 四、配置Django project
  1. 修改專案檔案下建立apache資料夾
  2. 在資料夾下建立兩個檔案:
        apache_django_wsgi.conf
django.wsgi.py
  1. 修改 django.wsgi.py 檔案
import os   import sys   #Calculate the path based on the location of the WSGI script.   apache_configuration= os.path.dirname(__file__)   project = os.path.dirname(apache_configuration)   workspace = os.path.dirname(project)   sys.path.append("F:/vehiclemanage")    #
這個路徑是專案主目錄,一定要加上 os.environ['DJANGO_SETTINGS_MODULE'] = 'apps.settings' import django.core.handlers.wsgi   application = django.core.handlers.wsgi.WSGIHandler()
  1. 修改 django.wsgi.py 檔案
# 設定python環境"^/"   WSGIPythonHome E:\Python\Python27 # 設定django admin靜態資源的訪問路徑   Alias /static/ "F:/vehiclemanage/static/"   <Directory "F:/vehiclemanage/static">   Options All AllowOverride All Require all granted </Directory>   # 設定root,不要使用"^/"   WSGIScriptAlias / "F:/vehiclemanage/apache/django.wsgi.py"   WSGIPythonPath F:/vehiclemanage/apache <Directory "F:/vehiclemanage/apache">   Options All AllowOverride All Require all granted </Directory>   <Directory "F:/vehiclemanage">   Options All AllowOverride All Require all granted </Directory>
  1. 啟動Apache
        開啟cmd,定位到apache安裝目錄下面的bin目錄,輸入httpd就可以啟動伺服器了,此時你就可以在你的瀏覽器中訪問localhost了
  1. 外網訪問
  • 在apache安裝目錄下的conf資料夾中的httpd.conf檔案中設定“ServerName”以及“Listen”
  • 在Windows伺服器上的“控制面板”中的“windows防火牆”中,給剛剛已經在httpd.conf檔案中設定好的監聽埠配置入站規則
        全部設定完成後,就可以在瀏覽器中訪問url(ServerName+Listen)了