1. 部署前準(zhǔn)備
- 工具的安裝
工具的名稱 | 用途 |
---|---|
vim | 文件的交互編輯 |
sed | 命令行操作文件內(nèi)容 |
curl | 訪問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項目
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. 啟動Apache
-
編輯httpd.conf文件
1. 添加第38行內(nèi)容,即apache的安裝路徑
圖3.1 apache.SRVROOT配置圖
2. 設(shè)置監(jiān)聽端口
圖3.2 apache.Listen配置圖
3. 添加229行內(nèi)容
圖3.3 apache.ServerName配置圖 -
啟動apache24服務(wù)
- 進(jìn)去httpd.exe所在的目錄
C:\testweb>cd Apache24/bin
- 安裝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)錯誤則解決錯誤后再啟動!,不是報錯 C:\testweb\Apache24\bin>
- 啟動apache24
C:\testweb\Apache24\bin>net start apache24 apache24 服務(wù)正在啟動 . apache24 服務(wù)已經(jīng)啟動成功。 D:\testweb\Apache24\bin>
- 測試apache頁面
D:\testweb\Apache24\bin>curl localhost:88 <html><body><h1>It works!</h1></body></html> D:\testweb\Apache24\bin>
- 進(jìn)去httpd.exe所在的目錄
4. 啟動Django
- 配置Django文件
- 進(jìn)入django項目目錄中
D:\testweb\Apache24\bin>cd D:\testweb\djangoweb\djangoweb D:\testweb\djangoweb\djangoweb>
- 配置settings.py文件
將settings.py文件中的DEBUG = True 改為 DEBUG = False(可以不改,看報錯),
將settings.py文件中的ALLOWED_HOSTS = [] 改為 ALLOWED_HOSTS = ["*"](允許所有機(jī)子訪問).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
- 配置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
- 查看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>
- 新建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
- 查看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>
- 進(jìn)入django項目目錄中
- 本地測試Django頁面
- 進(jìn)入manage.py所在的目錄,運行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.
- 訪問Django
D:\testweb\djangoweb>curl localhost:8000 <p>My Django</p> D:\testweb\djangoweb>
- 進(jìn)入manage.py所在的目錄,運行Django
- Apache托管Django
- 查看mod_wsgi-express
復(fù)制輸出的三行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>
- 配置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項目中wsgi.py文件路徑 WSGIScriptAlias / D:/testweb/djangoweb/djangoweb/wsgi.py # django項目徑路 WSGIPythonPath D:/testweb/djangoweb # 設(shè)置wsgi.py文件的權(quán)限 <Directory D:/testweb/djangoweb/djangoweb> <Files wsgi.py> Require all granted </Files> </Directory>
- 添加下面配置
- 重啟apache服務(wù),并訪問
C:\WINDOWS\system32>net stop apache24 apache24 服務(wù)正在停止. apache24 服務(wù)已成功停止。 C:\WINDOWS\system32>net start apache24 apache24 服務(wù)正在啟動 . apache24 服務(wù)已經(jīng)啟動成功凡泣。 C:\WINDOWS\system32>curl localhost:88 <p>My Django</p> C:\WINDOWS\system32>
- 至此初步配置完成
- 查看mod_wsgi-express
項目靜態(tài)文件地址, Django項目中靜態(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>
項目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)載請附上原文出處鏈接及本聲明汁汗。
原文鏈接:https://blog.csdn.net/u013172664/article/details/80866753