AWS nginx+python+flask

Run an instance in EC2 (The key-pair is set after the launch button is pressed)

Don’t forget to modify the security group to permit all tcp connections.

Install packages from this article

https://github.com/d5/docs/wiki/Installing-Flask-on-Amazon-EC2

Do all the steps but a bit differently:

  • Don’t change /etc/nginx/nginx.conf
  • Create a file /etc/nginx/sites-available/test:
server {
listen 10081;
server_name 54.187.82.133;
    location / {
include uwsgi_params;
uwsgi_pass unix://var/run/uwsgi.sock;
}
}
  • Create simlink:
sudo ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled
  • Create a flask application file test.py:
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello World!’
if __name__ == ‘__main__’:
app.run(host=’0.0.0.0', port=10081)
  • Create a uwsgi file uwsgi.yaml
uwsgi:
socket: /run/uwsgi.sock
master: 1
workers: 1
pidfile: /tmp/uwsgi.pid
#daemonize: /var/log/uwsgi.log
module: test:app
uid: www-data
gid: www-data
chmod-socket: 664
chown-socket: www-data:www-data
  • Run sudo uwsgi — yaml uwsgi.yaml