Exécutez Flask sur CentOS avec python3.4, Gunicorn + Nginx.

Les fichiers qui se déplacent réellement sont les suivants

app.py


from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

install

$ git clone https://github.com/riywo/anyenv ~/.anyenv
$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(anyenv init -)"' >> ~/.bashrc
$ exec $SHELL -l

$ pyenv install 3.4.1
$ cd ~/.anyenv/envs/pyenv/plugins
$ git clone git://github.com/yyuu/pyenv-virtualenv.git
$ pyenv virtualenv flask-3.4.1
$ pyenv rehash
$ sudo yum install -y nginx

Créez un environnement virtualenv pour ne pas polluer l'environnement. Créez un répertoire pour enregistrer l'application, apportez l'application et installez la bibliothèque

$ mkdir -p /path/to/flask
$ cd /path/to/flask
$ pyenv local flask-3.4.1
$ python --version
Python 3.4.1

$ cp -r /path/to/flask/app ./
$ pip install gunicorn
$ pip install -r requirements.txt
or 
$ pip install hoge fuga...

Exécutez gunicorn

En fait, exécutez gunicorn.

$ ~/.anyenv/envs/pyenv/shims/gunicorn app:app &

Vérification

Vérifiez si le processus a démarré normalement

$ ps ax | grep gunicorn

 2234 pts/0    S      0:00 /home/vagrant/.anyenv/envs/pyenv/versions/flask-3.4.1/bin/python3.4 /home/vagrant/.anyenv/envs/pyenv/versions/flask-3.4.1/bin/gunicorn app:app
 2241 pts/0    S      0:00 /home/vagrant/.anyenv/envs/pyenv/versions/flask-3.4.1/bin/python3.4 /home/vagrant/.anyenv/envs/pyenv/versions/flask-3.4.1/bin/gunicorn app:app
 2265 pts/0    S+     0:00 grep gunicorn

Créer un fichier de configuration

Abandonnez le processus gunicorn lancé dans le test.

$ ps ax | grep gunicorn | awk '{print $1}' | xargs kill

Créer un fichier de configuration nginx $ vim example.conf

example.conf


upstream app_server {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
    # For a TCP configuration:
}

server {
    listen 80 default;
    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # path for static files
    root /path/to/flask/public;

    location / {
        # checks for static file, if not found proxy to app
        try_files $uri @proxy_to_app;
		break;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://app_server;
    }
}

copy

$ sudo cp example.conf /etc/nginx/conf.d/

Créez un fichier de configuration gunicorn. $ vim gunicorn.conf.py

Python:gunicorn.conf.py


# Sample Gunicorn configuration file.

#
# Process naming
#
#   proc_name - A base to use with setproctitle to change the way
#       that Gunicorn processes are reported in the system process
#       table. This affects things like 'ps' and 'top'. If you're
#       going to be running more than one instance of Gunicorn you'll
#       probably want to set a name to tell them apart. This requires
#       that you install the setproctitle module.
#
#       A string or None to choose a default of something like 'gunicorn'.
#
proc_name = "gunicorn"

#
# Server socket
#
#   bind - The socket to bind.
#
#       A string of the form: 'HOST', 'HOST:PORT', 'unix:PATH'.
#       An IP is a valid HOST.
#
#   backlog - The number of pending connections. This refers
#       to the number of clients that can be waiting to be
#       served. Exceeding this number results in the client
#       getting an error when attempting to connect. It should
#       only affect servers under significant load.
#
#       Must be a positive integer. Generally set in the 64-2048
#       range.
#
bind = 'unix:/tmp/{0}.sock'.format(proc_name)
backlog = 2048

#
# Worker processes
#
#   workers - The number of worker processes that this server
#       should keep alive for handling requests.
#
#       A positive integer generally in the 2-4 x $(NUM_CORES)
#       range. You'll want to vary this a bit to find the best
#       for your particular application's work load.
#
#   worker_class - The type of workers to use. The default
#       async class should handle most 'normal' types of work
#       loads. You'll want to read http://gunicorn/deployment.hml
#       for information on when you might want to choose one
#       of the other worker classes.
#
#       An string referring to a 'gunicorn.workers' entry point
#       or a python path to a subclass of
#       gunicorn.workers.base.Worker. The default provided values
#       are:
#
#           egg:gunicorn#sync
#           egg:gunicorn#eventlet   - Requires eventlet >= 0.9.7
#           egg:gunicorn#gevent     - Requires gevent >= 0.12.2 (?)
#           egg:gunicorn#tornado    - Requires tornado >= 0.2
#
#   worker_connections - For the eventlet and gevent worker classes
#       this limits the maximum number of simultaneous clients that
#       a single process can handle.
#
#       A positive integer generally set to around 1000.
#
#   timeout - If a worker does not notify the master process in this
#       number of seconds it is killed and a new worker is spawned
#       to replace it.
#
#       Generally set to thirty seconds. Only set this noticeably
#       higher if you're sure of the repercussions for sync workers.
#       For the non sync workers it just means that the worker
#       process is still communicating and is not tied to the length
#       of time required to handle a single request.
#
#   keepalive - The number of seconds to wait for the next request
#       on a Keep-Alive HTTP connection.
#
#       A positive integer. Generally set in the 1-5 seconds range.
#

workers = 1
worker_class = 'sync'
worker_connections = 1000
timeout = 30
keepalive = 2

#
# Debugging
#
#   debug - Turn on debugging in the server. This limits the number of
#       worker processes to 1 and changes some error handling that's
#       sent to clients.
#
#       True or False
#
#   spew - Install a trace function that spews every line of Python
#       that is executed when running the server. This is the
#       nuclear option.
#
#       True or False
#

debug = False
spew = False

#
# Server mechanics
#
#   daemon - Detach the main Gunicorn process from the controlling
#       terminal with a standard fork/fork sequence.
#
#       True or False
#
#   pidfile - The path to a pid file to write
#
#       A path string or None to not write a pid file.
#
#   user - Switch worker processes to run as this user.
#
#       A valid user id (as an integer) or the name of a user that
#       can be retrieved with a call to pwd.getpwnam(value) or None
#       to not change the worker process user.
#
#   group - Switch worker process to run as this group.
#
#       A valid group id (as an integer) or the name of a user that
#       can be retrieved with a call to pwd.getgrnam(value) or None
#       to change the worker processes group.
#
#   umask - A mask for file permissions written by Gunicorn. Note that
#       this affects unix socket permissions.
#
#       A valid value for the os.umask(mode) call or a string
#       compatible with int(value, 0) (0 means Python guesses
#       the base, so values like "0", "0xFF", "0022" are valid
#       for decimal, hex, and octal representations)
#
#   tmp_upload_dir - A directory to store temporary request data when
#       requests are read. This will most likely be disappearing soon.
#
#       A path to a directory where the process owner can write. Or
#       None to signal that Python should choose one on its own.
#
daemon = True
pidfile = "/tmp/gunicorn.pid"
umask = 0
user = None
group = None
tmp_upload_dir = None

#
#   Logging
#
#   logfile - The path to a log file to write to.
#
#       A path string. "-" means log to stdout.
#
#   loglevel - The granularity of log output
#
#       A string of "debug", "info", "warning", "error", "critical"
#
errorlog = '/var/log/gunicorn/error.log'
loglevel = 'debug'
accesslog = '/var/log/gunicorn/access.log'

#
# Server hooks
#
#   post_fork - Called just after a worker has been forked.
#
#       A callable that takes a server and worker instance
#       as arguments.
#
#   pre_fork - Called just prior to forking the worker subprocess.
#
#       A callable that accepts the same arguments as after_fork
#
#   pre_exec - Called just prior to forking off a secondary
#       master process during things like config reloading.
#
#       A callable that takes a server instance as the sole argument.
#

def post_fork(server, worker):
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
    pass

def pre_exec(server):
    server.log.info("Forked child, re-executing.")

def when_ready(server):
    server.log.info("Server is ready. Spawning workers")

def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")

    ## get traceback info
    import threading, sys, traceback
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
            threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    worker.log.debug("\n".join(code))

def worker_abort(worker):
    worker.log.info("worker received SIGABRT signal")

Il est difficile de taper une commande à chaque fois, alors créez un script. $ vim production_server_rc

#!/bin/sh

GUNICORN=$HOME/.anyenv/envs/pyenv/shims/gunicorn
PROJECT_ROOT=/path/to/flask

APP=app:app

cd $PROJECT_ROOT
exec $GUNICORN -c $PROJECT_ROOT/gunicorn.conf.py $APP

Lancez nginx.

$ sudo /etc/init.d/nginx start

Lancez gunicorn.

$ ./production_server.sh

référence

Installation et utilisation de pyenv et virtualenv gunicorn / examples / gunicorn_rc gunicorn / examples / example_config.py

Recommended Posts

Exécutez Flask sur CentOS avec python3.4, Gunicorn + Nginx.
Exécutez des applications Web Python avec NGINX + NGINX Unit + Flask
[Python] Exécutez Flask sur Google App Engine
Un mémo contenant Python2.7 et Python3 dans CentOS
Exécutez le serveur python wsgi sur l'unité NGINX
Exécutez Python avec VBA
Exécutez prepDE.py avec python3
Créez un environnement python avec ansible sur centos6
Gagnez l'application Web Python + Flask avec Jenkins
[Ansible] Installer dnf sur Centos7 avec l'interpréteur Python3
Installez Python3.4 sur CentOS 6.6
Hello World avec nginx + uwsgi + python sur EC2
Exécutez Blender avec python
Installez Python 2.7.3 sur CentOS 5.4
Programmation avec Python Flask
Utilisation de Flask avec la configuration Nginx + gunicorn [Environnement local]
Exécutez iperf avec python
Jusqu'à ce que Python soit installé avec pythonbrew et que Flask s'exécute sur le serveur WSGI
Exécutez avec CentOS7 + Apache2.4 + Python3.6 pour le moment
Exécutez Paints Chainer sur CPU avec python officiel sur win10
Journal de sortie vers la console avec Flask + Nginx sur Docker
Exécutez python avec PyCharm (Windows)
Touch Flask + courir avec Heroku
Exécutez Python avec CloudFlash (arm926ej-s)
Installez Python 3.8 sur CentOS 7 (SCL)
Exécutez Tensorflow 2.x sur Python 3.7
Exécutez le CGI de Python sur CORESERVER
Exécutez la commande unix sur python
Exécuter Label avec tkinter [Python]
Installez Python 3.8 sur CentOS 8 (AppStream)
Application Web avec Python + Flask ② ③
Paramètres du serveur Python (nginx + Gunicorn)
Remarques sur l'installation de Python sur CentOS
Application Web avec Python + Flask ④
De l'introduction de Flask sur CentOS au service sur Nginx et uWSGI
[Avec diagramme d'image] Nginx + gunicorn + Flask converti en Docker [Partie 2]
Comment installer Python2.7 python3.5 avec pyenv (sur RHEL5 CentOS5) (novembre 2016)
[Avec diagramme d'image] Nginx + gunicorn + Flask converti en Docker [Partie 1]
Déployer l'application Django sur EC2 avec Nginx + Gunicorn + Supervisor
Jusqu'à Hello World avec Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Préparation à l'exécution de Flask sur EC2
Exécutez Rotrics DexArm avec l'API Python
Exécutez l'image Docker de TensorFlow sur Python3
Exécutez SwitchBot avec Bleak sur Windows 10
Exécutez XGBoost avec Cloud Dataflow (Python)
Exécutez Aprili depuis Python sur Orange
Exécutez python3 Django1.9 avec mod_wsgi (déployer)
Notes sur l'utilisation de rstrip avec python.
Construction de l'environnement Python3 avec pyenv-virtualenv (CentOS 7.3)
Installer Python sur CentOS à l'aide de Pyenv
Jusqu'à ce que Python fonctionne sur Apache
Premiers pas avec Python 3.8 sous Windows
Créer un environnement python3 sur CentOS7
Développement d'applications avec Docker + Python + Flask
Installez Python sur CentOS en utilisant pyenv
[Memo] Tweet sur Twitter avec Python
Exécutez régulièrement Python sur Heroku Scheduler