Skip to content

Instantly share code, notes, and snippets.

@codeasashu
Last active November 12, 2019 07:35
Show Gist options
  • Save codeasashu/9419bb42f41981d71201d9182b9a9ceb to your computer and use it in GitHub Desktop.
Save codeasashu/9419bb42f41981d71201d9182b9a9ceb to your computer and use it in GitHub Desktop.

Start your application in uwsgi

Create a file wsgi.py and write your bootstrap script like this:

from app import applicationn

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

Install Gunicorn

Install gunicorn3 using

apt-get install gunicorn3

Install and configure superviror

apt-get install supervisord

cat /etc/supervisor/conf.d/example.conf

[program:gunicorn]
# Use 3 worker process, use 127.0.0.1 and forwarded-allow-ips so no other application can access other than reverse proxy
command=gunicorn -b="127.0.0.1:9000" -w 3 --forwarded-allow-ips="localhost" --access-logfile /var/log/app/gunicorn_access.log --error-logfile /var/log/app/gunicorn_error.log --log-level debug config.wsgi
directory=/var/app/python/
user=nobody
autostart=true
autorestart=true
redirect_stderr=true

Create the log directory and give it 777 perms:

mkdir /var/log/app
chmod -R 777 /var/log/app

Use nginx or apache as reverse proxy

upstream app_server {
     server localhost:9000 fail_timeout=0;
  }

 server {
    # use 'listen 80 deferred;' for Linux
    # use 'listen 80 accept_filter=httpready;' for FreeBSD
    listen 80 443;
    client_max_body_size 4G;

    # set the correct host(s) for your site
    server_name example.com localhost;

    keepalive_timeout 5;

    # path for static files

    location / {
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://app_server;
    }

    error_page 500 502 503 504 /500.html;
    access_log /var/log/nginx/app.log combined;
    error_log  /var/log/nginx/app.log warn;

    location = /500.html {
      root /path/to/app/current/public;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment