简体   繁体   English

如何使用flask / uwsgi / nginx服务器管理内容和python代码?

[英]how to manage content and python code with a flask/uwsgi/nginx server?

After running Flask for a few days as a live webserver, I learned this is not a smart thing to do: after some hours of inactivity the server died, even when I used the most simple setup (one page, no python code). 在将Flask作为实时网络服务器运行了几天之后,我知道这不是一件明智的事情:经过数小时的不活动之后,服务器死了,即使我使用最简单的设置(一页,没有python代码)也是如此。 Googling around I found out that Flask is not meant to be used as production server and a good practice is to combine it with eg Nginx and uWSGI. 在谷歌上搜索时,我发现Flask并不是要用作生产服务器,而好的做法是将其与Nginx和uWSGI结合使用。 So I followed this guide and it seems to be running well. 因此,我遵循了本指南 ,它似乎运行良好。 But now I have no clue how to restart, refresh or reload whatever it takes to publish te stuff I add to the content or to the python code. 但是现在我不知道如何重新启动,刷新或重新加载发布到内容或python代码中的东西所需要的一切。 In stead of the Flask server, which reloads automatically after a file is saved (when in debug mode) I now have three engines running. 代替Flask服务器,该服务器在保存文件后(在调试模式下)会自动重新加载,现在我正在运行三个引擎。 Restarting nginx in terminal does not work, I've already tried that. 在终端中重启nginx不起作用,我已经尝试过了。

Please can somebody help this noob out? 请有人可以帮助这个菜鸟吗?

TIA!! TIA!

uwsgi_conf.ini: uwsgi_conf.ini:

============================ ============================

[uwsgi]

chdir = /home/pi/sampleApp
module = sample_app:first_app

master = true
processes = 1
threads = 2

uid = www-data 
gid = www-data
socket = /tmp/sample_app.sock
chmod-socket = 664
vacuum = true

die-on-term = true

============================ ============================

nginx.conf: nginx.conf:

============================ ============================

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

============================ ============================

sample_app_proxy: sample_app_proxy:

============================ ============================

server {
 listen 80;
 server_name localhost;

 location / { try_files $uri @app; }
 location @app {
 include uwsgi_params;
 uwsgi_pass unix:/tmp/sample_app.sock;
 }
}

============================ ============================

When used with Flask, Nginx is acting as a proxy server, which means there's no need to restart it when you want to reload your Flask app. 与Flask一起使用时,Nginx充当代理服务器,这意味着当您要重新加载Flask应用时无需重新启动它。 It's the uWSGI that you need to take care of, since uWSGI is sitting in the middle between Flask app and Nginx and is responsible for forwarding all requests towards your app, and, well, running it. 因为uWSGI位于Flask应用程序和Nginx之间,并且负责将所有请求转发到您的应用程序,并且很好地运行它,所以您需要照顾这是uWSGI。

Solution 1 解决方案1

One of the common approaches is to add following into uwsgi_conf.ini : 常见方法之一是在uwsgi_conf.ini添加以下uwsgi_conf.ini

py-autoreload = 1

This will tell uWSGI that it needs to monitor file timestamps every second and reload the app once triggered. 这将告诉uWSGI它需要每秒监视文件时间戳并在触发后重新加载应用程序。

Solution 2 解决方案2

Send graceful reload command to uWSGI Master FIFO : 发送优美的重载命令到uWSGI Master FIFO

Add following to your uwsgi_conf.ini : 将以下内容添加到您的uwsgi_conf.ini

master-fifo = /var/run/flask_uwsgi_fifo

And then reload uWSGI once you're done with your Flask source file changes: 完成Flask源文件更改后,然后重新加载uWSGI:

$ echo r > /var/run/flask_uwsgi_fifo

Solution 3 解决方案3

Similar to solution 2 , but via touch-reload . 解决方案2类似,但通过touch-reload

Add following to your uwsgi_conf.ini : 将以下内容添加到您的uwsgi_conf.ini

touch-reload = /var/run/flask_touch

And then reload your app via: 然后通过以下方式重新加载您的应用程序:

$ touch /var/run/flask_touch

Solution 4 解决方案4

Send SIGHUP to uWSGI pid file. SIGHUP发送到uWSGI pid文件。

Add following to your uwsgi_conf.ini : 将以下内容添加到您的uwsgi_conf.ini

safe-pidfile = /tmp/flask.pid

And then reload your app via: 然后通过以下方式重新加载您的应用程序:

$ kill -HUP `cat /tmp/flask.pid`

or 要么

$ uwsgi --reload /tmp/flask.pid

uwsgi is the part that hosts your application and so it is the part that needs to be restarted if the application changes. uwsgi是承载应用程序的部分,因此如果应用程序发生更改,它是需要重新启动的部分。

For auto-reload, you could try this answer: https://stackoverflow.com/a/41529718/187292 对于自动重新加载,您可以尝试以下答案: https : //stackoverflow.com/a/41529718/187292

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM