Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
hosting ec2 aws dns   0   19037
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   19037
0 comments on 'Using A Custom Domain For Django App Hosted On Aws Ec2'
Login to comment


Related Articles:
How to host Django application on DigitalOcean server using Gunicorn, Supervisor, and NGINX
hosting Django application on DigitalOcean Server with custom Domain. Using WSGI server to host the Django application. Hosting Django application with Gunicorn, Supervisor, NGINX. Service static and media files of Django application using NGINX. Using Godaddy Domain to server traffic of Django application. Production deployment of Django applications....
Preventing cross-site scripting attack on your Django website
XSS attack in Django, preventing cross-site scripting attack in Django website, avoid XSS in Django application, Enabling SECURE_BROWSER_XSS_FILTER in Django website, blocking cross-site scripting attach on Django website...
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...
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....
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap