PROUDLY FROM INDIA

+91-75083-23736

How to Run a Django App on AWS EC2

Webtunix Blog

AWS (Amazon Web Services) is a secure platform for cloud services, database storage, content delivery and functionality of business.

Step 1: To create instance

1. First of all go to Amazon Web Services EC2- Simple Cloud Hosting, click on its link and create account for Amazon Cloud Service.
After login, go to console and select EC2 in compute.

Run Django on AWS

2. Then in EC2 dashboard, click on running instances.

Run Django on AWS

3. To create an instance,  go to create instance and then click launch instance.

Run Django on AWS

4. Firstly, choose an Amazon Machine Image (AMI) and then you have to select your system from the following options : Window, Linux and Ubuntu.

Run Django on AWS

5. After selecting the system,  2nd step is to choose an Instance Type.  Micro instance size is enough for a single website.

Run Django on AWS

6. Click next and after completing all steps, you need to generate a key pair. Go to side bar at left side of your screen and in network and security, click key pairs. This key pair should be kept safe as it is generated only once for each instance.

Run Django on AWS

Step 2: To connect instance


1. To connect instance with local PC, select the instance, then right click on it and connect generated window click.
.
After this you will get an SSH which is used to connect instance. Copy this SSH and run it on terminal.

Step 3: Setting up instance


1. We are connected to the instance now and need to install some programs. For installation type the following commands :
commands:
$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi
2. Then install Django using pip tool, which should also be installed first.
$ sudo apt-get install python-pip
$ sudo pip install django

3. For creating directory for our Django app.
It create a directory in home directory to work and link to it from site-root defined in apache's configuration
(/var/www/html by default, see /etc/apache2/sites-enabled/000-default.conf for the current value).
WSGIDaemonProcess django threads=5
WSGIScriptAlias / /var/www/html/django/wsgi.py
<Directory django>
WSGIProcessGroup django
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
$ mkdir ~/djangoapp
$ sudo ln -sT ~/djangoapp /var/www/html/djangoapp
To verify our operation is working, create a simple index.html file.
$ cd ~/djangoapp
$ echo "Hello World" > index.html
You should now see "Hello World" displayed if you navigate to (your instance public DNS)/flaskapp in your
browser.

You can also Check out:

How to Run a Flask App on AWS EC2?

To Run a simple Django app

1. Enable mod_wsgi
Follow the link /var/www/html/django/mysite/wsgi.py


2. Restart the webserver
Use this command to restart the server with new configuration.
$ sudo apachectl restart


3. Test configuration
If you navigate your browser to your EC2 instance's public DNS again, you should see the text returned by
hello_world function of our app, "Hello from Django!"
Our server is now running and ready to crunch data and if something isn't working, try checking the log file
in /var/log/apache2/error.log .