Category Archives: Django

Simple Django install on Amazon EC2

I set up an amazon ec2 instance this weekend and as I knew I’d bedoing it again I noted the steps performed. Hopefully they’ll be of use to someone.

Firstly you need to set up an EC2 account. I’d also recommend downloading ElasticFox(XPIinstaller) which makes it easier to manages instances, volumes andelastic IPs. Also install the EC2API command line tools so you can start and stop instances etc.

The following guide is what I did to set up an Ubuntu 9.10 instance with EBS volume attached and a basic django install running. Bear the following in mind when reading the guide:

  • References to are to my elastic IP address that I set up using Elasticfox so it will be different for you.
  • “philroche” is the username I use so I’d hope you’d be changing that.
  • pk-XXXXXXXXXX.pem is the private key from the x509 certificate that was generated when creating my account
  • cert-XXXXXXXXXX.pem is the certificate file from the x509certificate that was generated when creating my account
  • i-d820f7af is the instance ID assigned to my instance – this will be different for you
  • is the public DNS for the instance I created. This will also be different for you.
  • philrocheEC2Europe.pem is the private key for the generated key pair which was generated during account set up
  • The AMI I used (ami-97e4cfe3) has a user “ubuntu” already created that is used for initial login
  • djangoreporting ( a simple django app that I used to test the set up.

Seting up Ubuntu 9.10 django install on EC2

  • AMI id = ami-97e4cfe3
  • instance id = i-d820f7af
  • ip address = – I created an elastic IP and assigned it to my instance
  • public dns name
  • login = ssh -i philrocheEC2Europe.pem ubuntu@ (philrocheEC2Europe.pem is private key generated key pair)
  • enable root user = sudo passwd root
  • change to root user = su
  • add new user = adduser philroche
  • add philroche to sudoers list = visudo
  • add the following to file = philroche ALL=(ALL) NOPASSWD:ALLcheck
  • Disable Password-based Login = nano /etc/ssh/sshd_config
    • check for PasswordAuthentication no
  • check root login is disabled
    • PermitRootLogin no
    • AllowUsers philroche ubuntu
  • restart sshd to take affect = /etc/init.d/ssh restart
  • create .ssh directory for philroche user to store public key =mkdir /home/philroche/.ssh
  • generate key pair LOCALLY in your users’ .ssh directory = ssh-keygen -b 1024 -f philroche -t dsa
  • make temp directory to upload public key = mkdir /home/philroche/tmp
    • chmod 777 /home/philroche/tmp
  • upload public key from LOCAL .ssh directory = scp -i philrocheEC2Europe.pem /home/philroche/.ssh/ ubuntu@
  • login again and authorize this key = ssh -i philrocheEC2Europe.pem ubuntu@
  • authorize the philroche user with uploaded public key = cat/home/philroche/tmp/ >> /home/philroche/.ssh/authorized_keys
    • chown philroche:philroche /home/philroche/.ssh
    • chmod 700 /home/philroche/.ssh
    • chown philroche:philroche /home/philroche/.ssh/authorized_keys
    • chmod 600 /home/philroche/.ssh/authorized_keys
  • Delete the tmp directory = rm -rf /home/philroche/tmp
  • Now login as new user (philroche) = ssh philroche@
  • Install require software = su
    • apt-get update
    • apt-get install apache2 python2.5 mysql-server mysql-client libapache2-mod-wsgi python-mysqldb python-setuptools subversion
    • NOTE – You will be prompted for mysql root password
  • enable the modules
    • a2enmod rewrite
    • a2enmod wsgi
  • make sure wsgi is using python2.5
    • rm /usr/lib/apache2/modules/
    • ln -s /usr/lib/apache2/modules/ /usr/lib/apache2/modules/
  • add virtual hosts by name support to apache
    • nano /etc/apache2/apache2.conf
    • make sure ‘NameVirtualHost *’ is present
  • install django = easy_install-2.5 django
  • Setting up sample app
    • nano /etc/apache2/sites-available/djangoreporting
    • a2ensite djangoreporting
    • mv /home/philroche/Sites/djangoreporting/reporting /usr/lib/python2.5/site-packages
    • echo ‘/home/philroche/Sites/djangoreporting’ >> /usr/lib/python2.5/site-packages/people_example.pth
    • mysql –user=”root” –password=”%Your root password%”
      • create database djangoreporting;
      • CREATE USER ‘djangoreporting’@’localhost’ IDENTIFIED BY ‘djangoreporting’;
      • GRANT ALL ON djangoreporting.* TO ‘djangoreporting’@’localhost’;
      • exit
  • LOCALLY nano /etc/hosts – add djangoreporting
  • /etc/init.d/apache2 restart python2.5 syncdb –noinput
  • To manage your instance
    • export EC2_HOME=$HOME/Applications/ec2-api-tools-1.3-46266/
    • export PATH=$PATH:$EC2_HOME/bin
    • export JAVA_HOME=/usr/lib/jvm/java-6-sun-
    • exportEC2_PRIVATE_KEY=/home/philroche/EC2InstanceSetUp/pk-XXXXXXXXXX.pem
    • exportEC2_CERT=/home/philroche/EC2InstanceSetUp/cert-XXXXXXXXXX.pem
    • sh ec2-describe-instances –region eu-west-1
    • sh ec2-stop-instances –region eu-west-1 i-d820f7af
    • sh ec2-start-instances –region eu-west-1 i-d820f7af
    • NOTE – when stoppped and restarted you have to re-associate the IP address

Mapping “djangoreporting” to in my hosts file lets methe test the server by accessing http://djangoreporting/

Links of interest:

Django project dependency management and deployment

I’m not our server admin here at Ticket-Text but the whole issue of project dependency management and deployment does interest me. As such I’ve been looking at ways to help and have compiled the following list of what I have found most useful. I admit I have not read all of these yet but Fabric and virtualenv do definitely seem to be standard now for any django project and they could save alot of work. I’ll let you know how we get on.

Some Djangocon 2009 videos that might be of interest (I have the videos in mp3 format if anybody wants them). The slides are also available @

If you’re looking for a simpler way to deploy SVN changesets check out my python script SVNArchiveByChangeset which still works a treat for packaging SVN changesets as a zip or tar file.