Posted at

CentOS7 + nginx + uWSGI + Flask

More than 3 years have passed since last update.


nginx インストール


shell

yum install epel-release

sed -ri 's/^enabled.*=.*1$/enabled = 0/g' /etc/yum.repos.d/epel.repo
yum install nginx --enablerepo=epel
vi /etc/nginx/nginx.conf



/etc/nginx/nginx.conf

user  nginx;

worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
index index.html index.htm;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name localhost;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/lib/nginx/tmp/uwsgi/uwsgi.sock;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}



shell

systemctl enable nginx.service

systemctl start nginx.service
systemctl status nginx.service


FLASKインストール


shell

yum install gcc python-devel

curl https://bootstrap.pypa.io/get-pip.py | python
pip install flask
mkdir -p /var/www/flask/
vi /var/www/flask/index.wsgi



/var/www/flask/index.wsgi

import sys

from flask import Flask
sys.path.append('/var/www/flask')
application = Flask(__name__)
@application.route('/')
def index():
return 'HelloWorld'



uWSGIインストール


shell

yum install pcre-devel openssl-devel zlib-devel

pip install uwsgi
vi /etc/nginx/uwsgi.ini



/etc/nginx/uwsgi.ini

#http://uwsgi-docs.readthedocs.org/en/latest/Systemd.html

[uwsgi]
socket = /var/lib/nginx/tmp/uwsgi/uwsgi.sock
python-path = /var/www/flask
wsgi-file = /var/www/flask/index.wsgi
uid = nginx
gid = nginx
processes = 4
threads = 2
stats = 127.0.0.1:9191



shell

vi /etc/systemd/system/uwsgi.service




/etc/systemd/system/uwsgi.service

[Unit]

Description=uWSGI
After=syslog.target

[Service]
ExecStart=/bin/uwsgi --ini /etc/nginx/uwsgi.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target




shell

systemctl enable uwsgi.service

systemctl start uwsgi.service
systemctl status uwsgi.service


firewalld


shell

firewall-cmd --permanent --add-service=http

firewall-cmd --reload