Cheatsheet for setting up A linux server running supervisor, gunicorn and nginx
User
adduser <username>
sudo usermod -aG sudo <username>
make user login without password
-
sudo EDITOR=vim visudo
-
append line:
User_Alias ADMIN = ubuntu ADMIN ALL = (ALL) NOPASSWD: ALL
ssh
ssh-keygen
cat ~/.ssh/id_rsa.pub
ssh-copy-id username@remote_host
or~/.ssh/authorized_keys
Local ssh config
- vim ~/.ssh/config
Host gitlab
HostName gitlab.com
IdentityFile ~/.ssh/gitlab_rsa
PIP
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
Ref: https://pip.pypa.io/en/stable/installing/
Virtualenv
pip install virtualenvwrapper
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
sudo apt-get install supervisor
/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
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)
add-apt-repository ppa:certbot/certbot && apt-get update
apt-get install certbot
- nginx conf:
location /.well-known {
alias /var/www/html/.well-known;
}
certbot certonly --webroot --webroot-path=/var/www/html -d example.com -d www.eample.com
- 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;
}
}