How to setup Django app on EC2 instance (AWS)

Hosting django on AWS ec2

Recently I hosted Django project on EC2 instance of AWS. I find the process very difficult due to multiple reasons which we will see in article.

Hosting on PythonAnyWhere is cake walk as compare to hosting on EC2 instance. And pricing is also almost same.

Since I have gone through the process, I am sharing the step by step approach to host Django application on EC2 instance.


Steps to host Django app on EC2:


Create the account and get the .pem file.

Place the file in on your system and change the permission on that file to 644.  chmod 644 aws_key.pem

Login to system from terminal.  ssh -i aws_key.pem

Now you are into your home directory. Running pwd  will give you  /home/ec2-user .

Getting code:

If you haven’t pushed your code to github or bitbucket, please do it. In my opinion using github or bitbucket is the best way to transfer the data from your system to aws. Using git is highly recommended in any coding project. Please read the importance of version controlling.

Clone your code to aws server.

git clone

If git is not installed, install it.  sudo yum install git


Creating virtual environment:

I am assuming that you were developing the project in virtual environment on your local system. If not, it is the recommended way.

Virtualenv is already installed. If you were using python2.x during development then you are good to go. If you were using python3.x then you need to install the python3 on ec2 instance.

sudo yum install python34

Now create a virtual environment using python3. Locate the python3 executable.  which python3 .

virtualenv -p /usr/bin/python3 env_name

Upgrade the pip version as well to avoid any warnings.  pip install --upgrade pip

Now install the dependencies using pip  and requirement.txt file .

pip install -r requirement.txt

Now during the installation of packages you might get multiple errors. Resolve them one by one with the help of stackoverflow or other online resources. Based on the errors I received, I made a list of packages that needs to be pre-installed in the system. So before installing dependencies in above step, install these packages.

sudo yum groupinstall "Development Tools" "Development Libraries" Install the development tools and libraries as well.

sudo yum groupinstall "Development Tools" "Development Libraries"

This was the hardest part I think. Searching each error over the net and installing suitable package.


Creating database:

Mysql has been installed in previous step. Now if you try to connect to mysql you might get an error. Something like ‘couldn’t connect to mysql on socket …’. If you are getting this error then mysql service is not running. Start the mysql server.

Now set the root user password.

mysqladmin -u root password [your_new_pwd]

You may create a new database. Check the name of database from your production settings file.

mysqladmin -u root -p create [your_new_db]

Login to MySQL and create a new user and assign all privileges to it from localhost.

Now if you select host , user  and password  from user  table of mysql  database, output would be something like below.

Now you can access the database using user user1  from localhost without password and with password from anywhere else.


Now our environment setup is complete. We will continue the Django app setup in next part.

How to setup Django app on EC2 instance (AWS) – Part 2.



(Visited 966 times, 1 visits today)

You must read this :

1 thought on “How to setup Django app on EC2 instance (AWS)”

Leave a Reply

Your email address will not be published. Required fields are marked *