Windows+apache+mod_wsgi部署Django项目

配置环境

windows10服务器
python版本3.8
Django版本3.0

Apache下载与配置

apache 官网下载地址:
https://www.apachehaus.com/cgi-bin/download.plx#APACHE24VC15
在这里插入图片描述此处我选择Apache2.4 VC15

在这里插入图片描述因为我是64位系统,点击此处

在这里插入图片描述
下载完是一个压缩包,解压后得到
在这里插入图片描述
把Apache24文件夹放在任意路径,我放在这里

在这里插入图片描述进入Apache24文件夹下的conf目录,找到httpd.conf配置文件
在这里插入图片描述我是用笔记本打开方式编辑,会看到很多代码,不用管,也不要删除任何代码,直接使用Ctrl+F查找,找到如下代码行进行修改

......
Define SRVROOT "C:/Program Files/Apache/Apache24" # 此处把引号内的路径改为你的Apache24文件夹路径
......
Listen 12.12.12.29:8010 # 此处为你要发布的网站的ip地址和端口
......
ServerName 12.12.12.29:8010 # 同上
......

创建Apache服务

以管理员身份进入命令行, 并cd进入Apache24目录,执行下方命令

httpd.exe -k install -n "apache2.4"  #apache2.4是所创建服务器名称,可自己命名。

出现successfully installed说明服务创建成功

创建成功后在命令行输入services.msc进入服务器管理器,找到你创建的apache服务,并启动

在这里插入图片描述
启动成功后打开浏览器输出你在httpd.conf中配置的ip地址和端口访问,出现如下页面成功

在这里插入图片描述

配置Django

安装mod_wsgi,进入http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi,下载相应版本mod_wsgi Python的插件

我的python版本是3.8,这里我找到我的对应版本下载

在这里插入图片描述

得到
在这里插入图片描述将下载好的whl文件移动到服务器下载的的python下的Scripts目录下
在这里插入图片描述

在命令行进入服务器下载的的python下的Scripts目录,并输入

pip install “mod_wsgi-4.7.1-cp38-cp38-win_amd64.whl”

 

在这里插入图片描述
安装成功后执行下方命令行

mod_wsgi-express module-config

现以下三行提示,这三行一定要记下来,后面配置最重要的就是它了,复制这三行内容:

在这里插入图片描述

再次打开apache的配置文件httpd.conf
在文件的最后,粘贴刚刚复制下来的三行内容
并且加入其他配置内容如下

在这里插入图片描述

LoadFile "c:/users/.../appdata/local/programs/python/Python38/python38.dll" 
LoadModule wsgi_module "c:/users/.../appdata/local/programs/python/Python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_adm64.pyd"
WSGIPythonHome "c:/users/.../appdata/local/programs/python/Python38"
#设置工程中的wsgi路径
WSGIScriptAlias / C:\Pybishe\OBE\wsgi.py
#设置工程路径
WSGIPythonPath C:\Pybishe
#设置wsgi路径
<Directory C:\Pybishe\OBE>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>
#设置静态文件路径
Alias /static C:\Pybishe\static
<Directory C:\Pybishe\static>  
    AllowOverride None  
    Options None  
    Require all granted  
</Directory> 

其中的所有配置根据自己的情况进行更改

写好后保存文件,回到服务器管理器,重启apache2.4服务

如果之前配置都没问题,那么打开浏览器输入你配置的ip网址加端口,你就会见到眼熟的django页面啦。

 

遇到的错误

  1. 请求链接时网页上出现了错误
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

需要在django的wsgi.py中添加项目目录:

import os
import sys

root = os.path.join(os.path.dirname(__file__))
sys.path.insert(0, root)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值