Question

Can someone give specific steps how to implement hosting a Django based website by using Amazon EC2 hosting service?

Is that possible?

My website source code can be found here

I goolged and found this article

But before doing anything I just want to get some basic ideas about Amazon EC2 hosting.

Was it helpful?

Solution

It certainly is possible, but it sounds like EC2 is not the best option for you. For examples of people doing it, google for "django ec2" and check out the first and second links. links like this or this (edited because this question is now the top google result for that query!)

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. What stage of your project are you at? Just researching, or have you gotten stuck on a particular problem with either django or ec2?

UPDATE: Ok, I think what you are confused about is 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, the 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

OTHER TIPS

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).

Assume you are using Apache server on your instance, the official instruction on Django site works better than a lot of blog posts. Here is what I copied from the link: https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/modwsgi/

Edit and add the following code to /etc/apache2/apache2.conf would work out.

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top