🚀Django Deployment

Django Deployment to Ubuntu 18.04

In this guide, we will go through all the steps to create a VPS, secure it, and deploy a Django application. This is a summarized document from this digital ocean docarrow-up-right

Any commands with "$" at the beginning run on your local machine and any "#" run when logged into the server

Security & Access

Creating SSH keys (Optional)

You can choose to create SSH keys to log in if you want. If not, you will get the password sent to your email to login via SSH

To generate a key on your local machine

$ ssh-keygen

Hit enter all the way through and it will create a public and private key at

~/.ssh/id_rsa
~/.ssh/id_rsa.pub

You want to copy the public key (.pub file)

$ cat ~/.ssh/id_rsa.pub

Copy the entire output and add as an SSH key for Digital Ocean

Login To Your Server

If you setup SSH keys correctly the command below will let you right in. If you did not use SSH keys, it will ask for a password. This is the one that was mailed to you

Create a new user

It will ask for a password, use something secure. You can just hit enter through all the fields.

Give root privileges

SSH keys for the new user

Now we need to setup SSH keys for the new user. You will need to get them from your local machine

Exit the server

You need to copy the key from your local machine so either exit or open a new terminal

You can generate a different key if you want but we will use the same one so lets output it, select it and copy it

Log back into the server

Add SSH key for new user

Navigate to the new users home folder and create a file at '.ssh/authorized_keys' and paste in the key

Login as new user

You should now get let in as the new user

Disable root login

Change the following

Reload sshd service

Simple Firewall Setup

See which apps are registered with the firewall

Allow OpenSSH

Enable firewall

To check status

We are now done with access and security and will move on to installing software

Software

Update packages

Install Python 3, Postgres & NGINX

Postgres Database & User Setup

You should now be logged into the pg shell

Create a database

Create user

Give the User access to the database

Quit out of Postgres

Virtual Environment

You need to install the python3-venv package

Create a project directory

Create venv

Activate the environment

Git & Upload

Pip dependencies

From your local machine, create a requirements.txt with your app dependencies. Make sure you push this to your repo

Create a new repo and push to it (you guys know how to do that)

Clone the project into the app folder on your server (Either HTTPS or setup SSH keys)

Install pip modules from requirements

You could manually install each one as well

Local Settings Setup

Add code to your settings.py file and push to server

Create a file called local_settings.py on your server alongside of settings.py and add the following

  • SECRET_KEY

  • ALLOWED_HOSTS

  • DATABASES

  • DEBUG

  • EMAIL_*

Run Migrations

Create superuser

Create static files

Create an exception for port 8000

Run Server

Test the site at YOUR_SERVER_IP:8000

Add some data in the admin area

Gunicorn Setup

Install gunicorn

Add to requirements.txt

Test Gunicorn serve

Your images, etc will be gone

Stop server & deactivate virtual env

Open gunicorn.socket file

Copy this code, paste it in and save

Open gunicorn.service file

Copy this code, paste it in and save

Start and enable Gunicorn socket

Check the status of guinicorn

Check the existence of gunicorn.sock

NGINX Setup

Create a project folder

Copy this code and paste into the file

Enable the file by linking to the sites-enabled dir

Test NGINX config

Restart NGINX

Remove port 8000 from firewall and open up our firewall to allow normal traffic on port 80

You will probably need to up the max upload size to be able to create listings with images

Open up the nginx conf file

Add this to the http{} area

Reload NGINX

Media File Issue

You may have some issues with images not showing up. I would suggest, deleting all data and starting fresh as well as removeing the "photos" folder in the "media folder"

Domain Setup

Go to your domain registrar and create the following a record

Go to local_settings.py on the server and change "ALLOWED_HOSTS" to include the domain

Edit /etc/nginx/sites-available/btre_project

Reload NGINX & Gunicorn

Last updated