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 79.125.24.49 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
  • ec2-79-125-24-49.eu-west-1.compute.amazonaws.com 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 (http://code.google.com/p/django-reporting/)is 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 = 79.125.24.49 – I created an elastic IP and assigned it to my instance
  • public dns name =ec2-79-125-24-49.eu-west-1.compute.amazonaws.com
  • login = ssh -i philrocheEC2Europe.pem ubuntu@79.125.24.49 (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/philroche.pub ubuntu@79.125.24.49:/home/philroche/tmp
  • login again and authorize this key = ssh -i philrocheEC2Europe.pem ubuntu@79.125.24.49
  • authorize the philroche user with uploaded public key = cat/home/philroche/tmp/philroche.pub >> /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@79.125.24.49
  • 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/mod_wsgi.so
    • ln -s /usr/lib/apache2/modules/mod_wsgi.so-2.5 /usr/lib/apache2/modules/mod_wsgi.so
  • 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 79.125.24.49 djangoreporting
  • /etc/init.d/apache2 restart python2.5 manage.py 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-1.6.0.15/
    • 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 79.125.24.49 in my hosts file lets methe test the server by accessing http://djangoreporting/

Links of interest: