Question

I'm trying to deploy a simple flask app on my raspberry pi using nginx. I've followed these two guides:

http://www.onurguzel.com/how-to-run-flask-applications-with-nginx-using-gunicorn/ http://www.onurguzel.com/managing-gunicorn-processes-with-supervisor/

And have got everything running without error. But when I load a web browser pointing at my PI's IP (I work over ssh) - all I see is the default "welcome to nginx" page. What's going on?

here are my files:

/home/pi/hello/hello.py

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)

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

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == '__main__':
    app.run()

/etc/nginx/sites-available/hello.conf (symlinked to: /etc/nginx/sites-enabled/)

server {
    listen 80;
    server_name hello.itu24.com;

    root /home/pi/hello/hello.py;

    access_log /home/pi/hello/access.log;
    error_log /home/pi/hello/error.log;

    location / {
        try_files $uri @gunicorn_proxy;
        }

    location @gunicorn_proxy {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8000;
        }
}    

Here's my nginx.conf (though I have not changed it at all)

/etc/nginx/nginx.conf

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

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;

        ##
        # 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/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

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

For the supervisor part:

/etc/supervisor/conf.d/hello.conf

[program:hello]
command = /home/pi/hello/bin/python /home/pi/hello/bin/gunicorn hello:app
directory = /home/pi/hello
user = pi

I can spin everything up with :

sudo supervisorctl start hello

But when I hit my Pi's IP:

http://192.168.1.28 

from my macs browser all I get is: "Welcome to nginx"

Any ideas? This is my first server that I'm running and deploying to - running it on a Ras Pi probably wasn't the best idea but I'm learning a lot so far.

Était-ce utile?

La solution

You might running flask on the default port, which is 5000.

Try changing this line:

if __name__ == '__main__':
    #app.run()
    app.run(port=8000)

or change your supervisord command to:

command = /home/pi/hello/bin/python /home/pi/hello/bin/gunicorn hello:app -b 0.0.0.0:8000

Autres conseils

You might want to make sure the default site is disabled. Simply delete the symlink default from sites-enabled.

Also, the default port for Flask is 5000 not 8000, so in your nginx configuration, you need to change the following:

location @gunicorn_proxy {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:5000; # Default port
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top