Single-file deploy Django to a generic Ubuntu host

Full-automatic, copy-paste Nginx/Gunicorn/Let’s Encrypt HTTPS set up in minutes

Fresh Django project deployment always looks a bit cumbersome. Bunch of script files, configurations, and container infrastructure setup time could take more time than an actual MVP project. This article describes a way to deploy to a generic Ubuntu host, just with a single tool — Ansible, and the one-file deployment script.

The deployment could be really handy to get into production just after startproject/startapp commands, and before hiring a first member of your DevOps team.

If you already use Pipenv and Django-Environ just grab deploy.yml put it into your project and go down to “Let’s deploy” section

Here is the quick tutorial, how to get from ./ start project to a codebase that is ready to deploy.

The only tool that we need on the host is the Pipenv. If you already have python set up on your machine, it’s likely that you already have pip tool installed. So just run

Or you could install pipenv tool globally with your system package manager. Installation details could be found in the Pipenv documentation.

Let’s create a directory for our project and go into it

It’s time to install project dependencies. Pipenv creates a separate virtual environment to install packages into and Pipfile to keep the project dependencies list.

Start the Django project

Please note the last dot. It means we are creating a project within the current directory. The pipenv run command activates the virtual environment with installed packages and executes the command. When you need to run the project ./ command, you may activate the virtual environment with pipenv shell or run the command directly as pipenv run ./ ...

At this time we have following set up

For deploying, let’s modify config/ to use django-environ instead of hardcoded values.

You may follow django-enviton tutorial, or get sample code from this article. Set up DEBUG,SECRET_KEY, ALLOWED_HOSTS andDATABASES configuration parameters:

In addition, set up folders for static/ files and uploaded media/

It’s time to deploy. At this moment you should get the domain, have running a Linux host in a cloud, and proper DNS configuration. Check that you have the ssh access to your server, with your domain name

You may also create a user with sudo rights and use it instead root

We're going to use Ansible to deploy our project, let’s install it with pipenv as a development dependency:

And run the deployment. During the first time, you will be asked for an email for Let’s Encrypt certificate registration.

Use a proper domain name instead of and custom user, if you have it, instead of root. Please note the comma after the domain name. It’s required and tells Ansible that we have provided a domain name, instead of an inventory file name.

  • All code from the local folder was synchronized to /srv/ on your server
  • Postgresql / Nginx / Gunicorn tools installed
  • Let’s encrypt certificated requested and installed — ready to serve to you!

Please read the troubleshooting guide at the repository Feel free to report bugs or ask questions in issues!

  • Do you have ready to use project template?

Sure, just start with it:

  • What about celery and channels?

That’s our issue #1 and #2

  • Is it suitable for production?

For a small-sized project, you do the best that can be done in a few minutes. In addition, you need to go through the Django deployment checklist, hardened your server at least with fail2ban installation and set up backups.

Actually, start with setting up backups first, please.

  • Why no docker?

That would be one of the next articles! Docker adds a lot of complexity to a whole setup, but it can’t be avoided for complex projects. Subscribe to get notified on how to use Helm to set up k8s cluster for a Django project.

  • Why no poetry?

Many poetry design decisions would make this tutorial longer. Besides that currently pre-released 1.0 version is broken, whereas previous stable 0.12 has major show stoppers like #522

Reusable workflow library #django #python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store