Extreme Thinking
python+flask+uwsgi+apache in CentOS7

2019-09-12


yum -y install python36 python36-pip
yum -y install python36 python36-devel python36-pip mod_proxy_uwsgi gcc
python3.6 -m pip install --upgrade pip
python3.6 -m pip install virtualenv flask uwsgi

Add User & env …

adduser flask
passwd flask
su - flask
mkdir app
cd app
virtualenv -p python3 env
source env/bin/activate
python3.6 -m pip install flask uwsgi (install you need)

vi ~/app/app.py

#!/usr/bin/env python
from flask import Flask
app = Flask(__name__)

@app.route("/")
def root():
    import sys
    return sys.version

if __name__ == "__main__":
    app.run(host='0.0.0.0')

vi ~/app/wsgi.py

from app import app as application

if __name__ == "__main__":
    application.run()

vi ~/app/wsgi.ini

[uwsgi]
module = wsgi

master = true
processes = 5

socket = 127.0.0.1:8099
chmod-socket = 660
vacuum = true

die-on-term = true

in root

vi /etc/systemd/system/flaskapp.service

[Unit]
Description=uWSGI server for flask
After=network.target

[Service]
User=flask
Group=apache
WorkingDirectory=/home/flask/app
Environment="PATH=/home/flask/app/env/bin"
ExecStart=/home/flask/app/env/bin/uwsgi --ini wsgi.ini

[Install]
WantedBy=multi-user.target

start service

systemctl enable flaskapp.service
systemctl start flaskapp.service
systemctl status flaskapp.service

vi /etc/httpd/conf/httpd.conf

Listen 12349
LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so
<VirtualHost *:12349>
    ProxyPass / uwsgi://127.0.0.1:8099/
</VirtualHost>

check apache & start

apachectl configtest
systemctl enable httpd
systemctl start httpd

When you reconfig app.py need

systemctl restart flaskapp.service

debug app.py service … or add log in httpd.conf

systemctl status flaskapp.service -l