Flask + Gunicorn + Nginx + Supervisor Until it works

Flask + Gunicorn + Nginx + Supervisor Until it works

Reference: Run the flask app on nginx / gunicorn / supervisor | Kaneshirogu http://blog.shun-ichiro.com/howto/nginx-gunicorn-supervisor-flask/

Information

Flask

Base script: /var/www/apps/sampleapp/sample.py

sample.py


#!/usr/bin/env python
#coding: utf-8

from flask import Flask

app = Flask("sample")

@app.route("/sample") #Top directory/To use separately
def index():

    return "<h1>Hello World</h1>"

if __name__ == "__main__":

    app.run(host="0.0.0.0",debug=True)

Gunicorn

Configuration file: /var/www/apps/sampleapp/guniconf.py

guniconf.py


import multiprocessing

# Server Socket
bind = 'unix:/tmp/gunicorn_my_app.sock'
backlog = 2048

# Worker Processes
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'sync'
worker_connections = 1000
max_requests = 0
timeout = 30
keepalive = 2
debug = False
spew = False

# Logging
logfile = '/var/www/apps/sampleapp/app.log'
loglevel = 'info'
logconfig = None

# Process Name
proc_name = 'gunicorn_my_app'

Try to start once

$ cd /var/www/apps/sampleapp/
$ gunicorn sample:app --config guniconf.py

Nginx

Default settings: /etc/nginx/sites-enabled/000-default.conf

server {
  listen 80 default_server;

  root /var/www;
  index index.php index.html index.htm;

  location / {
          try_files $uri $uri/ /index.php?q=$uri&$args;
          autoindex on;
  }

  error_page 404 /404.html;

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
          root /usr/share/nginx/html;
  }

  location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  }
}

Add the following information to this file

+ upstream my_app_server{
+     #server unix:/tmp/gunicorn_my_app.sock fail_timeout=0;
+     server unix:/tmp/gunicorn_my_app.sock;
+ }

server {
  listen 80 default_server;

  root /var/www;
  index index.php index.html index.htm;

  location / {
          try_files $uri $uri/ /index.php?q=$uri&$args;
          autoindex on;
  }

  error_page 404 /404.html;

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
          root /usr/share/nginx/html;
  }

  location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  }

  + location /sample { 
  +         try_files $uri @my_app;
  + }
  + 
  + location @my_app {
  + 
  +         proxy_set_header Host $host;
  +         proxy_set_header X-Real-IP $remote_addr;
  +         proxy_redirect off;
  +     
  +         proxy_pass http://my_app_server;
  + }

}

Reboot once

$ sudo service nginx restart

After restarting and starting Gunicorn, check if it can be accessed properly.

Supervisor

Supervisor ** daemonizes a process **. Install with the package manager of each distribution.

Configuration file: /etc/supervisor/conf.d/my_app.conf

[program:my_app]
command = /home/***/.pyenv/shims/gunicorn sample:app --config /var/www/apps/sampleapp/guniconf.py
directory = /var/www/apps/sampleapp/
user = root 

Write gunicorn commands with ** absolute path **. Please rewrite each

You need to load the config file into supervisor.

$ supervisorctl reread
$ supervisorctl update
$ supervisorctl start my_app

This should work for the time being! !!

Recommended Posts

Flask + Gunicorn + Nginx + Supervisor Until it works
Until it works with virtualenv + flask + apache + wsgi
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Using Flask with Nginx + gunicorn configuration [Local environment]
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 2]
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)