How to Deploy a Simple Django App Using AWS

Liz Tom

September 16th, 2015

So you've written your first Django app and now you want to show the world your awesome To Do List. If you like me, your first Django app was from the awesome Django tutorial on their site.

You may have heard of AWS. What exactly does this mean, and how does it pertain to getting your app out there. AWS is Amazon Web Services. They have many different products, but we're just going to focus on using one today:

Elastic Compute Cloud (EC2) - Scalable virtual private servers.

So you have your Django app and it runs beautifully locally. The goal is to reproduce everything but on Amazon's servers.

Note: There are many different ways to set up your servers, this is just one way. You can and should experiment to see what works best for you.

Application Server

First up we're going to need to spin up a server to host your application.

Let's go back, since the very first step would actually be to sign up for an AWS account. Please make sure to do that first.

Now that we're back on track, you'll want to log into your account and go to your management dashboard. Click on EC2 under compute.

Then click "Launch Instance". Now choose your operating system. I use Ubuntu because that's what we use at work. Basically, you should choose an operating system that is as close to the operating system that you use to develop in.

Step 2 has you choosing an instance type. Since this is a small app and I want to be in the free tier the t2.micro will do. When you have a production ready app to go, you can read up more on EC2 instance types here. Basically you can add more power to your EC2 instance as you move up.

Step 3: Click Next: Configure Instance Details

For a simple app we don't need to change anything on this page. One thing to note is the Purchasing option. There are three different types of EC2 Purchasing Options, Spot Instances, Reserved Instances and Dedicated Instances. See them but since we're still on the free tier, let's not worry about this for now.

Step 4: Click Next: Add Storage

You don't need to change anything here, but this is where you'd click Next: Tag Instance (Step 5).

You also don't need to change anything here, but if you're managing a lot of EC2 instances it's probably a good idea to to tag your instances.

Step 6: Click Next: Configure Security Group.

Under Type select HTTP and the rest should autofill. Otherwise you will spend hours wondering why Nginx hates you and doesn't want to work.

Finally, Click Launch.

A modal should have popped up prompting you to select an existing key pair or create a new key pair.

Unless you already have an exisiting key pair, select Create a new key pair and give it name.

You have to download this file and make sure to keep it somewhere safe and somewhere you will remember. You won't be able to download this file again, but you can always spin up another EC2 instance, and create a new key again.

Click Launch Instances!

You did it! You launched an EC2 instance!

Configuring your EC2 Instance

But I'm sorry to tell you that your journey is not over. You'll still need to configure your server with everything it needs to run your Django app.

Click View Instances.

This should bring you to a panel that shows you if your instance is running or not. You'll need to grab your Public IP address from here.

So do you remember that private key you downloaded? You'll be needing that for this step.

Open your terminal:

cd path/to/your/secret/key
chmod 400 your_key-pair_name.pem

chmod 400 your_key-pair_name.pem is to set the permissions on the key so only you can read it.

Now let's SSH to your instance.

ssh -i path/to/your/secret/key/your_key-pair_name.pem ubuntu@IP-ADDRESS

Since we're running Ubuntu and will be using apt, we need to make sure that apt is up to date:

sudo apt-get update

Then you need your webserver (nginx):

sudo apt-get install nginx

Since we installed Ubuntu 14.04, Nginx starts up automatically. You should be able to visit your public IP address and see a screen that says Welcome to nginx!

Great, nginx was downloaded correctly and is all booted up. Let's get your app on there!

Since this is a Django project, you'll need to install Django on your server.

sudo apt-get install python-pip
sudo pip install virtualenv
sudo pip install git

Pull your project down from github:

git clone my-git-hub-url

In your project's root directory make sure you have at a minimum a requirements.txt file with the following:

django
gunicorn

Side note: gunicorn is a Python WSGI HTTP Server for UNIX. You can find out more here.

Make a virtualenv and install your pip requirements using:

pip install -r requirements.txt

Now you should have django and gunicorn installed.

Since nginx starts automatically you'll want to shut it down.

sudo service nginx stop

Now you'll turn on gunicorn by running:

gunicorn app-name.wsgi

Now that gunicorn is up and running it's time to turn on nginx:

cd ~/etc/nginx
sudo vi nginx.conf

Within the http block either at the top or the bottom, you'll want to insert this block:

server {

       listen 80;
       server_name public-ip-address;

       access_log /var/log/nginx-access.log;
       error_log /var/log/nginx-error.log;
       root /home/ubuntu/project-root;

       location / {
           proxy_pass http://127.0.0.1:8000;
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
   }

Now start up nginx again:

sudo service nginx start

Go to your public IP address and you should see your lovely app on the Internet.

The End

Congratulations! You did it. You just deployed your awesome Django app using AWS. Do a little dance, pat yourself on back and feel good about what you just accomplished!

But, one note, as soon as you close your connection and terminate gunicorn, your app will no longer be running. You'll need to set up something like Upstart to keep your app running all the time.

Hope you had fun!

 

About the author

Liz Tom is a Creative Technologist at iStrategyLabs in Washington D.C. Liz’s passion for full stack development and digital media makes her a natural fit at ISL. Before joining iStrategyLabs, she worked in the film industry doing everything from mopping blood off of floors to managing budgets. When she’s not in the office, you can find Liz attempting parkour and going to check out interactive displays at museums.