Configuring Nginx server to python flask applicati

2019-07-13 11:47发布

问题:

i am new to configuring server. And i want to configure my Amazon-EC2 instance. I configured it according to this document. http://www.soundrenalin.com/about

However when i click the url, 502 Bad Gateway error is being encountered. My project is located at this path: /home/ubuntu/dsn/app.
And the /home/ubuntu/dsn folder tree is:

app/
    app.py
    static/
    templates/
    themes/
bin/
build/
include/
lib/
local/
run.py


Here is my nginx config (/etc/nginx/sites-available/default):

server {
        listen   80; 

        root /home/ubuntu/dsn/app
        index index.html index.htm;

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

And here is my uwsgi.ini file:

[uwsgi]
     chdir = /home/ubuntu/dsn/
     uid = www-data
     gid = www-data
     chmod-socket = 666
     socket = /tmp/uwsgi.sock
     module = run
     virtualenv = /home/ubuntu/dsn/   

And the other thing is:
When i run the command tail -f /var/log/nginx/error.log result is:

2013/06/09 15:58:11 [error] 5321#0: *1 connect() to unix:/tmp/uwsgi.sock failed (111: Connection refused) while connecting to upstream, client: <myip>, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "54.218.14.213"

How can i solve this? Thanks.

回答1:

It could be one of the following things:

  1. Your UWSGI socket is not located at /tmp/uwsgi.sock
  2. The uwsgi or www-data user does not have permissions to create uwsgi.sock in your /tmp directory.

If you installed Uwsgi on Ubuntu 12.04, the config file should be located at:

/usr/share/uwsgi/conf/default.ini

Here's the default socket config in that file:

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /run/uwsgi/%(deb-confnamespace)/%(deb-confname)/socket

You should create an app-specific config file that looks similar to mine. I can confirm it works inside a virtual environment.

sudo vim /etc/uwsgi/apps-available/your-app.ini

The contents of your-app.ini:

[uwsgi]
base = /home/nick/your-app.com

#location of the flask application file
file = /home/nick/your-app.com/main.py

#uwsgi varible only, does not relate to your flask application
callable = app

#uwsgi plugins
plugins = http,python

#to create within a virtual environment
home = %(base)/venv

pythonpath = %(base)
socket = /tmp/%n.sock
logto = /var/log/uwsgi/%n.log
workers = 3

To enable, you need to symlink and restart nginx.

#make sure that any user to write to the /tmp directory
sudo chmod 777 /tmp
sudo ln -s /etc/uwsgi/apps-available/your-app.ini /etc/uwsgi/apps-enabled/your-app.ini
sudo service uwsgi restart


回答2:

From https://stackoverflow.com/a/33587478/260127

You can use ubutu's upstart, and uwsgi's --emperor option to execute the python uwsgi apps easily.