Skip to content

Instantly share code, notes, and snippets.

@amites
Created April 25, 2019 14:59
Show Gist options
  • Save amites/f67a1bcda661d70bc6e5bbe2dd71b860 to your computer and use it in GitHub Desktop.
Save amites/f67a1bcda661d70bc6e5bbe2dd71b860 to your computer and use it in GitHub Desktop.
Nginx & Gunicorn Boilerplate Config
server {
listen 80;
server_name DOMAIN, *.DOMAIN;
location = /favicon.ico {access_log off; log_not_found off; }
location /static/ {
root /PATH/TO/PROJECT/ROOT;
}
location / {
include proxy_params;
proxy_pass http://unix:/PATH/TO/PROJECT/ROOT/PROJECTNAME.sock;
}
client_max_body_size 50M;
}
[Unit]
Description=Gunicorn PROJECTNAME Daemon
After=network.target
[Service]
User=VALIDUSER
Group=www-data
WorkingDirectory=/PATH/TO/PROJECT/ROOT
ExecStart=/PATH/TO/GUNICORN/bin/gunicorn --workers 3 --bind unix:/PATH/TO/PROJECT/ROOT/PROJECTNAME.sock PROJECTDIR.wsgi:application
[Install]
WantedBy=multi-user.target
@amites
Copy link
Author

amites commented Apr 25, 2019

Recommend updating file names to match project to avoid confusion, anything written in UPPERCASE is meant to be replaced
ie VALIDUSER could be ubuntu or any other system user with appropriate permissions

above assumes that VALIDUSER belongs to group www-data

usermod -a -G www-data VALIDUSER

If running Ubuntu for your server

nginx.conf will goto /etc/nginx.sites-available and create a symlink ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled

for systemd to daemonize the gunicorn instance place PROJECT.service under directory

/etc/systemd/system/

update appropriate names

to test the service will work run the updated line from ExecStart in the console as the chosen VALIDUSER

sudo -u VALIDUSER /PATH/TO/GUNICORN/bin/gunicorn --workers 3 --bind unix:/PATH/TO/PROJECT/ROOT/PROJECTNAME.sock PROJECTDIR.wsgi:application

after verifying that works correctly

run systemctl daemon-reload to load the new service

run systemctl start PROJECT to start the service manually

you can verify it's running with ps aux | grep gunicorn
the above config will have multiple instances running, if not try running the command mannually

to make it auto-start on reboot of server

systemctl enable PROJECT

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