Skip to content

Instantly share code, notes, and snippets.

@singerng
Last active July 25, 2023 20:13
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save singerng/ebbcc4c09fdb58cfce3f6000635cc46d to your computer and use it in GitHub Desktop.
Save singerng/ebbcc4c09fdb58cfce3f6000635cc46d to your computer and use it in GitHub Desktop.
Config for Django/Pipenv/Gunicorn/Nginx/Systemd.

Reasonable default configuration for a web app that uses Django and the following dependencies:

  • Pipenv: Python package manager
  • Gunicorn: WSGI server
  • Nginx: Static webserver passing to gunicorn, handling SSL, etc.
  • Systemd: Used to load Gunicorn on startup

This is my goto for setting up projects.

server {
listen 80;
server_name <domain>;
location /.well-known {
root /srv/ssl/<site>/;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
# Server declaration
listen 443 ssl;
client_max_body_size 200M;
server_name <domain>;
location /static {
root /srv/http/<site>;
}
location / {
include proxy_params;
proxy_pass http://unix:/srv/http/<site>/<site>.sock;
}
# SSL
ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/<domain>/chain.pem;
include /etc/nginx/snippets/intermediate.conf;
}
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=<site>
Group=<site>
WorkingDirectory=/srv/http/<site>
ExecStart=/usr/local/bin/pipenv run gunicorn --access-logfile - --workers 3 --bind unix:/srv/http/<site>/<site>.sock <site>.wsgi:application
[Install]
WantedBy=multi-user.target
@texasroh
Copy link

Awesome. Thanks

@nesssery
Copy link

Nice ! Thanks

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