Deploying Django on Amazon EC2 Server
Hello. So you too have discovered the djangofied goodness that is 'django'. Hopefully you feel empowered with the knowledge that you can develop seriously great web applications in a relatively timely manner. But these applications are only really useful if they are available online.
I'll guide you through the steps I take each time I am setting up a server on the Amazon Web Services(AWS). In particular, we will be running our django app on an Amazon EC2 instance with Ubuntu Server 12.10 running as the Operating System. If you are not yet comfortable using AWS, take a quick look at my 'Getting Cosy with AWS' guide. This guide will get you up and running on an EC2 instance fairly quickly, but before you open up your server to the world, I would highly recommend ready up on server security, just to make sure your server stays healthy. With that said, lets get our inner geek out and get started...
Getting the EC2 instance running
If you haven't already, head over to AWS and get yourself an account. It only takes a couple of minutes and is free. Once you have logged in, open the 'AWS Management Console':
This is your view into AWS. Here you can do pretty much everything from starting up servers, to cloud storage, to routing high numbers of servers together. It truly is amazing what can be done with AWS, so I would highly recommend reading up on exactly what can be done with it.
We are going to launch a new EC2 instance. So click on EC2 from the Management Console, then select 'Launch Instance'. Select the classic wizard and find Ubuntu Server 12.04 LTS.
On the next page make sure that 'micro instance' is selected. Using the micro instance is fine for getting a simple application going and is free if you are following amazons free tier usage. Continue with everything else left at default until you get to get to the 'Key Pair' page:
Enter a name for the key (e.g. nickpoletkey) then 'create and download' the key to somewhere you will remember. You will be using this quite a lot for connecting to the server.
Continue through the firewall settings with the defaults, and click launch to actually launch the new instance.
While the instance is starting up (should take a minute or two), lets change the permissions of the key we downloaded from amazon. When we connect to the server, if the key does not have the correct file permissions, our connection will be rejected. Open up a terminal and run:
chmod 400 myKey.pem
So lets connect to the server and get everything we need installed. Run the following code from you terminal, replacing 'PUBLIC_DNS' with your own servers Public DNS location.
ssh -i myKey.pem ubuntu@PUBLIC_DNS
Installing everything we need
Now that we are logged in, lets update the systems repository and upgrade everything that needs it.
sudo apt-get update
sudo apt-get upgrade
Now install Apache and mod_wsgi. Apache is used as the webserver to handle requests from the other computers. The requests are then piped through to our django app using mod_wsgi.
sudo apt-get install apache2 libapache2-mod-wsgi
To make it easy to install various python libraries, including django, lets install pip.
sudo apt-get install python-pip
Ok, so lets now install Django. (If you are wondering why we haven't installed python, its because python is already installed by default on a standard ubuntu installation.)
sudo pip install django
Now we'll install mysql for our database. Django can handle many database servers, including postgreSQL and sqllite, but there are many resources around the net on how to install these on Ubuntu. I typically use mysql for web development, so I will leave the installation of other db servers up to you. We will also install the libraries needed for python to communicate to mysql as well.
sudo apt-get install mysql-server python-mysqldb
“Django is now installed and has everything its needs to run on the server. Next we need to create a folder that will be used to store the project and link up mod_wsgi to the project.”
Create a folder in the 'home' folder, which should be 'ubuntu' since we logged as the user 'ubuntu'. I tend to call this folder 'srv' which will contain the site-specific data (your django project).
To keep this guide as simple as possible, I am going to create a new django project (called 'testproject') instead of importing an existing project onto the server. I will cover this in a later guide, but if you have some server experience, you should find it relatively easy to move a project into the 'srv' folder, using git, sftp, ssh...
django-admin.py startproject testproject
Now we will edit the apache config file to use our wsgi file, which is generated by django-admin.py when we created the project.
sudo nano /etc/apache2/httpd.conf
Paste the following into the httpd.conf file (make sure to change any directories if you are not strictly following the guide)
WSGIScriptAlias / /home/ubuntu/srv/testproject/testproject/wsgi.py
Allow from all
Save the file (ctrl+o) and exit. Now we need to restart Apache:
sudo service apache2 restart
Now if you view the servers Public DNS in your browser you should see the default django page telling
you that 'it worked'. If you do not see this page, then make sure you have
allowed port 80 through on the AWS Security Group that the instance has been
assigned. If that doesn't work then see below.
PLEASE NOTE: if you installed ubuntu 12.10 or greater on the EC2 server, then you will need to do one more step before apache can load the config file we made above (httpd.conf). Lets edit the apache2.conf file (sudo nano /etc/apache2/apache2.conf) and add this to the end of the file:
Serving static files with Apache
sudo mkdir static
sudo mkdir media
Lets change the permissions of these folders to allow Apache to read and write to them.
sudo chown www-data static/
sudo chown www-data media/
posted on: April 2, 2013, 2:48 p.m.