在Apache 24中部署python flask应用程序 [英] deploying python flask application in apache 24
问题描述
我已经开发了python(python 3.6 32位)flask应用程序,我需要将其部署在具有apache24 32bit的Windows服务器中.
I have developed a python (python 3.6 32bit) flask application and I need this to be deployed in a windows server with apache24 32bit.
当我尝试在apache24中启动httpd.exe时,出现以下错误
When I try to launch the httpd.exe in apache24 am getting the below error
[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检查您是否安装了OLD python,并正确删除了它(包括旧环境vars,路径..)
#1 check if you have an OLD python installation and remove it properly (including old environment vars, path ..)
#2如果可能的话,我建议您将python安装升级到最后一个3.8.x +
#2 i would recommend you, if possible, upgrading your python installation to the last one 3.8.x +
#3您的问题很普遍:您的环境变量设置不正确:
#3 your problem is very common: your environment variables are NOT correctly set:
- 转到"
系统属性
"下的"高级
"标签,然后单击"环境变量
". - 在
系统变量
下创建这些变量:-
APACHE_HOME
= C:\ wamp \ bin \ apache \ apache2.4.23(我正在使用WAMPSERVER
) -
MOD_WSGI_APACHE_ROOTDIR
=%APACHE_HOME%
(由于您使用的是mod_wsgi
,请检查官方
- go to
Advanced
tab underSystem Properties
and clickEnvironment Variables
- Under
System Variables
create those variables:APACHE_HOME
= C:\wamp\bin\apache\apache2.4.23 (i'm usingWAMPSERVER
)MOD_WSGI_APACHE_ROOTDIR
=%APACHE_HOME%
(since you are usingmod_wsgi
, check official doc onpypi
)PYTHON_HOME
= C:\Python37 (it depends on your python installation)
-
PATH
=%APACHE_HOME%\ bin;%MOD_WSGI_APACHE_ROOTDIR%;%PYTHON_HOME%;%PYTHON_HOME%\ Scripts;
#4打开新控制台并检查您的python安装:
#4 open new console and check your python installation:
-
python --version
#5创建一个简单的flask应用程序,以确保一切正常工作
#5 create a simple flask app to make sure everything is working as expected
-
py -m flask运行
#6将应用程序部署在
Apache
服务器上,请查看此flask
文档#6 to deploy the app on
Apache
server, have look at thisflask
doc and the officialmod_wsgi
doc- 您必须安装
mod_wsgi
全球,这意味着您必须先停用当前活动应用程序的虚拟环境. -
(venv)C:\ myapps \ flask \ helloflask>停用
(我正在使用venv
标准和默认的python虚拟环境py -m venvvenv
) -
C:\ myapps \ flask \ helloflask> pip install mod_wsgi
-
C:\ myapps \ flask \ helloflask>点列表
- you have to install
mod_wsgi
GLOBALLY, meaning you have to deactivate first your virtual environment of your current active application. (venv) C:\myapps\flask\helloflask>deactivate
(i'm usingvenv
the standard and default python virtual environmentpy -m venv venv
)C:\myapps\flask\helloflask>pip install mod_wsgi
C:\myapps\flask\helloflask>pip list
#7在
Apache
服务器- 检查
mod_wsgi
是否已正确安装和配置
- check if
mod_wsgi
is correctly installed and configured
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
中.为使内容一致,请找到列出模块的部分,然后将其添加到列表的末尾. - Copy the output of the above command and paste it in
C:\wamp\bin\apache\apache2.4.23\conf
. To make things consistent, locate the section where Modules are listed and add it at the very end of the list.
#8在项目根目录下创建
wsgi.py
并粘贴代码(这是不言自明的)#8 create
wsgi.py
under the root of your project and paste the code (it's self-explanatory)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://stackoverflow.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应用配置虚拟主机
#9 Configure a Virtual Host for the Flask App
<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
服务器
httpd -t
if it's OK, restart yourApache
server
这篇关于在Apache 24中部署python flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-