在 apache 24 中部署 python 烧瓶应用程序
Posted
技术标签:
【中文标题】在 apache 24 中部署 python 烧瓶应用程序【英文标题】:deploying python flask application in apache 24 【发布时间】:2020-10-11 11:54:03 【问题描述】:我开发了一个 python (python 3.6 32bit) 烧瓶应用程序,我需要将它部署在具有 apache24 32bit 的 Windows 服务器中。
我参考了https://medium.com/@madumalt/flask-app-deployment-in-windows-apache-server-mod-wsgi-82e1cfeeb2ed中的步骤
当我尝试在 apache24 中启动 httpd.exe 时出现以下错误
[Sun Jun 21 20:36:15.112840 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00455: Apache/2.4.43 (Win32) mod_wsgi/4.7.1 Python/3.6 configured -- resuming normal operations
[Sun Jun 21 20:36:15.112840 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00456: Apache Lounge VS16 Server built: Apr 21 2020 16:02:41
[Sun Jun 21 20:36:15.112840 2020] [core:notice] [pid 20600:tid 476] AH00094: Command line: 'httpd.exe -d C:/Apache24'
[Sun Jun 21 20:36:15.123841 2020] [mpm_winnt:notice] [pid 20600:tid 476] AH00418: Parent: Created child process 2064
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00002dfc (most recent call first):
[Sun Jun 21 20:36:21.808509 2020] [mpm_winnt:crit] [pid 20600:tid 476] AH00419: master_main: create child process failed. Exiting.
Please find the SET configurations below,
OS=Windows_NT
Path=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Program Files\Common Files\Micro
soft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Micros
oft Shared\Microsoft Online Services;C:\ProgramData\Oracle\Java\javapath;C:\Wind
ows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowe
rShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\
PYTHONHOME=C:\Python36-32\
PYTHONPATH=C:\Python36-32\Scripts\
【问题讨论】:
【参考方案1】:#1 检查您是否安装了旧的 python 并将其正确删除(包括旧的环境变量、路径..)
#2 如果可能的话,我建议你将你的 python 安装升级到最后一个 3.8.x +
#3 你的问题很常见:你的环境变量设置不正确:
转到System Properties
下的Advanced
选项卡并单击Environment Variables
在System Variables
下创建这些变量:
APACHE_HOME
= C:\wamp\bin\apache\apache2.4.23(我正在使用WAMPSERVER
)
MOD_WSGI_APACHE_ROOTDIR
= %APACHE_HOME%
(由于您使用的是mod_wsgi
,请查看pypi
上的官方doc)
PYTHON_HOME
= C:\Python37(取决于你的 python 安装)
转到User variables
并将变量添加/附加到PATH
,如下所示:
PATH
= %APACHE_HOME%\bin;%MOD_WSGI_APACHE_ROOTDIR%;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;
#4 打开新控制台并检查您的 python 安装:
python --version
#5 创建一个简单的烧瓶应用程序以确保一切都按预期工作
py -m flask run
#6 在Apache
服务器上部署应用,看看这个flask
doc 和官方mod_wsgi
doc
mod_wsgi
GLOBALLY,这意味着您必须首先停用当前活动应用程序的虚拟环境。
(venv) C:\myapps\flask\helloflask>deactivate
(我使用的是venv
标准和默认的python虚拟环境py -m venv venv
)
C:\myapps\flask\helloflask>pip install mod_wsgi
C:\myapps\flask\helloflask>pip list
#7 在Apache
服务器中配置mod_wsgi
mod_wsgi
是否正确安装和配置
C:\myapps\flask\helloflask>mod_wsgi-express --help
Usage: mod_wsgi-express command [params]
Commands:
module-config
module-location
mod_wsgi-express: error: Invalid command was specified.
运行此命令:
mod_wsgi-express module-config
该命令的输出应如下所示(取决于您的系统和 python 安装):
WSGIPythonHome "c:/python37"
LoadFile "c:/python37/python37.dll"
loadmodule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd"
复制上述命令的输出并将其粘贴到C:\wamp\bin\apache\apache2.4.23\conf
。为了使事情保持一致,请找到列出模块的部分并将其添加到列表的最后。
#8 在项目根目录下创建wsgi.py
并粘贴代码(不言自明)
import os
import sys
# activate virtualenv
PROJECT = "helloflask"
# i'm using py -m venv venv
# @see: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
# @see: https://***.com/questions/25020451/no-activate-this-py-file-in-venv-pyvenv
activate_this = os.path.join('C:/myapps/flask', PROJECT, 'venv/Scripts/activate_this.py')
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
BASE_DIR = os.path.join(os.path.dirname(__file__))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)
from helloflask import create_app
application = create_app()
#9 为 Flask 应用配置虚拟主机
<VirtualHost *:80>
ServerName helloflask.local
DocumentRoot "C:/myapps/flask/helloflask"
WSGIScriptAlias / "C:/myapps/flask/helloflask/wsgi.py"
<Directory "C:/myapps/flask/helloflask">
Require all granted
</Directory>
# app = Flask(
# __name__,
# static_url_path='/public/static',
# static_folder='static'
# )
# Alias /public/static "C:/myapps/flask/helloflask/public/static"
# <Directory "C:/myapps/flask/helloflask/public/static">
# Require all granted
# </Directory>
ErrorLog "C:/wamp/logs/helloflask.error.log"
CustomLog "C:/wamp/logs/helloflask.access.log" common
</VirtualHost>
#10 检查你的Apache
配置
httpd -t
如果没问题,重启你的Apache
服务器
【讨论】:
以上是关于在 apache 24 中部署 python 烧瓶应用程序的主要内容,如果未能解决你的问题,请参考以下文章
在 Windows 上通过 Apache Lounge 部署烧瓶应用程序时出现内部服务器错误
如何在 apache windows 10 中使用烧瓶修复错误 500
Python 烧瓶、apache 和 mod_wsgi 应用程序不工作
在 Elastic Beanstalk 中扩展实例会破坏 python 烧瓶应用程序的动态图像链接