Windows+Apache+Django的生产部署大坑!
今天试图在windows server 2016上利用Apache部署一个Django项目,折腾了一天,记录一下期间遇到的坑;
直接执行httpd.exe服务执行没问题,但是安装为系统服务后就不能正常运行,排查好久找不到原因……
一、环境:
1、windows server 2016;
2、Apache 2.4.47;(httpd-2.4.47-o111k-x64-vs16)
3、Django 3.2.3
4、数据库Maria DB;
5、python版本:3.8.10
-----------------------------------------------------------------------------
二、Django项目迁移略过……
1、debug模式项目可以正常运行,端口号8090正常访问;
三、Apache 安装部署和配置都是根据网上指导进行的;
1、httpd.conf文件中增加了以下内容:(配置文件应该没问题,httpd.exe可以正常运行)
# 以下为新增内容
#添加mod_wsgi.so模块,这三行是mod_wsgi-express module-config命令行中显示出来的
LoadFile "c:/python/python38.dll"
LoadModule wsgi_module "c:/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/python"
# WSGIApplicationGroup %{GLOBAL}
#指定项目的wsgi.py配置文件路径,这个py文件是在你的Django项目中
WSGIScriptAlias / "D:/web/djweb/djweb/wsgi.py" application-group=%{GLOBAL}
#指定项目目录,即你的Django项目路径
WSGIPythonPath "D:/web/djweb"
<Directory "D:/web/djweb/djweb">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#项目静态文件地址, Django项目中静态文件的路径
Alias /static "D:/web/djweb/djecharts/static"
<Directory "D:/web/djweb/djecharts/static">
AllowOverride None
Options None
Require all granted
</Directory>
#项目media地址, 上传图片等文件夹的路径
Alias /media "D:/web/djweb/media"
<Directory "D:/web/djweb/media">
AllowOverride None
Options None
Require all granted
</Directory>
2、直接打开httpd.exe,项目可以正常运行,8090端口正常访问;
3、使用httpd -k install -n Apache24 安装服务;
4、使用ApacheMonitor.exe 监控 Apache24,服务无法开启;提示:the requested operation has failed
5、直接在windows系统服务中开启Apache24,服务无法开启;
6、查看error.log文件显示:
[Thu May 20 16:08:49.086771 2021] [core:notice] [pid 864:tid 560] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Thu May 20 16:08:49.086771 2021] [mpm_winnt:notice] [pid 864:tid 560] AH00418: Parent: Created child process 688
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Apache24\\bin\\httpd.exe'
sys.base_prefix = 'C:\\python'
sys.base_exec_prefix = 'C:\\python'
sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
sys.prefix = 'C:\\python'
sys.exec_prefix = 'C:\\python'
sys.path = [
'C:\\python\\python38.zip',
'.\\DLLs',
'.\\lib',
'C:\\Apache24\\bin',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x0000164c (most recent call first):
<no Python frame>
[Thu May 20 16:08:49.539905 2021] [mpm_winnt:crit] [pid 864:tid 560] AH00419: master_main: create child process failed. Exiting.
7、检查系统环境变量,python两个地址设置都正常,C:\python\Scripts\;C:\python\
8、直接执行httpd.exe服务执行没问题,但是安装为系统服务后就不能正常运行,排查好久找不到原因……无语了……
最后根据UPDATE DEFAULT的评论,添加添加两个PYTHONHOME、PYTHONPATH这两个环境变量;
然后在运行httpd -k install -n Apache24 安装服务,就可以正常运行了。
服务已经正常启动了。
--------------------------------
原以为Python安装的时候,自动添加的path就应该可以的……被误导了……
下图是原来的环境变量: