How do I set up a Django website on Amazon EC2 hosting?

I have hosted my own django website on AWS EC2 t2.micro instance (AWS free tier). I used Django 1.9 for this project and MySQL as database. Make SSH tunnel to your instance and follow the steps:

  1. Install apache2 and libapache2-mod-wsgi on your instance:

    sudo apt-get install apache2 libapache2-mod-wsgi

  2. Install django on your instance :

    sudo pip install django

  3. Install mysql:

    sudo apt-get install mysqldb

    sudo pip install mysql-python

    sudo apt-get install libmysqlclient-dev

(if you don't have pip installed : sudo apt-get install python-pip)

  1. Configure mysql, for your django project. Import your django project to /var/www/html. (using git is the best way).

  2. Edit /etc/apache2/sites-available/000-default.conf:

    <VirtualHost *:80>
        Alias /static /path_to_my_static_folder
    
        <Directory /path_to_my_project_folder_containing_wsgi.py>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        WSGIDaemonProcess project_name python-path=/path_to/lib/python2.7/site-packages
    
        WSGIProcessGroup project_name
        WSGIScriptAlias / /path_to_wsgi.py
    </VirtualHost>
    
  3. Run migrate to sync db:

    python manage.py migrate

  4. Restart apache2:

    sudo service apache2 reload

I hope you have not hard-coded your template and static paths in settings.py, if yes then change it to dynamic path, or else edit it accordingly.

That's it! Visit your public IP or DNS to access your Django Website hosted on AWS EC2 instance.

Please comment below if you get any error.


Another option for you if you don't want to deal with setting up your server from scratch is to use BitNami Django Stack Amazon image. I'm a BitNami developer and worked on creating stacks for several Python applications. The BitNami Django Stack already includes MySQL 5.1, Apache 2.2 (with mod_wsgi) and Python with MarkDown, html5lib and python-openid installed. It also included django 1.3.

You will need to install Django Debug Toolbar, create the database and copy your files in /opt/bitnami/apps/django/django_projects and run the python manage.py commands. After that you will need to configure apache to server your project if you want to use in production (instead of the django server).

Before you try to deploy your application directly in the cloud you could use the native installers and test the deploy in your local machine.

We also have a cost estimation tool. This is just for getting a rough idea for a simple EC2/EBS setup, Amazon is not always as expensive as you can expect although it depends on a lot of factors. (Although per your comments it seems that you already took a look at the costs).


It certainly is possible, but it sounds like EC2 is not the best option for you. For examples of people doing it, see for example this or this.

In a very oversimplified sense, EC2 is just a server you can rent by the hour. You can have it run Windows or Linux, and then install Python and Django like you normally would. In fact there is probably an image that has Django preconfigured already.

You should understand that there are all different types of hosting out there. At one extreme, you can pay for your very own physical server, install your own operating system (like Windows or Linux), install your own Python, you own web server like Apache or IIS, your own Django libraries, your own database (like MySQL) etc, and then upload your web site to that. At the other extreme you can pay for an account on a shared hosting service, where someone else has done all the setup of the OS, Python, the web server, etc, and all you need to do is upload your web site code. EC2 is a lot closer to the first extreme, and is probably overkill for you. I think in your case you should be looking for a more managed solution.
I would check out this web page, which lists a bunch of different Django hosting companies: Django hosting