SaltyCrane Blog — Notes on JavaScript and web development

Notes on Django and MySql on Amazon's EC2

Install Elasticfox

Install the Elasticfox Firefox Extension for Amazon EC2: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=609

Set up Amazon EC2 accounts and Elasticfox

Follow Arope's instructions for setting up Amazon EC2 accounts and Elasticfox. I used the alestic/ubuntu-8.04-hardy-base-20080628.manifest.xml machine image.

view standard apache page

In Elasticfox, right-click on your running instance and select "Copy Public DNS Name to clipboard". Then, paste that address in your browser. You should see Apache's "It works!" page.

ssh into instance

In Elasticfox, right-click on your running instance and select "SSH to Public Domain Name"

install stuff

Ubuntu Hardy has the following versions:

  • Apache 2.2.8
  • Mod_python 3.3.1
  • MySql 5.0.51
  • Django 0.96.1

On your remote instance, do the following.

# apt-get update
# apt-get install python-django
# apt-get install mysql-server
# apt-get install python-mysqldb
# apt-get install libapache2-mod-python

Update 2008-09-09: The Django mod_python documentation recommends using Apache's prefork MPM as opposed to the worker MPM. The worker MPM was installed by default on my Alestic Ubuntu image so I uninstalled it and replaced it with the prefork version.

# apt-get autoremove --purge apache2-mpm-worker
# apt-get install apache2-mpm-prefork

To see your current version of Apache, run the command: apache2 -V

create a django project
# cd /srv
# django-admin startproject mysite
configure django mod_python

See also Jeff Baier's article: Installing Django on an Ubuntu Linux Server for more information.

Edit /etc/apache2/httpd.conf and insert the following:

<location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonPath "['/srv'] + sys.path"
    PythonDebug On
</location>
restart the apache server
# /etc/init.d/apache2 restart

You should see Django's "It Worked!" page.

Set up a MySql database and user

Note, use the password you entered when installing MySql

# mysql -u root -p
Enter password: 

mysql> CREATE DATABASE django_db;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL ON django_db.* TO 'djangouser'@'localhost' IDENTIFIED BY 'yourpassword';
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye
Edit the Django database settings
Edit mysite/settings.py:
DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'django_db'             # Or path to database file if using sqlite3.
DATABASE_USER = 'djangouser'             # Not used with sqlite3.
DATABASE_PASSWORD = 'yourpassword'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
Do a 'syncdb' to create the database tables
# cd mysite
# python manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'sofeng'):    
E-mail address: [email protected]
Password: 
Password (again): 
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Loading 'initial_data' fixtures...
No fixtures found.
upload a mercurial django project

on the remote instance, install mercurial:

# apt-get install mercurial

on your local machine with the mercurial repo, run:

$ hg clone -e 'ssh -i /home/sofeng/.ec2-elasticfox/id_django-keypair.pem' yourproj ssh://[email protected]//srv/yourproj
where /home/sofeng/.ec2-elasticfox/id_django-keypair.pem is the private key associated with your instance and yourdns.compute-1.amazonaws.com is the public domain name associated with your instance.

back on the remote instance:

# cd /srv/mozblog
# hg update
# python manage.py syncdb
set up apache to serve static files
  • Create a link to the media files:
    # cd /var/www
    # ln -s /srv/mozblog/media site_media
    # ln -s /usr/share/python-support/python-django/django/contrib/admin/media/ admin_media
  • Edit /etc/apache2/httpd.conf:
    <location "/">
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE mozblog.settings
        PythonPath "['/srv'] + sys.path"
        PythonDebug On
    </location>
    <location "/site_media">
    
        SetHandler None
    </location>
    <location "/admin_media">
        SetHandler None
    </location>
Restart the apache server
# /etc/init.d/apache2 restart

Comments


#1 Igor Ganapolsky commented on :

I have django 1.1 running on ec2, and it is giving me a headache. I keep getting circular http redirects that I cannot track down. However, I use exactly the same django project on my own machine, and no problems there.


#2 Josh Gachnang commented on :

Great article! I'll be doing a similar one on my recent work on an Django EC2 load balanced cluster. The only thing I wanted to point out is there are many benefits to use mod_wsgi over mod_python, and is the recommended way to run a Django production server.

The only changes would be to install mod_wsgi instead and make a wsgi_handler.py file.

Details can be found here: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/