how-to hosting ec2 aws   0   285
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.

how-to hosting ec2 aws   0   285

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
How to upgrade to paid account on PythonAnyWhere
Which is the best server for hosting Django Apps. Best hosting provider for Django Apps. Cheapest Django Hosting. PythonAnyWhere Reviews. Django Hosting....

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:

© 2018-2019