kduy
4/19/2017 - 10:05 PM

DevOps

DevOps

export $BASE=/path/to/project/

Flask

File $BASE/sample.py

from flask import Flask

  application = Flask(__name__)
  
  @application.route("/")
  def hello():
      return "<h1 style='color:blue'>Hello</h1>"

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

Gunicorn

File $BASE/gunicorn.config.py

bind = '127.0.0.1:8000'
errorlog = '/Users/kidio/logs/gunicorn-error.log'
accesslog = '/Users/kidio/logs/gunicorn-access.log'
loglevel = 'debug'
workers = 5

File $BASE/wsgi.py

from sample import application

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

Run gunicorn with daemon even the session is shutdown

cd $BASE
gunicorn -c gunicorn.conf.py wsgi:application --pid ~/logs/gunicorn.pid --daemon

Nginx

File $BASE/nginx.conf

server {
    listen 8080;
    server_name localhost;

    access_log /Users/kidio/logs/access.log;     # <- make sure to create the logs directory
    error_log /Users/kidio/logs/error.log;       # <- you will need this file for debugging

    location / {
        proxy_pass http://127.0.0.1:8000;         # <- let nginx pass traffic to the gunicorn server
    }

    location /static {
        root /Volumes/Data/WORKSPACE/git/motionscloud/sample_flask;
    }
}

From sites-enabled, create a soft link referring to the config file of project

cd /usr/local/etc/nginx/site-enabled
ln -s /Users/kidio/git/motionscloud/sample_flask/nginx.config
 sample_project

Import all file in sites-enabled to config file in nginx root

vim /usr/local/etc/nginx/nginx.conf

move this line include /usr/local/etc/nginx/sites-enabled/*; under http

From project folder, start nginx

sudo nginx

To stop it,

sudo nginx -s stop

To reload the config file without shutting down the server:

sudo nginx -s reload

access to localhost:8080

References:

1 2 3