Weekly letter pythonprogramming.email
Hi,
To get the curated list of awesome python articles from all over the Internet, please subscribe with pythonprogramming.email. This is specifically suitable for beginners.
Advertise with us
hosting ec2 aws dns   0   2252
Using a custom domain for Django app hosted on AWS EC2


We already know how to host a Django app for free on Amazon (AWS) EC2 instance with Gunicorn and Nginx. But we were accessing the application using public IP. IP addresses are hard to remember and are not user-friendly. 

Domain names are easy to remember and gives a unique identity to your web application or website. In this article, we will see how to use a custom domain purchased from GoDaddy to access our Django web application.


The public IP address assigned to our EC2 instance can be changed if we restart the instance. We need to have an IP address that does not change every time our EC2 instance restarts. Elastic IP comes to rescue here.



Associating an Elastic IP to EC2 instance:

In the left-hand side menu of your EC2 instance dashboard, click on Elastic IP link.

Click on the 'Allocate Elastic IP Address' button. 

elastic ip to ec2 instance


Check the radio button 'Amazon's pool of IPv4 addresses' on the next screen and proceed. You will be assigned a new Elastic IP.

Now select this IP address and associate this with your running EC2 instance. 

Once this IP address is associated with your EC2 instance, this will your public IP. You can access your application on this IP address now. (Refer this article to learn how to access your Django application using public IP).

Now we have a static IP address associated with our EC2 instance which will not change between instance restarts.



Using a custom domain to access the Django application:

First, go to your domain registrar's website and manage your DNS settings.

In the A record, update the Elastic IP obtained previously.

a record dns


Now go to your Nginx sites-available folder and in the respective file, update the server block.

Replace the YOUR-PUBLIC-IP we used with the domain name.


server {
    listen 80;
    server_name YOUR-PUBLIC-IP;

    # to avoid any error while fetching fevicon
    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/ubuntu/helloworld;
    }

    location / {
        include proxy_params;
        # communicate via socket file created by Gunicorn
        proxy_pass http://unix:/home/ubuntu/helloworld/helloworld.sock;
    }
}


The third line will become

server_name example.com


Reload the Nginx server using the command

sudo service nginx reload



Update the settings.py file:

In your Django project's settings.py file, update the allowed hosts variable. In addition to public IP, add the domain name in the list.

ALLOWED_HOSTS = ["3.12.45.222", "example.com"]


Wait for 10-20 minutes to propagate the A record changes.

Go to your browser and access your Django app using the domain name.

accessing django app using domain



Host your Django App on PythonAnyWhere without any hassle.

hosting ec2 aws dns   0   2252

Related Articles:
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,...
Automatically updating Django website hosted on PythonAnyWhere server with every git push
How to automate the process of updating website hosted on python any where server everytime you commit and push code to git repository....
Python Tip 1: Accessing localhost Django webserver over the Internet
how to generate public URL to expose your local Django webserver over the Internet. How to let everyone access your Django project running on localhost....
How to add Favicon to Django websites
Adding favicon to Django website, SEO of Django blog, Branding of Django website, How to add a favicon to Django website, Adding shortcut icon, website icon, tab icon, URL icon, or bookmark icon to Django website and blogs...

0 thoughts on 'Using A Custom Domain For Django App Hosted On Aws Ec2'
Leave a comment:


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

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap