deploying python flask application in apache 24

半城伤御伤魂 提交于 2021-02-04 08:28:32

问题


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.

I referred steps in https://medium.com/@madumalt/flask-app-deployment-in-windows-apache-server-mod-wsgi-82e1cfeeb2ed

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:


#1 check if you have an OLD python installation and remove it properly (including old environment vars, path ..)

#2 i would recommend you, if possible, upgrading your python installation to the last one 3.8.x +

#3 your problem is very common: your environment variables are NOT correctly set:

  • go to Advanced tab under System Properties and click Environment Variables
  • Under System Variables create those variables:
    • APACHE_HOME = C:\wamp\bin\apache\apache2.4.23 (i'm using WAMPSERVER)
    • MOD_WSGI_APACHE_ROOTDIR = %APACHE_HOME% (since you are using mod_wsgi, check official doc on pypi)
    • PYTHON_HOME = C:\Python37 (it depends on your python installation)
  • go to User variables and add/append variables to PATH like so:
    • PATH = %APACHE_HOME%\bin;%MOD_WSGI_APACHE_ROOTDIR%;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;

#4 open new console and check your python installation:

  • python --version

#5 create a simple flask app to make sure everything is working as expected

  • py -m flask run

#6 to deploy the app on Apache server, have look at this flask doc and the official mod_wsgi doc

  • 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 using venv the standard and default python virtual environment py -m venv venv)
  • C:\myapps\flask\helloflask>pip install mod_wsgi
  • C:\myapps\flask\helloflask>pip list

#7 configure mod_wsgi in Apache Server

  • 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.
  • run this command:
mod_wsgi-express module-config
  • the output of the command should be like the following (depending on your system and python installation):
    WSGIPythonHome "c:/python37"
    LoadFile "c:/python37/python37.dll"
    loadmodule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd"
  • 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 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 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 check your Apache configuration

  • httpd -t if it's OK, restart your Apache server


来源:https://stackoverflow.com/questions/62500556/deploying-python-flask-application-in-apache-24

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!