Simple Django deployment part one: infrastructure

Sun 26 April 2020, by Matthew Segal
Category: Django

In order to deploy our Django app, we need a somewhere to run it: we need a server. In this section we'll be setting up our server in "the cloud". Doing this can be fiddly and annoying, especially if you're new, so we want to get it right first before we involve our Django app. By the end of this section we will learn how to:

  • Install ssh and scp on Windows
  • Create a SSH key
  • Set up our cloud web server
  • Learn how to access our server, upload files
  • Test our setup

Installing our tools

We'll need some tools to access the server:

  • the "bash" shell for scripting
  • the "ssh" tool for logging into our web server
  • the "scp" tool to transfer file to our server

We need to install these tools on Windows and the fastest and easiest way I know of is to just install Git. We won't be using Git, just some of the tools that get installed with it. You can learn Git some other time. If you're using a Mac or Linux you can skip this step and open up a terminal window.

Creating a SSH key

We'll be using an "SSH key" a way to authenticate ourselves when we log in to the server with ssh. We need this key for when we create our server, so we're doing this bit first. It's possible to just use a username / password when logging in via SSH, but creating a key is more convenient in the long run. In particular, using a key means you don't have to type in a password every time you want to access the server.

In this video, we'll be creating an SSH key using the "ssh-keygen" command in bash:

ssh-keygen -C "[email protected]"

Now that we've created our key, we're ready to use it to log into web servers. To recap: the SSH key we just created has two parts, a public key ( and a private key (id_rsa). Both keys will be stored in ~/.ssh by convention. You can read your public key like this:

cat ~/.ssh/

A public key is like your "username" when logging in - it's public information and it's OK to share it. It looks like this:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCVtWYN+uDbdG6RtmU8vcaj1cxYM0kK6565LFa
u0Qr1 [email protected]

It's usually all just long one line of text. The "ssh-rsa" part means it's a key for SSH and uses RSA encryption. The "AAAAB3N...u0Qr1" part is your actual key, it uniquely identifies your keypair. The "[email protected]" part is just a comment, it can be anything, but by convention it's your email.

A private key is like your password - do not share it with anyone. Private keys look like this:


... I'm not actually using this one so it's OK to share.

Creating the server

You've probably heard the word "server" used to refer to a dozen different things, so let me be specific. Our server will be a Linux virtual machine (VM), which we are going to rent from DigitalOcean, a cloud hosting company. DigitalOcean will run our VM in one of their datacenters, which is a big building that is full of computers. For our purposes, this VM is a stand-alone computer that is for our private usage, with a static IP address which we can use to find it online.

The first thing you need to do is create an account with DigitalOcean. The only reason I've chosen this company is because they have a nice web user interface and I already use them. Other than that, there's no reason you couldn't also use Linode, AWS, Google Cloud or Azure to do the exact same thing. They all provide Linux web servers for rent.

Once you've created your account, you can follow this video for the rest of the setup. I'm not sure exactly when they're going to ask you to put your credit card details it, but have a credit card ready.

By the end of this video we'll have created our server and we'll have an IP address, which we can use to log into the server.

Setting up the server

The main tools that we'll need to run our Django app on this server are Python 3 and pip, the Python package manager. You'll find that Python 3 is already installed on our Ubuntu server, but we need to install pip. We'll be using the apt package manager to download and install pip.

By the way, it turns out that "LTS" stands for Long Term Support and refers to Ubuntu's policy of how they plan to provide patches in the future (not super relevant to this guide).

Uploading files and troubleshooting HTTP

So now we know how to create a server, log in with ssh and install the software we need to run Django. Next I will show you how to upload files to the server with scp. In addition, I'll show you how to run a quick and easy HTTP web server, which can be useful for debugging later. You will need Python 3 installed on your desktop for this step.

If you want to take a 40 minute side-quest I recommend checking out Brian Will's "The Internet" videos to learn more about what HTTP, TCP, and ports are: part 1, part 2, part 3, part 4.

I'll recap on the HTTP troubleshooting, so you understand why we just did that. The main purpose was to get us ready to troubleshoot our Django app later.

We ran a program (http.server) on our Linux VM using Python. That program was listening for HTTP requests on port 80, and when someone sends it a GET request, it responds with the contents of the index.html file that we wrote and uploaded.

If we use our web browser and visit our web server's IP address ( in my case), our web browser will send an HTTP GET request to our server on port 80. Sometimes this works and gets the the HTML, but under some circumstances it will fail. The point of our troubleshooting is to figure out why it is failing. It could be that:

  • Our computer is not connected to the internet
  • Our web server is turned off
  • We cannot access our web server over the internet
  • A firewall on our web server is blocking port 80
  • etc. etc. etc

One way we can figure out what's going on is to log into the VM using ssh and make a HTTP GET request using curl. If the curl request works, then we know that the our HTTP server program is working and serving requests inside the VM, and the problem is something between our computer and the VM. Once we've narrowed down the exact problem, then we can figure out how to fix it.

troubleshooting http

This style of troubleshooting will become useful when we start setting up our Django app on the server.

Next steps

Now our server is ready to serve Django and we know how to troubleshoot HTTP connections. Next we will prepare and test Django locally.

If you have any feedback or questions email me at [email protected]