ec2 aws   0   7636
How to setup Django app on EC2 instance (AWS)

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.

Django - 1.9
Python - 3.x
Ec2 OS - Fedora

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

Locate the python3 executable. which python3.

Now create a virtual environment using 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"

sudo yum install python-devel mysql-server libffi-devel gcc python34-devel mysql-devl openssl-devel

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.

sudo chkconfig mysqld on 
sudo service mysqld start

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.

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' ;
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' ;
CREATE USER 'user'@'%' IDENTIFIED BY 'Password' ;

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

mysql> select  host, user , password from mysql.user;
| host           | user    | password                                  |
| localhost      | root    | *XXXXXXXXXXD8B98E17C30C0DC4586A4277A6811A |
| ip-172-1-6-41  | root    |                                           |
|      | root    |                                           |
| ::1            | root    |                                           |
| %              | user1   | *XXXXXXXC8C5A3E8A8961A931202967B08D63C4C8 |
| localhost      | user1   |                                           |
6 rows in set (0.00 sec)

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.

ec2 aws   0   7636

Related Articles:
How to setup Django app on EC2 instance (AWS) - Part 2
Step by step guide on hosting Django application on AWS ec2 instance. How to host the Django app on AWS ec2 instance from scratch....

0 thoughts on 'How To Setup Django App On Ec2 Instance (Aws)'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2017-2019 Python Circle   Contact Us   Advertise with Us