LoginSignup
49
52

More than 5 years have passed since last update.

Flask + Gunicorn + Nginx + Supervisor ちゃんと動くまで

Posted at

Flask + Gunicorn + Nginx + Supervisor ちゃんと動くまで

参考:
nginx/gunicorn/supervisorでflaskアプリを動かす | かねしろぐ
http://blog.shun-ichiro.com/howto/nginx-gunicorn-supervisor-flask/

Information

  • wwwディレクトリ: /var/www
  • Flaskアプリディレクトリ: /var/www/apps/
  • 今回使うスクリプトがあるディレクトリ: /var/www/apps/sampleapp/

Flask

ベースになるスクリプト: /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") #トップディレクトリ / は別に使うため
def index():

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

if __name__ == "__main__":

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

Gunicorn

設定ファイル: /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'

一回起動してみる

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

Nginx

デフォルトの設定: /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;
  }
}

このファイルに以下の情報を追加

+ 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;
  + }

}

一回再起動

$ sudo service nginx restart

再起動してGunicorn起動したらちゃんとアクセスできるか確認する。

Supervisor

Supervisorは プロセスをデーモン化 するもの。
それぞれのディストリビューション等のパッケージマネージャでインストールする。

設定ファイル: /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 

gunicornのコマンドは 絶対パス で書く。各々書き換えて下さい

設定ファイルをsupervisorに読み込ませる必要があります。

$ supervisorctl reread
$ supervisorctl update
$ supervisorctl start my_app

これで一応動くはず!!

49
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
49
52