Skip to content

Instantly share code, notes, and snippets.

@berceanu
Created August 12, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berceanu/ccc3365bc55b7b762fa0fbdb3639f4ea to your computer and use it in GitHub Desktop.
Save berceanu/ccc3365bc55b7b762fa0fbdb3639f4ea to your computer and use it in GitHub Desktop.
Instructions for setting up multiple gunicorn instances on subdomains via nginx

Initial setup for Nginx + Gunicorn + signac-dashboard

Install dependencies

This guide assumes a working Ubuntu 18.04 installation.

$ sudo apt update
$ sudo apt install nginx
$ sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
$ sudo apt install python3-venv

Configure Gunicorn

$ conda activate random-phase-approximation
$ conda install gunicorn
$ cd ~/Development/random-phase-approximation/signac/projects/rpa/src
$ gunicorn --bind 0.0.0.0:5000 wsgi:dashboard
# /etc/systemd/system/rpa.service
[Unit]
Description=Gunicorn instance to serve random-phase-approximation/rpa
After=network.target

[Service]
User=berceanu
Group=www-data
WorkingDirectory=/home/berceanu/Development/random-phase-approximation/signac/projects/rpa/src
Environment="PATH=/home/berceanu/miniconda3/envs/random-phase-approximation/bin"
ExecStart=/home/berceanu/miniconda3/envs/random-phase-approximation/bin/gunicorn --workers 3 --bind unix:rpa.sock -m 007 wsgi:dashboard

[Install]
WantedBy=multi-user.target
$ sudo systemctl start rpa
$ sudo systemctl enable rpa
$ sudo systemctl status rpa

Notes:

  1. Add multiple services by changing the respective paths above and follow the same steps to enable them.

Configure Nginx

# /etc/nginx/sites-available/rpa
server {
    listen 80;
    listen [::]:80;

    server_name rpa.ra5.ro www.rpa.ra5.ro;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/berceanu/Development/random-phase-approximation/signac/projects/rpa/src/rpa.sock;
    }
}
$ sudo ln -s /etc/nginx/sites-available/rpa /etc/nginx/sites-enabled
$ sudo nginx -t
$ sudo systemctl restart nginx

Notes

  1. Add more subdomains by changing the sock path above and making the respective symlinks.
  2. The subdomains need to have correct DNS A-record entries.
  3. The max socks path length is 107 chars.

Add page to root domain with links to subdomains

$ sudo mkdir -p /var/www/ra5.ro/html
$ sudo chown -R $USER:$USER /var/www/ra5.ro/html
$ sudo chmod -R 755 /var/www/ra5.ro
$ vi /var/www/ra5.ro/html/index.html
<html>
    <head>
        <title>ra5.ro</title>
    </head>
    <body>
        <h1><a href="http://rpa.ra5.ro">rpa</a></h1>
        <h1><a href="http://rpa.agg.ra5.ro">rpa-aggregation</a></h1>
        <h1><a href="http://rpa.agg.anim.ra5.ro">rpa-animation</a></h1>
    </body>
</html>
$ sudo vi /etc/nginx/sites-available/ra5.ro
# /etc/nginx/sites-available/ra5.ro
server {
        listen 80;
        listen [::]:80;

        root /var/www/ra5.ro/html;
        index index.html index.htm index.nginx-debian.html;

        server_name ra5.ro www.ra5.ro;

        location / {
                try_files $uri $uri/ =404;
        }
}
$ sudo ln -s /etc/nginx/sites-available/ra5.ro /etc/nginx/sites-enabled/
$ sudo vi /etc/nginx/nginx.conf
# /etc/nginx/nginx.conf
...
http {
    ...
    server_names_hash_bucket_size 64;
    ...
}
...
$ sudo nginx -t
$ sudo systemctl restart nginx

Adapted from this guide.

See also how to configure Nginx(and links therein).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment