A Comprehensive Guide to AWS Hosting for Web Applications: From Deployment to Domain Linking

Ravindra Raj Kanitkar
Canadiv’s Technology and Design
10 min readSep 12, 2023

AWS Hosting Made Easy: The Ultimate Guide to Deploying Web Applications and Connecting Custom Domains

Hey everyone, it’s your friend Ravindra! Today, we’re going to talk about hosting our web applications on cloud services like AWS. This means we can serve our website to people all over the world, allowing them to use it while we handle the management. We’ll also learn how to connect our domain and subdomain to our web hosting provider. Trust me, it’s much easier than you might think. So, without further ado, let’s get started!

Alright, let me break it down for you in a simple and fun way! So, here’s the deal: We’re going to take our awesome website, which is built using Django (a cool Python framework) and Django-Rest-Framework (a powerful tool for creating APIs), and deploy it to AWS (that’s Amazon Web Services).

To get started, we’ll head over to the AWS dashboard and create an EC2 instance. Think of it as a virtual computer that lives in the cloud. It’s like having a little space to run our website.

Once our EC2 instance is up and running, we’ll do something cool. We’ll run our website on it, just like we do on our local machine. It’s like taking our application to the cloud and saying, “Hey, run this for the whole world to see!” But wait, there’s more! We want to connect our domain (the web address people use to access our website) to our application. This is where GoDaddy comes into play.

And boom! Just like that, our application is ready to be served to the world. People from all over can visit our website, use its awesome features, and marvel at our tech wizardry.

So, my friend, let’s dive into each part and explore them thoroughly. We’ll make sure you understand the ins and outs of deploying, connecting domains, and making your application shine like a superstar. Get ready for an exciting journey into the world of web deployment!

I’ve built an API authentication service using the Django Rest framework. To start the application, we run the command “python manage.py runserver”. Now, let’s create an AWS EC2 instance. We’ll head to the AWS console to get started.

Before we continue, make sure you have completed the registration process on AWS. Here’s what you need to do:

  1. Search for “ec2” and click on the first link in the results.
  2. You’ll find various details, including the resources section, launch instance section, service details, and more.
  3. Click on the “Launch an instance” button or go to the “Instances” section and click on “Launch an Instance” at the top right.
  4. You’ll be directed to a form where you can fill in the required information to create an instance.

To start, let’s give your special computer a name. Think of it as giving it a personal nickname. Next, we need to choose the right operating system (OS), which is like the computer’s main software. For your project, it’s best to pick Linux as the OS. Linux is reliable and versatile, making it a good choice. So, name your computer and select Linux as the operating system for your project.

Server is just an another computer where you just need to run your application!

Now, let’s go through the steps one by one. First, click on the operating system (OS) option. The system will automatically select the image with the free tier for you. If it doesn’t set it automatically, just choose the option that is eligible for the free tier.

Next, move on to selecting the architecture. In my case, I recommend choosing the 64-bit option.

After that, it’s time to select the instance type. For me, the suitable choice is t2.micro.

Now, we need to generate a key pair to ensure a secure login to our instance. I suggest creating a new key pair for each instance. Provide a name for the key pair, select the essential pair type (RSA), and choose the file format as .pem to generate a new key pair.

One of the most important steps is to create or select a security group for your instance. This determines which types of traffic your system will allow and handle. For now, like the basic options like SSH, HTTP, and HTTPS. You can always configure this later once your instance is created, so no need to worry about it right now. We’ll revisit this point when we set up our whole project.

Finally, click the “launch instance” button, and voila! Your instance will be created. Please note that it may take some time to complete the creation process.

Hushhh!! We have created an instance that is running successfully. One more thing that I would like to tell you all about the service we will be going to use. We are not going to use the Django default server instead we will be using Gunicorn and Nginx together to start our Django server. These two packages help host our server with more advanced features.

Now, we’re going to put our application into the EC2 instance (basically, the computer) we set up earlier. To do this, I have a GitHub repository where I’ve written my Django project. If you’re new to Django or want to learn how to host a website, you can check out a tutorial or search for a simple Django project on GitHub to get a better idea of how it all works.

You should see this list of instances

That’s correct! Now, all you need to do is select the instance and click on the “connect” button located at the top-right corner of your computer screen. Take a close look at all the details provided in the EC2 instance connect window, especially the username, as sometimes errors occur due to incorrect usernames. Once you’ve double-checked everything, go ahead and click “connect.” You’ll be taken to a new window, which serves as the terminal for your EC2 instance. From here on out, we’ll be handling everything within this instance.

We’ve chosen a Linux instance, so having some basic Linux commands under your belt will be handy. If you’re not familiar with them, at least try to look up the following commands.

First, let’s create a new directory in our instance. Use this command:

mkdir “my_django_project

Avoid using generic names like “app” or “Linux” to prevent confusion with existing directories. Now, let’s enter into this newly created directory using the `cd my_django_project` command. Next, we’ll need Git to interact with our repository. To install Git on our instance, run these commands:

sudo yum update
sudo yum install git

You can check if Git is installed by typing `git version`.

Now, go to your GitHub repository, click the “code” button, and copy the URL to clone the repository into our instance. Use the following command `git clone {{your_github_repo_url}}` and press Enter. If everything is set up correctly, your project will be cloned.

Navigate into your project directory and start the Django server with `python manage.py runserver`.

I won’t be diving deep into the technical details of Django; instead, I’ll focus on the instance setup.

Django comes with a built-in development server, which is excellent for development purposes. However, for production, you need a robust and powerful server. That’s where Gunicorn and Nginx come into play. Gunicorn acts as a WSGI (Web Server Gateway Interface) server, handling the interaction between your application and the server. It’s known for its ability to serve content efficiently, with features like logging and security. However, serving static files can be challenging, which is where Nginx, a web server, comes in.

After cloning your repository, you’ll need to install the required packages. Django projects typically have a ‘requirements.txt’ file. If you don’t have one, don’t worry; you can manually install the needed packages. I’ll show you a quick trick to create a ‘requirements.txt’ file. First, ensure you have Python, Pip, and Virtualenv installed. Execute the following commands:

sudo apt update
sudo apt install python3
sudo apt install python3-pip
sudo pip3 install virtualenv


Please note that if you’re coming from a Windows background, you don’t need to use python3 or pip3; you can use python or pip directly. In Ubuntu, we add the “3” to the front.

Now, let’s create the ‘requirements.txt’ file with the following command:

pip3 freeze > requirements.txt

This command captures all the packages your project needs, along with their versions, and saves them in the ‘requirements.txt’ file.

Additionally, consider setting up a virtual environment, especially if you’re working on multiple projects or projects with different tech stacks. A virtual environment isolates your project from others. To create and activate a virtual environment, use these commands:

virtualenv myenv
source myenv/bin/activate


Finally, install all the required packages listed in the ‘requirements.txt’ file by running:

pip3 install -r requirements.txt

Now, a few more steps remain to complete the setup. Go to the Instances section and select the IPv4 address of the instance (in my case is 161.25.185.225) where you previously clicked on the ‘connect’ button. This URL is where your website will be accessible. Next, open your ‘settings.py’ file and include this URL in the ‘allowed hosts’ array. You can edit a file using the sudo nano settings.py command.

Now, you’re ready to set up Gunicorn and Nginx for your project.

To set up Gunicorn and Nginx, start by using the “pip3 install Gunicorn” command. Be sure to take note of the directory where your “manage.py” file resides; in my case, it’s in a folder called “webapp,” making my project directory path “/home/ubuntu/my_django_project/webapp/” Now, within your “my_django_project” directory, create two new folders, one named “conf” and the other “static.” To create and edit a Python configuration file, run the “sudo nano conf/gunicorn_config.py” command. In this file, add the following contents for your configuration.

command = “/home/ubuntu/myenv/bin/gunicorn”
pythonpath = “/home/ubuntu/my_django_project/webapp”
bind = “161.25.185.225:8000”
workers = 3

After making your changes in the configuration file, save it by pressing “Ctrl + O” Now, you can link your Gunicorn configuration to your project’s WSGI file by running the following command: “gunicorn -c conf/gunicorn_config.py webapp.wsgi” This step connects your Gunicorn settings to your project’s WSGI file.

To configure Nginx, first, start the Nginx service with the command “sudo service nginx start” Next, open your project’s “settings.py” file and locate the “STATIC_URL” setting. Modify it to include the path to your static files directory: “/home/ubuntu/my_django_project/static” Save the changes to the file.

Now, let’s create a new configuration file for your web application. Execute the following command to open a new file: “sudo nano /etc/nginx/sites-available/webapp” Once the file is open, input the following configuration settings.

server {
listen 80;
server_name 161.25.185.225;

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

location / {
proxy_pass
http://161.25.185.225:8000
}

}

In the previous instructions, we’ve configured Nginx to listen on port 80 and set the server name to the IPv4 address. Additionally, we’ve defined various locations for serving static files and handling the root URL.

To complete the Nginx setup, navigate to the “/etc/nginx/sites-enabled/” directory and create a symbolic link to the Nginx configuration file we just created using the following command: “sudo ln -s /etc/nginx/sites-available/webapp

Finally, to apply the changes and restart Nginx, execute the following command: “sudo systemctl restart nginx” This will ensure that your Nginx server is up and running with the newly configured settings.

To view your project in your web browser, simply enter the URL, and you should see your project up and running. If you want to link your domain from a vendor like GoDaddy, you can follow these steps:

  1. Log in to your GoDaddy account dashboard and select the domain you want to link.
  2. Click on “Manage DNS” where you’ll find an “ADD” button to add a DNS record.
  3. Choose the record type as “A record” The “Name” field can be your subdomain, such as “dev,” “api,” or simply “www” to link directly to your homepage.
  4. The crucial step is to add the IPv4 address of your EC2 instance in the “Value” field.
  5. Leave the TTL (Time to Live) at its default setting and click on “Add” Note that it may take some time for the DNS records to propagate globally to all DNS servers.

Now, you’ve linked your GoDaddy domain to your EC2 instance in one direction. To complete the process and enable reverse linking, follow these additional steps:

  1. Edit the Nginx configuration file by running the command: “sudo nano /etc/nginx/sites-available/webapp
  2. In the configuration file, locate the “server_name” directive and change it from the IPv4 address to your new domain name (e.g., “www.webapp.com").
  3. After making this change, save the file and exit the text editor.
  4. Finally, restart Nginx to apply the new configuration with the command: “sudo systemctl restart nginx

Now, your website should be perfectly up and running on your custom domain name.

I trust that you’ve grasped all the fundamental concepts of AWS hosting explained here. While there may be some nuanced steps I haven’t covered, they should be straightforward for you to handle. Your journey to mastering AWS hosting is well underway. Thanks for taking the time to read this blog, and I look forward to seeing you in the next one!

Happy Coding :)

--

--

Ravindra Raj Kanitkar
Canadiv’s Technology and Design

Hi there! I'm Ravindra Kanitkar, a backend developer at Canadiv and a Youtuber. I'm passionate about coding and creating online content.