1. 部署前准备
- 工具的安装
工具的名称 | 用途 |
---|---|
vim | 文件的交互编辑 |
sed | 命令行操作文件内容 |
curl | 访问url获取html内容 |
cmd | dos窗口中的一些命令,eg:mv, xcopy, echo, type |
- 安装信息
软件名称 | 软件版本 |
---|---|
系统环境 | 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环境. -
Apache安装
图1.2 apache下载图
下载完成后,解压下载到的压缩包即可. -
mod_wsgi安装
图1.3 mod_wsgi下载图
下载完成后,进去到下载目录执行命: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. 构建目录及相关文件
- 创建工作目录
C:\Users\admin>D: D:\>mkdir testweb
- 创建Django项目
D:\>cd testweb D:\testweb>django-admin startproject djangoweb
- 复制Apache解压文件
D:\testweb>mkdir Apache24 D:\testweb>xcopy C:\Users\admin\Downloads\httpd-2.4.48-win64-VS16\Apache24 Apache24 /s/q
- 目录结构
D:\TESTWEB ├─Apache24 │ │ │ └─****** └─djangoweb ├─manage.py │ └─djangoweb asgi.py settings.py urls.py wsgi.py __init__.py
3. 启动Apache
-
编辑httpd.conf文件
1. 添加第38行内容,即apache的安装路径
图3.1 apache.SRVROOT配置图
2. 设置监听端口
图3.2 apache.Listen配置图
3. 添加229行内容
图3.3 apache.ServerName配置图 -
启动apache24服务
- 进去httpd.exe所在的目录
C:\testweb>cd Apache24/bin
- 安装apache24服务
C:\testweb\Apache24\bin>httpd.exe -k install -n "apache24" # 服务名为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. # 提示:如果这行下边出现错误则解决错误后再启动!,不是报错 C:\testweb\Apache24\bin>
- 启动apache24
C:\testweb\Apache24\bin>net start apache24 apache24 服务正在启动 . apache24 服务已经启动成功。 D:\testweb\Apache24\bin>
- 测试apache页面
D:\testweb\Apache24\bin>curl localhost:88 <html><body><h1>It works!</h1></body></html> D:\testweb\Apache24\bin>
- 进去httpd.exe所在的目录
4. 启动Django
- 配置Django文件
- 进入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 = ["*"](允许所有机子访问).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>
- 进入django项目目录中
- 本地测试Django页面
- 进入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>
- 进入manage.py所在的目录,运行Django
- Apache托管Django
- 查看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>
- 配置httpd.conf
- 添加下面配置
# 粘贴复制的三行 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 # 设置wsgi.py文件的权限 <Directory D:/testweb/djangoweb/djangoweb> <Files wsgi.py> Require all granted </Files> </Directory>
- 添加下面配置
- 重启apache服务,并访问
C:\WINDOWS\system32>net stop apache24 apache24 服务正在停止. apache24 服务已成功停止。 C:\WINDOWS\system32>net start apache24 apache24 服务正在启动 . apache24 服务已经启动成功。 C:\WINDOWS\system32>curl localhost:88 <p>My Django</p> C:\WINDOWS\system32>
- 至此初步配置完成
- 查看mod_wsgi-express
项目静态文件地址, Django项目中静态文件的路径
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>
————————————————
版权声明:本文为CSDN博主「彩虹hai」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013172664/article/details/80866753