I am a total newbie at Python, so I am writing this blog just to document my journey/effort into setting up a Django project on an EC2 instance!
I set up an EC2 instance with the latest publicly available Ubuntu machine image. For this project, I chose a t1.micro instance.
It is highly recommended that Python projects be set up in Virtual Environments. A Python VE is noting but a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. The reason this is recommended is because you may write multiple python apps and each app may end up using different versions of python modules and packages. If one of your Python app is using version 1.0 of a particular package and another app is using version 2.0 of the same package and both these packages are installed in the same environment; one of the apps will break due to a conflict in package versions.
However, in a virtual environment , each package’s version is isolated from other versions and hence there cannot be such a clash.
Since each app will have its own virtual environment , it is a good idea to have all your virtual environments in a hidden directory. For this I followed the official Python docs’ recommendation and created my first virtual environment as under:
python3 -m venv ~/.virtualenvs/testenv
Next, the virtual environment needs to be activated by running this command:
source ~/.virtualenvs/testenv/bin/activate
The above command “activates” the virtual environment and your shell prompt should now change to ‘testenv‘ . You are now working in a python virtual environment.
The next step is to actually install Django framework within this VE by simply running:
python3 -m pip install Django
Note that Django will get installed for THIS virtual environment and not globally! This is the whole point of creating a VE…django is now available as a local copy and if you create a new project down the road (months/years later), you will install a new copy/version of Django in a new virtual environment….without running into the danger of breaking any existing Django app/code!
Once Django is installed successfully, you can start your new Django project by running:
django-admin startproject mysite
This will install all the necessary files and create the dir structure needed by the Django app. However, this article is about running the Django app on an EC2 instance. You cannot simply run
python manage.py runserver
and expect to go to http://127.0.0.1:8000/ and see the “rocket launch” of an empty Django project! In order to run your project on an EC2 instance, you will need to modify the settings.py file under the ‘mysite’ directory:
sudo vi mysite/setting.py
Add the public IPV4 DNS of the EC2 on the ALLOWED HOSTS line:ALLOWED _HOSTS = ['ec252.XX.XX.XXX.compute1.amazonaws.com']
Your EC2 security group must allow INBOUND TCP traffic on port 8000 before you start the server. Once that is done, run the following command and visit your EC2 in a browser and if all went well, you should see the Django rocket launched!
python3 manage.py runserver ec2.xxxxx.compute-1.amazonaws.com:8000
1,484 total views, 1 views today