简体   繁体   中英

Deploying Python flask application using nginx/gunicorn on Amazon Linux EC2 instance

I am trying to deploy a python flask app on an Amazon Linux EC2 instance. I am unable to spin up the upstart script and get it running.

I am following this tutorial (which is for ubuntu): https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04

My config file looks like: myapp.conf

description "Gunicorn application server running myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid user
setgid www-data

env PATH=/home/ec2-user/flask/venv/bin
chdir /home/ec2-user/flask
exec gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi

My wsgi file looks like: wsgi.py

from app import application

if __name__ == "__main__":
    application.run()

My actual application looks like: app.py

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return ""Hello World"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

When I do a:

sudo start myapp

I get an error saying unknown job. What exactly do I have to do to get this running? Or is there a different way to make upstart scripts on Amazon Linux instances? Could someone please help

I ran into the same issue as well. After some digging, here is what I found.

The start: Unknown job: myapp message indicates something wrong with the Upstart script myapp.conf . You can check the logs by running sudo tail -n 5 /var/log/messages

init: /etc/init/myapp.conf:7: Unknown stanza

In this case, it is having issue with setuid . According to Upstart documentation , setuid was added in Upstart v1.4.

Now checking the version of Upstart installed by running initctl --version . It returned initctl (upstart 0.6.5) , which explains why it doesn't work with Amazon Linux. Unfortunately, there seems to be no easy way to upgrade Upstart .

As a proof of concept, you can comment out setuid and setgid (I also changed the --bind parameter)

description "Gunicorn application server running myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
#setuid user
#setgid www-data

env PATH=/home/ec2-user/flask/venv/bin
chdir /home/ec2-user/flask
exec gunicorn --workers 3 --bind 0.0.0.0:8000 wsgi

And then it should work

sudo start myapp

However, this is not recommended as it would run gunicorn as root . And instead of using setuid and setgid , Upstart doc mentioned that

if you are not using Upstart 1.4, it is easy to accomplish the required goal. There are a couple of methods you can use. The recommended method for Debian and Ubuntu systems is to use the helper utility start-stop-daemon(8) like this:

exec start-stop-daemon --start -c myuser --exec command

Unfortunately, start-stop-daemon is not available on Amazon Linux either...There are other possible replacements, such as daemon and runuser , but at this point I don't think it's worth to pursue using Upstart . There are many other monitoring options for Gunicorn . I ended up using Supervisor instead.

As of the time of this writing, using gunicorn 19.9.0 , there are the arguments --user and --group that can be specified with the gunicorn command. So, in order to run your gunicorn wsgi server as non-root on amazon linux with upstart 0.6.5 (ie ec2-user:nginx ), you can specify this in the exec stanza of your upstart script as follows (this example uses a unix socket but an address:port will work too:

exec gunicorn --user ec2-user --group nginx --bind socket.sock wsgi

Also, in your case, the nginx version that is currently in the amazon linux yum repo does not use the group www-data or the root directory /var/www/html . This is the way it exists in the ubuntu apt repo, and is meant to mimic apache. Instead, the amazon linux yum repo uses the group nginx and the root directory /usr/share/nginx/html . You could set up your system to mimic ubuntu, but this is not the default setup. In that case, just use:

exec gunicorn --user ec2-user --group www-data --bind socket.sock wsgi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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