How to Install django on aws ec2(Amazon Linux2)

This tutorial explains How to Install Django on AWS EC2( Amazon Linux 2)

Django

Django is a python-based opensource framework to write a web application.If  you want to install Django on AWS EC2( Amazon Linux) there are several methods to install it .We will use most popular and easy method to install Django on AWS EC2 by installing it using PIP.

Python Version recommended for Django

Python3 is recommended for Django.Last Django version supported for Python 2.7 is Django 1.11. Support of Python 2.7 and Django 1.11 ends in 2020. So, in this tutorial we will install Python 3 for Django.

Nothing goes wrong if you install old version of Django, but if you don’t install the latest version you will not be able to take advantage of improvements and optimizations in newer python release.

Also Read: How to install anaconda on Amazon Linux(AWS EC2)

&& How to install PostgreSQL from source

Install Django Using PIP

Step 1 – Update your Amazon Linux 2 (EC2 Instance)

                  $ yum update -y

Step 2 – Install the Epel Repository to download and install PIP.

                 $ sudo amazon-linux-extras install epel -y

Step 3 – Install Django using PIP

    3.1) First, Install PIP  by using the following command

                 $ sudo yum install python3-pip -y

   3.2) Now install Django using PIP

                  $ pip3 install django

Step 4- Check the django version to make sure Django is Installed

               $ pip3 freeze

As a output you can see the Django Version (Django==3.0.6)

 asgiref==3.2.7
Django==3.0.6
pytz==2020.1
sqlparse==0.3.1

Install Django in Virtual Environment

Virtual Environment comes in to picture when you have a multiple projects and these different projects is compatible with different Django Versions.Virtual Env creates isolated environments with each running their own compatible version of packages.

So you can use this method if you have different projects with different Django version.

Step 1 -Install Virutualenv

                 $ sudo pip3 install virtualenv

Step 2 – Check Whereis command to see location of  virtualenv

                 $ whereis virtualenv

Output will be like

virtualenv: /usr/local/bin/virtualenv

Step 3 – Create a Project in Virtualenv

Go to home directory

                   $ cd ~

Create Virutalenv

                  $ virtualenv djangoenv

Or  use absolute path using output obtained from whereis command.

                  $ /usr/local/bin/virtualenv djangoenv

 The above command will create a Virtual Environment in ~/djangoenv/ .

Step 4 – Activate Virtual Environment.

                 $ source ~/djangoenv/bin/activate

  It will change your SSH Terminal as follows:

               (djangoenv) [[email protected]~]$

Step 5 – Install Django inside Virtual Environment.

                (djangoenv) [[email protected] ~]#  pip3 install – -upgrade Django==2.*

 Check Django Version

                  (djangoenv) [[email protected] ~]#  python -m django – -version

                   2.2.12

 Step 6 -Type Deactivate to exit

                 (djangoenv) [[email protected] ~]#  deactivate

Step 7 – You can also create a different virtualenv and install different version of python.

             $ /usr/local/bin/virtualenv djangoenv2

Activate Virtualenv  djangoenv2

                 $ source ~/djangoenv2/bin/activate

  It will change your SSH Terminal as follows:

               (djangoenv2) [[email protected]~]$

Install Django inside Virtual Env.

                (djangoenv2) [[email protected] ~]#  pip3 install – -upgrade Django

 Check Django Version

                  (djangoenv2) [[email protected] ~]#  python -m django – -version

                   3.0.6

 Type Deactivate to exit

                 (djangoenv2) [[email protected] ~]#  deactivate

Click to Tweet this!

I hope you enjoyed this tutorial and learned to Install Django on ec2 instance (Amazon Linux). If you think this is really helpful, please do share this with others as well. Please also share your valuable feedback, comment or any query in the comment box. I will really happy to resolve your all queries anytime.

Thank You

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

Leave a Reply