Create and Deploy Django App in 10 Minutes Using Git & SSH

dboost.me
3 min readMay 23, 2022

--

Sometimes things should not be complicated. To illustrate this we are going to show how one can create and deploy an application with a few tools only: python git and ssh

What’s required:

  • git repository
  • remote server and ssh access to it
  • installed python and venv on local and remote devices

Building an App

Firstly, we need to create a dependency file requirements.txt in the project directory and add django dependency there:

# requirements.txtDjango==4.0.4

Let’s create a virtual environment and install dependencies:

# create virtual environment
python3 -m venv .venv
# activate virtual environment
source .venv/bin/activate
# installing dependencies
pip install -r requirements.txt

Now we have our virtual environment set with django dependencies. It’s time to create Django project.

# create project in current directory
django-admin startproject app .
# create directory for django application
mkdir -p apps/hello_app
# create django application within django project
django-admin startapp hello_app apps/hello_app

Content of the directory will look like this:

Include hello_app application in the list of INSTALLED_APPS in app/settings.py

# app/settings.pyINSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.hello_app'
]

Change name of the freshly generated app from hello_app to apps.hello_app in apps/hello_app/apps.py

# apps/hello_app/apps.pyclass HelloAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.hello_app'

Creating app

On the next step we are going to add a simple endpoint to apps/hello_app/views.py file:

from django.http import HttpResponse


def hello_world(request):
return HttpResponse("Hello, World!")

And register it in app/urls.py :

from apps.hello_app.views import hello_world

from django.urls import path

urlpatterns = [
path('hello', hello_world)
]

Our app is ready to use! Let’s check it with python manage.py runserver . The output should contain:

Starting development server at http://127.0.0.1:8000/

For the future deployment let’s modify app/settings.py and change the value for ALLOWED_HOSTS setting:

ALLOWED_HOSTS = [
h for h in os.getenv('ALLOWED_HOSTS', '').split(",") if h
]

This will allow to pass ALLOWED_HOST through env variable which we are going to use on the remove machine.

Setting up git

Keeping your code in a VCS allows you to use it in your deployment pipeline. So let’s create and push our project to the remote git repository.

  1. initialize repository git init
  2. add files to be ignored by git in .gitignore
.venv
*.pyc
db.sqlite3

3. commit and push changes (in test repositorygit@github.com:repo.git)

git add *
git commit -m "initial"
git remote add origin git@github.com:repo.git
git push -u origin master

Deploy

Let’s assume we have a test machine accessible on 159.223.235.94 with django user. We are going to access remote machine and and pull the code from the git repository. If we use the same ssh key pair to access git repository and the remote machine we may load the key pair to ssh client which would be used to pull changes from the remote git repo.

# forward agent will let you use your local keys on remote machine
ssh django@159.223.235.96 -o ForwardAgent=yes
git clone git@github.com:repo.git
cd repo

Create virtual environment as well as required dependencies on the remote machine:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Set ALLOWED_HOSTS environment variable and start Django:

ALLOWED_HOSTS="159.223.235.96" python manage.py runserver 0.0.0.0:80

Let’s curl:

> curl http://159.223.235.96/hello
> Hello, World!

So we’ve successfully deployed an app in the cloud which is accessible from an outside world using very basic tools.

--

--

No responses yet