Skip to content

Instantly share code, notes, and snippets.

@edchelstephens
Last active June 13, 2023 04:49
Show Gist options
  • Save edchelstephens/984a6bb3ce65a065183ec0583a3cfa26 to your computer and use it in GitHub Desktop.
Save edchelstephens/984a6bb3ce65a065183ec0583a3cfa26 to your computer and use it in GitHub Desktop.
Django Deployment How To

Deployment 🚀🚀🚀

Deploy on Ubuntu 22.04 with Postgres, Nginx and Gunicorn

  1. Create server via AWS EC2 or Digital Ocean Droplet, or other similar service
  1. Prepare server with this setup: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-22-04

  2. Allow ubuntu user to have sudo privileges

  • logged in via ssh
  • type sudo visudo
  • add username ALL=(ALL) NOPASSWD: ALL to the file
  • save and exit
  1. Create ssh keys for server add add to git so server can download repo

  2. Download repo

  3. Create .env file from .env.sample: cp .env.sample .env

  4. Follow tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04

  5. Check for static files access sudo -u www-data stat /home/ubuntu/djangoboys/static

It it gives the error: stat: cannot statx '/home/ubuntu/djangoboys/static': Permission denied

Then

Add the www-data to the username group:

sudo gpasswd -a www-data username

  1. If nginx gives this error: nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64 nginx: configuration file /etc/nginx/nginx.conf test failed

Then

Edit the nginx.conf and on the http block, add this line:

http{ server_names_hash_bucket_size 64; ... }

  1. Also check this tutorial in case of errors: https://pylessons.com/django-deployment

  2. Visit your site and verify deployment

Follow Along Video Tutorials ⏯️ ⏯️ ⏯️

  1. https://www.youtube.com/watch?v=1fjpNXK7yqc
  2. https://www.youtube.com/watch?v=7O1H9kr1CsA
  3. https://www.youtube.com/watch?v=1D4mn_rO2cM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment