48

Can someone give specific steps on how to implement hosting of a Django-based website using Amazon's EC2 hosting service?

Is that possible?

My website source code can be found here.

I searched and found this article.

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

Boann
  • 48,794
  • 16
  • 117
  • 146
CodeYun
  • 749
  • 2
  • 12
  • 20
  • 1
    What questions do you have which are not answered if you read the article you found? – dar Oct 28 '09 at 17:50
  • After reading that article I feel EC2 is not a simple hosting like FTP my website to some folder, I have to follow lots of steps make it work. Just want to know from high level why i need to do those steps not simply upload my website and database to hosting machine. – CodeYun Oct 28 '09 at 18:13
  • Django code will NEVER be "just simply upload my website". It doesn't work like that. Any host at all will require extensive setup and configuration. Django is not PHP, sorry. – Paul McMillan Oct 29 '09 at 08:39
  • Any specific reason on trying to deploy on ec2 rather than using something like Elastic Beanstalk? Will be much easier with latter. – silent_grave May 29 '17 at 16:48

4 Answers4

32

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

Boann
  • 48,794
  • 16
  • 117
  • 146
Peter Recore
  • 14,037
  • 4
  • 42
  • 62
  • 5
    As an added point I would recommend they go with a managed solution if they don't know how to manage a server. Chances are they will spend less time pulling their hair out and the web will enjoy not having another bot in the farm. – Matt Oct 28 '09 at 17:47
  • 3
    I am a newbie in terms of django and python. I want to try to run an open source website on EC2 just because I believe EC2 is cheapest one I can use. I can run my django website on my local machine. Just want to publish it to Internet. – CodeYun Oct 28 '09 at 17:50
  • 4
    EC2 is far from the cheapest you can use. If you NEED a dedicated VPS, try slicehost.com. Otherwise, look at one of the hosted solutions. – Paul McMillan Oct 29 '09 at 08:40
  • 4
    Funny, because this page is now the #1 result for "django ec2." :) – Bialecki Feb 10 '11 at 21:54
  • Wouldn't the exception here be the free year of limited usage? Maybe it's great for Lean development, to test the product with a pen of users in the first year, and migrate later if necessary. – Adam Grant Dec 06 '13 at 18:45
  • ...especially when products like this exist: http://docs.docker.io/en/latest/installation/amazon/ – Adam Grant Dec 06 '13 at 19:03
  • @ajkochanowicz - using Docker is orders of magnitude more advanced than FTPing some files to a shared hosting site that supports django specifically. – Peter Recore Dec 16 '13 at 19:12
8

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

kaysa
  • 1,491
  • 13
  • 9
4

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.

Rishabh Agrahari
  • 3,447
  • 2
  • 21
  • 22
  • Could you shed some light on using gunicorn with nginx as a reverse proxy? It would be great if you could add those steps in your answer. That kind of a webserver cocktail is imperative for production-grade deployments. – Hassan Baig Sep 18 '17 at 18:29
  • Everything executed without errors. But visiting public IP showed nothing – Aseem Mar 26 '19 at 08:59
2

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>
Michael J. Gray
  • 9,784
  • 6
  • 38
  • 67
Yulun
  • 204
  • 3
  • 11