Skip to content

Instantly share code, notes, and snippets.

@g33klord
Last active September 9, 2018 07:45
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 g33klord/59a9a16039068dd89543104126db1ff6 to your computer and use it in GitHub Desktop.
Save g33klord/59a9a16039068dd89543104126db1ff6 to your computer and use it in GitHub Desktop.

Cheatsheet for setting up A linux server running supervisor, gunicorn and nginx

User

  1. adduser <username>
  2. sudo usermod -aG sudo <username>

make user login without password

  1. sudo EDITOR=vim visudo

  2. append line:

    User_Alias  ADMIN = ubuntu
    ADMIN       ALL = (ALL) NOPASSWD: ALL
    

ssh

  1. ssh-keygen
  2. cat ~/.ssh/id_rsa.pub
  3. ssh-copy-id username@remote_host or ~/.ssh/authorized_keys

Local ssh config

  1. vim ~/.ssh/config
Host gitlab
    HostName gitlab.com
    IdentityFile ~/.ssh/gitlab_rsa

PIP

  1. wget https://bootstrap.pypa.io/get-pip.py
  2. python get-pip.py

Ref: https://pip.pypa.io/en/stable/installing/

Virtualenv

  1. pip install virtualenvwrapper
  2. source /usr/local/bin/virtualenvwrapper.sh

or /home/<username>/.local/bin/virtualenvwrapper.sh

Ref: http://virtualenvwrapper.readthedocs.io/en/latest/install.html#basic-installation

-> better to use pipenv instead

Supervisor

  1. sudo apt-get install supervisor
  2. /etc/supervisor/conf.d
[program:sigrid]
command=/home/ubuntu/.virtualenvs/sigrid/bin/gunicorn testprep.wsgi
directory = /home/ubuntu/testprep/
stdout_logfile = /home/ubuntu/testprep/log/gunicorn_stdout.log
stderr_logfile = /home/ubuntu/testprep/log//gunicorn_stderr.log
user = ubuntu
redirect_stderr = True
autostart=true
autorestart=true

nginx

  1. sudo apt-get install nginx
server {
    listen 80;
    server_name example.com;

    access_log /var/www/example/logs/access.log;
    error_log /var/www/example/logs/error.log;

    location /static/ {
        alias /var/www/example/static/;
        expires 10d;
    }

    location /media/ {
        alias /var/www/example/media/;
        expires 10d;
    }

    location / {
    	proxy_pass         http://localhost:9000;
    	proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
	}

SSL (Let's encrypt)

  1. add-apt-repository ppa:certbot/certbot && apt-get update
  2. apt-get install certbot
  3. nginx conf:
location /.well-known {
      alias /var/www/html/.well-known;
   }
  1. certbot certonly --webroot --webroot-path=/var/www/html -d example.com -d www.eample.com
  2. ssl config:
server {
       listen         80;
       server_name    example.com;
       return         301 https://$server_name$request_uri;
}


server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    access_log /var/www/example/log/access.log;
    error_log /var/www/example/log/error.log;

    location /static/ {
        alias  /var/www/example/static/;
        expires 10d;
    }

    location /media/ {
        alias /var/www/example/media/;
        expires 10d;
    }

   location /.well-known {
      alias /var/www/html/.well-known;
   }

    location / {
        proxy_pass         http://localhost:8081;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment