Skip to content

Instantly share code, notes, and snippets.

@TanjimReza
Created August 2, 2023 17:56
Show Gist options
  • Save TanjimReza/5892cb2d4dbe19d9b91e0df3fd484896 to your computer and use it in GitHub Desktop.
Save TanjimReza/5892cb2d4dbe19d9b91e0df3fd484896 to your computer and use it in GitHub Desktop.
VPS Configuration with Gunicorn + Nginx

Setting up a Python[Django/Flask] on VPS

Personal Deployment Guide - Tanjim Reza - 02-08-2023

  • Must Create Project Dir in /home/projectname

  • Project Root -> /home/projectname/htdocs/subdomain.domain.com

  • Nginx | VHost | /etc/nginx/sites-available/projectname

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    {{ssl_certificate_key}}
    {{ssl_certificate}}
    server_name subdomain.domain.com www.subdomain.domain.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/projectname/htdocs/subdomain.domain.com/projectname.sock;
    }
}
  • Create python3 venv env

    • pip install gunicorn
    • pip install whitenoise
  • Project Service for AutoStartup | /etc/systemd/system/projectname.service

[Unit]
Description=Gunicorn for projects
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/projectname/htdocs/subdomain.domain.com
Environment="PATH=/home/projectname/htdocs/subdomain.domain.com/env/bin"
ExecStart=/home/projectname/htdocs/subdomain.domain.com/env/bin/gunicorn --workers 3 --bind unix:usus.sock --umask 007 DjangoProjectName.wsgi:app

[Install]
WantedBy=multi-user.target

  • Project Gunicorn Status Check

    • sudo systemctl start projectname
    • sudo systemctl enable projectname
    • sudo systemctl status projectname
    • ** Ignore static errors **
  • Symlink for Nginx Server Config

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
  • Restart Processes

    • sudo systemctl daemon-reload
    • sudo systemctl restart projectname
    • sudo systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment