For the first python web app I created, most of the tricky parts and learning came with trying to deploy it to a server, which result in lots of trial and error, and writing down a huge long list of instructions. Since that time I’ve learned using Ansible, Docker and Github actions which all have different aspects of automating software deployment .
PART 1: Deployment Choices (this post)
PART 2: Setting Up Django Cheat Sheet
PART 3: Deployment using Ansible and Pyenv (this post)
PART 4: Alternative deployment with Docker (future post)
For this project, I have a Django python web app which I want to deploy to a Ubuntu server. I started making some plans but then realised there is quite a lot of overlap and quite a lot of different ways to do the same thing… so here are the options I considered and why:
Choice 1: Deploy from local machine or from Github actions (with Ansible)
Ansible (or a similar piece of software) is essential for deploying to a VPS (and so it’s easy to change servers, or providers if needed). For a standard VPS there’s no API like for cloud microservices (e.g. AWS) or platforms (e.g. Heroku), so there needs to be a way to connect to the VPS over SSH, setup things like firewalls, install python, software etc. There is a choice to make where the Ansible playbooks are run from: my local machine, or github actions. Having used github actions (which creates a temporary machine) for deploying to microservices, I like this method, as deployment can be achieved on a single push to the github repo. That would mean Ansible would be part of the repo and configured to run on github actions. The problem with this is that for a free account github actions has a limit on the number of minutes to run actions per month, and I’m not keen on having a paid account at this stage. It also introduces more complexity when I’m also in the process of learning Docker (which we’ll come to soon!).
My choice: Deploy using Ansible from local machine. Django code in a separate repo to the Ansible code. This means deployment will need two manual steps on the local machine: push changes to github repo, and run the Ansible playbook to deploy the changes to the VPS.
Choice 2: Run the Django app in Docker or just in a python venv
Running the Django app in a venv on the server would work fine, and could be accomplished simply with Ansible, however the benefit of using docker is that the entire system is containerized and predictable (not just the python environment) and it can be replicated exactly on a test machine by running the same docker container. In addition, one of the reasons for doing this project was to learn docker!
My choice: Try both options! There’ a tutorial to start me off here for docker
The next post will be to show how I setup a basic Django app.