Referenz: Führen Sie die Kolben-App auf Nginx / Gunicorn / Supervisor | Kaneshirogu aus http://blog.shun-ichiro.com/howto/nginx-gunicorn-supervisor-flask/
Information
/ var / www
/ var / www / apps /
/ var / www / apps / sampleapp /
Flask
Basisskript: / 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-Verzeichnis/Separat zu verwenden
def index():
return "<h1>Hello World</h1>"
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)
Gunicorn
Konfigurationsdatei: / 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'
Versuchen Sie einmal zu starten
$ cd /var/www/apps/sampleapp/
$ gunicorn sample:app --config guniconf.py
Nginx
Standardeinstellungen: / 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;
}
}
Fügen Sie dieser Datei die folgenden Informationen hinzu
+ 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;
+ }
}
Einmal neu starten
$ sudo service nginx restart
Überprüfen Sie nach dem Neustart und dem Starten von Gunicorn, ob der Zugriff ordnungsgemäß erfolgt.
Supervisor
Supervisor ** macht einen Prozess zu einem Daemon **. Installieren Sie mit dem Paketmanager jeder Distribution.
Konfigurationsdatei: / 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
Schreibe Gunicorn-Befehle mit ** absolutem Pfad **. Bitte schreiben Sie jedes neu
Sie müssen die Konfigurationsdatei in "Supervisor" laden.
$ supervisorctl reread
$ supervisorctl update
$ supervisorctl start my_app
Dies sollte vorerst funktionieren! !!
Recommended Posts