Deploying your Django project for the first time can seem like a huge task, and a blocking one in addition to that. You can’t move forward until the site is in production. The pressure is on.
However, there’s one trick to make deployment way less scary, and way less of a big deal.
Just deploy as early as possible. It doesn’t even need to be a publicly accessible location. Just an environment which is distinct from your development environment. Somewhere, where you can try your deployment procedures from scratch every now and then.
If this sounds painful and tedious, this article is for you. Deployment doesn’t need to be a frustrating and task which costs you days to get right.
A Safe Environment
There’s the perfect tool, to create safe environments without much effort - it’s Vagrant.
“Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.”
Vagrant makes it possible to create a virtual machine with a single command, configure it with code, and completely clean up the environment (and recreate it from scratch) whenever you wish.
It makes for the perfect environment to prototype your deployment approaches. A completely safe sandbox to try to deploy your application and find out what’s missing and what can be done better.
Your Goal
The idea behind deploying early, and doing so in an environment which is meant to be destroyed eventually, is to work on your processes. You want to make deployment really easy, almost boring.
This is done by iterating and learning as you go. In the beginning, you’ll be working on making notes for your future-self. Jotting down notes and tweaking your webapp project to make it more deploy-friendly. Eventually, you’ll start adding automation to your written documentation, to make each time you recreate your deployment environment easier and less intimidating.
If anything goes wrong or if you don’t know how to fix an issue - no biggie. You can simply completely destroy the VM, restart from scratch and arrive at a working state again.
Vagrant for your Django App - Step by Step
Here’s how you can create your first, safe Django deployment environment - right on your development machine. You’ll be able to get your feet wet here without the perceived responsibility and steep learning curve of working with a publicly-accessible remote environment.
First, you’ll need to setup tooling such as Vagrant or VirtualBox and create the right configs. You can read more about these steps in this article.
The First “Deployment”
Now that we have made our way into a brand-new environment, we can get a very first deployment version of your application up and running.
In the beginning, it’s best to aim for the easiest possible option - simply bring up a development server inside of the VM. Once that one works, you’ll be able to make it more complicated. Consider it a “Hello World!” of deployment.
Be sure to take notes of all steps which you are taking. Jotting down info on the packages you need to install or commands you type in this environment are useful clues to make it easier the next time around! Don’t forget, you’re working on your deployment processes, not building a “forever” deployment environment. Being conscious of those processes is the first step forward.
Don’t Have A Project? Start A New One!
As I don’t want to leave you hanging 1:1 with this (possibly) new environment, I’d like to share a series of commands which will help you to get up a brand-new Django project inside of the VM for testing.
Attention! If you already have a Django project in your VM’s code
directory, you won’t
need to create a new one. Everything else should be pretty much the same.
First, we’ll need to install Python 3, and add some tooling while we’re at it:
$ sudo install python3
$ sudo install python3-pip
$ pip3 install virtualenv
$ pip3 install pipenv
Now, we should exit the SSH session, and start a new one with vagrant ssh
. This will make sure
that we can use pipenv.
Here, we’ll create a new Pipfile and install the latest version of Django. These steps are loosely based no this article. Then, we’ll use that Django version to start a new project and run it inside of the VM:
$ pipenv --three
$ pipenv install django
$ pipenv shell
$ django-admin startproject testproject .
$ python manage.py runserver 0.0.0.0:8000
Notice how we run the development server on 0.0.0.0:8000
inside the VM? Because of the
port forwarding settings in the Vagrantfile, we can access this running application from outside
of the VM. Just open up your browser, and navigate to http://127.0.0.1:8081/.
All done! We have created a new Django VM, installed everything we need to run a Django project in it, and accessed the running application from the development machine. Shiny!
Cool. Now What?
This was only the first non-development (but still develop-ish) environment you’ve created. Congratulations! You are now ready to build on your hard work, and bring it closer to a production environment.
Don’t forget: your goal is to build reproducible processes, not to “get it working once and forget how”. These processes will help you to shape your application to be easy to deploy, but they’ll also get you in the habit of bringing your project into a deployed state.
Here is what you can try next:
- Install a reverse proxy like Nginx, and make it pass traffic to your app.
- Install a PostgreSQL database inside the VM, make your app use it.
- Use Gunicorn instead of the development server to run your code.
- Automate some setups steps of the Vagrant VM - using either Bash, Ansible or Fabric.
- Try Docker for some of your backing services.
I hope you’ll be able to learn about deployment way earlier than the dreaded “time to deploy”, and get comfortable with the task in a safe, fully controlled environment. If it seems like repeating yourself - don’t worry. Approaching deployment iteratively will make it less stressful and save you time down the line.
Wanna learn how to use Vagrant better, and develop your first deployment environment into a production-like environment for your Django app? Sign up below and get the first lesson directly in your inbox!