Django On Amazon Free Tier Installation

$ pip install django After installing necessary requirements from.txt file or running various commands manually, migrate the database, get static files ready, and test run the Django inbuilt.

Recently I hosted a Django project on EC2 instance of AWS. Hosting on PythonAnyWhere is a cakewalk as compared to hosting on EC2 instance. Since I have gone through the process, I am sharing the step by step approach to host Django application on EC2 instance.

1) my-keypair 2) Create new KeyPair Select a key pair if you have one already, or follow the prompts to create one. If you don't see the prompt or need to change your settings later, run eb init -i. Create an environment and deploy your application to it with eb create. /ebdjango$ eb create django-env. To upload your media files to S3 set: To allow django-admin collectstatic to automatically put your static files in your bucket set the following in your settings.py: If you want to use something like ManifestStaticFilesStorage then you must instead use: Your Amazon Web. Trials: These free tier offers are short term trial offers that start from the time of first usage begins. Once the trial period expires you simply pay standard, pay-as-you-go service rates (see each service page for full pricing details). The Amazon AWS Free Tier applies to participating services across our global regions. Your free usage.

Django On Amazon Free Tier Installation


Django On Amazon Free Tier Installation Download

We will be hosting the Django App on Free Tier EC2 Instance (t2.micro) which gives us the 750 hours of CPU usage, which should be more than enough for a small scale website.

Content:

- EC2 Free Tier account creation and SSH
- Hello World Django App
- Gunicorn
- Nginx


EC2 Free Tier account creation and SSH:

Amazon offers an EC2 instance with 750 hours of CPU usage for free for 12 months. You can start by creating an account. Instructions to create an account and starting the EC2 instance are pretty clear.

I chose Ubuntu 16.04 AMI.

Once your instance is up and running, download the private key file and place the same in ~/.ssh/ directory of your personal system.

You can SSH into your EC2 instance from your terminal using the below command.

ssh -i ~/.ssh/aws_key.pem [email protected]

Where ec2-13-99-202-128.us-east-2.compute.amazonaws.com is your instance's public DNS.

If you are not able to connect the instance using SSH, go to security groups and in the inbound tab, create a rule for SSH with port 22 enabled.


If the problem persists, follow connection guidelines.



Hello World Django App:

Python3 is already installed on this instance. We need to install pip and other dev tools. Run below commands


We will be working in a virtual environment hence we need to install the same.


Create a virtual environment and activate it. Now create a Django project.

Update the ALLOWED_HOSTS in settings.py file. For now, you can allow requests from all DNS or IPs but this is not safe. A much safer way would be to use the current public IP of the instance.


Start the development server.


Now when you type IP your instance's public IP address with 8000 port in the browser, you should be able to see the Default home page of hello world Django project.

Django

But before that, you need to add a custom TCP rule to allow incoming traffic on port 8000.


Gunicorn:

The development server of the Django application is good for testing but is highly insecure and can not handle more than one request at a time. In short for the production environment, we have to move away from the development server.

Stop the development server running on port 8000.

In the virtual environment, install Gunicorn if not installed already.

Start the Gunicorn using the below command.

This will start the Gunicorn on the same interface on which the development server was started. You can again test the application from the browser using public IP and port.


Nginx:

To integrate Gunicorn with Nginx make below changes.

Create a file /etc/nginx/sites-available/helloworld and paste the below content in it.


Now create the soft link of this file in the sites-enabled directory.


Django On Amazon Free Tier Installation Tool

Restart the Gunicorn using the below command.

Django On Amazon Free Tier Installation Tool


This will enable the Nginx and Gunicorn communication over a socket instead of a port, which is faster and more secure.


Now you can access the application by typing your public IP in the browser. Enjoy the free hosting for up to 12 months.

Django On Amazon Free Tier Installation Program


You can host your app for free (or for $5 with custom DNS) on Pythonanywhere.