Windows10環(huán)境下用Apache部署Django

1. 部署前準(zhǔn)備

  • 工具的安裝
工具的名稱 用途
vim 文件的交互編輯
sed 命令行操作文件內(nèi)容
curl 訪問(wèn)url獲取html內(nèi)容
cmd dos窗口中的一些命令,eg:mv, xcopy, echo, type
  • 安裝信息
軟件名稱 軟件版本
系統(tǒng)環(huán)境 Windows 10
Apache 2.4.48
mod_wsgi 4.9.0
Django 3.2.6
python 3.7.9
  • python安裝
    圖1.1 python下載圖

    下載python安裝包,安裝python37環(huán)境.
  • Apache安裝
    圖1.2 apache下載圖

    下載完成后,解壓下載到的壓縮包即可.
  • mod_wsgi安裝
    圖1.3 mod_wsgi下載圖

    下載完成后,進(jìn)去到下載目錄執(zhí)行命:
    C:\Users\admin>cd Downloads
    C:\Users\admin\Downloads> pip install mod_wsgi-4.9.0-cp37-cp37m-win_amd64.whl
    
  • Django安裝
    C:\Users\admin> pip install django
    

2. 構(gòu)建目錄及相關(guān)文件

  • 創(chuàng)建工作目錄
    C:\Users\admin>D:
    D:\>mkdir testweb
    
  • 創(chuàng)建Django項(xiàng)目
    D:\>cd testweb
    D:\testweb>django-admin startproject djangoweb 
    
  • 復(fù)制Apache解壓文件
    D:\testweb>mkdir Apache24
    D:\testweb>xcopy C:\Users\admin\Downloads\httpd-2.4.48-win64-VS16\Apache24 Apache24 /s/q
    
  • 目錄結(jié)構(gòu)
       D:\TESTWEB
       ├─Apache24
       │    │
       │    └─******
       └─djangoweb
            ├─manage.py
            │
            └─djangoweb
                  asgi.py
                  settings.py
                  urls.py
                  wsgi.py
                  __init__.py
    

3. 啟動(dòng)Apache

  • 編輯httpd.conf文件
    1. 添加第38行內(nèi)容,即apache的安裝路徑


    圖3.1 apache.SRVROOT配置圖

    2. 設(shè)置監(jiān)聽(tīng)端口


    圖3.2 apache.Listen配置圖

    3. 添加229行內(nèi)容
    圖3.3 apache.ServerName配置圖
  • 啟動(dòng)apache24服務(wù)

    1. 進(jìn)去httpd.exe所在的目錄
      C:\testweb>cd Apache24/bin
      
    2. 安裝apache24服務(wù)
      C:\testweb\Apache24\bin>httpd.exe -k install -n "apache24"   # 服務(wù)名為apache24
      Installing the 'apache24' service
      The 'apache24' service is successfully installed.
      Testing httpd.conf....
      Errors reported here must be corrected before the service can be started.     # 提示:如果這行下邊出現(xiàn)錯(cuò)誤則解決錯(cuò)誤后再啟動(dòng)!,不是報(bào)錯(cuò)
      C:\testweb\Apache24\bin>
      
    3. 啟動(dòng)apache24
      C:\testweb\Apache24\bin>net start apache24
      apache24 服務(wù)正在啟動(dòng) .
      apache24 服務(wù)已經(jīng)啟動(dòng)成功。
      
      D:\testweb\Apache24\bin> 
      
    4. 測(cè)試apache頁(yè)面
      D:\testweb\Apache24\bin>curl localhost:88
      <html><body><h1>It works!</h1></body></html>
      
      D:\testweb\Apache24\bin> 
      

4. 啟動(dòng)Django

  • 配置Django文件
    1. 進(jìn)入django項(xiàng)目目錄中
      D:\testweb\Apache24\bin>cd D:\testweb\djangoweb\djangoweb
      D:\testweb\djangoweb\djangoweb> 
      
    2. 配置settings.py文件
      將settings.py文件中的DEBUG = True 改為 DEBUG = False(可以不改,看報(bào)錯(cuò)),
      將settings.py文件中的ALLOWED_HOSTS = [] 改為 ALLOWED_HOSTS = ["*"](允許所有機(jī)子訪問(wèn)).
      D:\testweb\djangoweb\djangoweb>sed -e 's/DEBUG = True/DEBUG = False/; /ALLOWED_HOSTS = \[\]/d;$aALLOWED_HOSTS = ["*"]' settings.py > setting.tmp
      D:\testweb\djangoweb\djangoweb>mv setting.tmp settings.py     
      
    3. 配置urls.py文件
      D:\testweb\djangoweb\djangoweb>sed -e '17afrom . import views' -e '20a/\r    url("^$", views.index),' -e '16afrom django.conf.urls import url' urls.py > urls.tmp
      D:\testweb\djangoweb\djangoweb>mv urls.tmp urls.py
      
    4. 查看urls.py文件
      D:\testweb\djangoweb\djangoweb>sed '16,100p' urls.py -n
      from django.contrib import admin
      from django.conf.urls import url
      from django.urls import path
      from . import views
      
      urlpatterns = [
            url('^$', views.index),
            path('admin/', admin.site.urls),
      ]
      
      D:\testweb\djangoweb\djangoweb>
      
    5. 新建views.py文件
      D:\testweb\djangoweb\djangoweb>echo from django.http import HttpResponse > views.py
      D:\testweb\djangoweb\djangoweb>sed -e '$G;G' -e '$adef index(request):\n    return HttpResponse("<p>My Django</p>")' views.py > views.tmp
      D:\testweb\djangoweb\djangoweb>mv views.tmp views.py
      
    6. 查看views.py文件
      D:\testweb\djangoweb\djangoweb>type views.py
      from django.http import HttpResponse
      
      
      def index(request):
            return HttpResponse("<p>My Django</p>")
      
      D:\testweb\djangoweb\djangoweb>
      
  • 本地測(cè)試Django頁(yè)面
    1. 進(jìn)入manage.py所在的目錄,運(yùn)行Django
      D:\testweb\djangoweb\djangoweb>cd ..
      D:\testweb\djangoweb>python manage.py runserver
      Watching for file changes with StatReloader
      Performing system checks...
      
      System check identified no issues (0 silenced).
      
      You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
      Run 'python manage.py migrate' to apply them.
      August 09, 2021 - 22:03:17
      Django version 3.2.6, using settings 'djangoweb.settings'
      Starting development server at http://127.0.0.1:8000/
      Quit the server with CTRL-BREAK.
      
      
    2. 訪問(wèn)Django
      D:\testweb\djangoweb>curl localhost:8000
      <p>My Django</p>
      D:\testweb\djangoweb> 
      
  • Apache托管Django
    1. 查看mod_wsgi-express
      D:\testweb\djangoweb>mod_wsgi-express module-config
      LoadFile "d:/web/services/python/python37.dll"
      LoadModule wsgi_module "d:/web/services/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
      WSGIPythonHome "d:/web/services/python"
      
      D:\testweb\djangoweb>
      
      復(fù)制輸出的三行
    2. 配置httpd.conf
      • 添加下面配置
        # 粘貼復(fù)制的三行
        LoadFile "d:/web/services/python/python37.dll" 
        LoadModule wsgi_module "d:/web/services/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
        WSGIPythonHome "d:/web/services/python" 
        
        # django項(xiàng)目中wsgi.py文件路徑
        WSGIScriptAlias / D:/testweb/djangoweb/djangoweb/wsgi.py
        # django項(xiàng)目徑路
        WSGIPythonPath  D:/testweb/djangoweb
        # 設(shè)置wsgi.py文件的權(quán)限
        <Directory D:/testweb/djangoweb/djangoweb>
        <Files wsgi.py>
              Require all granted
        </Files>
        </Directory>
        
    3. 重啟apache服務(wù),并訪問(wèn)
      C:\WINDOWS\system32>net stop apache24
      apache24 服務(wù)正在停止.
      apache24 服務(wù)已成功停止。
      
      
      C:\WINDOWS\system32>net start apache24
      apache24 服務(wù)正在啟動(dòng) .
      apache24 服務(wù)已經(jīng)啟動(dòng)成功。
      
      
      C:\WINDOWS\system32>curl localhost:88
      <p>My Django</p>
      C:\WINDOWS\system32>
      
    4. 至此初步配置完成

項(xiàng)目靜態(tài)文件地址, Django項(xiàng)目中靜態(tài)文件的路徑

Alias /static C:/Users/GLX/Desktop/Mydj/VariousData/static
<Directory C:/Users/GLX/Desktop/Mydj/VariousData/static>
AllowOverride None
Options None
Require all granted
</Directory>

項(xiàng)目media地址, 上傳圖片等文件夾的路徑

Alias /media C:/Users/GLX/Desktop/Mydj/VariousData/media
<Directory C:/Users/GLX/Desktop/Mydj/VariousData/media>
AllowOverride None
Options None
Require all granted

</Directory>
————————————————
版權(quán)聲明:本文為CSDN博主「彩虹hai」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u013172664/article/details/80866753

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容