greenelab / connectivity-search-backend

Django backend for hetnet connectivity search
https://search-api.het.io
BSD 3-Clause "New" or "Revised" License
6 stars 2 forks source link

Automate Nginx Deployment #29

Open dongbohu opened 5 years ago

dongbohu commented 5 years ago

This Django app is called by Nginx, which behaves as a reverse proxy to client requests. Here is the nginx configuration file:

# HTTP configuration 
# Default HTTP server: always redirect to HTTPS
server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# HTTPS server
server {
    listen 443;
    server_name search-api.het.io;

    if ( $http_host !~* ^(search-api\.het\.io)$ ) {
        return 444;
    }

    ssl on;
    ssl_certificate /etc/letsencrypt/live/search-api.het.io/fullchain.pem;   # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/search-api.het.io/privkey.pem; # managed by Certbot

    charset utf-8;

    # max upload size (adjust to taste)
    client_max_body_size 10M;

    location / {
        return 301 $scheme://$host/v1;
    }

   location /static {
       alias /home/ubuntu/hetmech-backend/dj_hetmech/static;
    }

    location /v1 {
        proxy_pass http://127.0.0.1:8001/v1;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }

}

The directory /home/ubuntu/hetmech-backend/dj_hetmech/static (which holds static files for API view) should be populated by the following management command:

python manage.py collectstatic

Gunicorn is started at boot time by supervisord, with the following configuration:

command=/home/ubuntu/miniconda3/envs/hetmech-backend/bin/gunicorn dj_hetmech.wsgi:application --bind 127.0.0.1:8001 --error-logfile /tmp/hetmech-g
unicorn.log -w 3
directory=/home/ubuntu/hetmech-backend/
user=nobody
group=nobody
autostart=true
autorestart=true
priority=991
stopsignal=KILL
dhimmel commented 5 years ago

This is helpful to jot down. Hopefully, in the medium term, we can design a solution that produces the server instance in an entirely scripted way. One solution that comes to mind is for this repository to define a Docker image with its environment. This image could be built / deployed by CI as necessary. @dongbohu interested to hear what you think is the best solution?

dongbohu commented 5 years ago

For now I can write a shell script to keep track of all deployment steps. Then if we (or users of this repo) need to deploy the backend, all they need to do is run this script on either docker container or cloud instances such as AWS EC2 or Google Cloud etc.