Django App to AWS

Deploy Django App to AWS EC2 in 2024

  • Post author:
  • Post category:AWS / Blog
  • Post last modified:November 4, 2024
  • Reading time:12 mins read

Django is a powerful, open-source web framework designed to simplify and accelerate the development of complex, database-driven websites. If you’re ready to start building robust applications with Python, here’s a straightforward guide to installing and configuring Django. This guide walks you through setting up Django on an EC2 instance using Amazon Linux AMI.

Prerequisites

  1. An AWS Account with an EC2 instance is already set up.
  2. SSH Access to the EC2 instance.

Step 1: Connect to the EC2 Instance

Log in to your instance using SSH:

ssh -i your-key.pem ec2-user@your-ec2-public-ip

Step 2: Setting Up the Prerequisites

Before installing Django, make sure you have Python installed. Django is compatible with Python 3.6 and above, so you’ll want to confirm your Python version first.

To check your Python version, open your terminal and type:

[root@ip-172-31-7-169 ~]# python --version
Python 3.9.16

If you don’t have Python installed, you can download it from the official Python website and follow the installation instructions for your operating system.

Next, install pip, Python’s package manager, if it’s not already installed. To install pip, use the following command:

pip (also known by Python 3’s alias pip3) is a package-management system written in Python and is used to install and manage software packages.

sudo yum install python3-pip  # For Debian/Ubuntu/AWSAMI

On Windows, pip usually comes pre-installed with Python.

Step 3: Create a Virtual Environment

Using a virtual environment is highly recommended when working with Django. It allows you to manage dependencies separately and keeps your project isolated.

To create a virtual environment, first, navigate to your project directory, then run:

python3 -m venv myprojectenv

Replace myproject env with any name you prefer. This command will create a folder containing a copy of Python and a fresh installation of pip. To activate the environment:

On Linux/macOS:

source myprojectenv/bin/activate

On Windows:

myprojectenv\Scripts\activate

Your terminal prompt should change to indicate that the virtual environment is active.

Step 4: Install Django

With the virtual environment active, you can install Django using pip:

pip install django

You can verify that Django was installed correctly by checking the version:

[root@ip-172-31-7-169 ~]# django-admin --version
4.2.16

If everything is set up, you’ll see Django’s version number, confirming it’s ready for use.

Step 5: Create a Django Project

Now that Django is installed, you can create your first project. In your virtual environment, run the following command, replacing myproject with your project’s name:

django-admin startproject myproject

This command will create a new folder, myproject, with the following structure:

myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
  • manage.py: A command-line tool for interacting with your project.
  • settings.py: Contains all configurations for your Django project.
  • urls.py: Defines URL patterns for routing requests.
  • wsgi.py: Helps in deploying your project on a web server.

Step 6: Configuring Django

Open settings.py to adjust a few essential configurations.

  1. Secret Key: Django generates a SECRET_KEY automatically, but make sure it’s secure and unique.
  2. Debug Mode: For development, set DEBUG = True. For production, make sure to set DEBUG = False.

Allowed Hosts: Set the hosts that can access the project. During development, use: Python

Best Practices for ALLOWED_HOSTS

Development Environment: It’s acceptable to use ALLOWED_HOSTS = ["*"] during development when you’re testing the application locally or in a secure environment.

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

Production Environment: In production, you should specify the actual domain names or IP addresses your Django application will serve. For example:

ALLOWED_HOSTS = ["yourdomain.com", "www.yourdomain.com", "your-ec2-public-ip"]

Database Configuration: By default, Django uses SQLite. To configure a different database, change the DATABASES setting. For example, to use PostgreSQL, install psycopg2 and adjust the settings python

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # Specify the database backend
'NAME': 'your_database_name', # Name of your database
'USER': 'your_database_user', # Your database username
'PASSWORD': 'your_database_password', # Your database password
'HOST': 'localhost', # Database host, e.g., localhost or IP
'PORT': '5432', # Default PostgreSQL port
}
}

Step 7: Running Your Django Application

After configuring the settings, it’s time to start your Django application. Run the following commands to migrate the database and start the development server:

python manage.py migrate
cd myproject/

manage.py

python manage.py runserver 0.0.0.0:8000

The server should now be running at http://127.0.0.1:8000/. Visit this address in a web browser, and you should see the Django welcome page, confirming everything is set up correctly.

In the AWS console, make sure the security group associated with your EC2 instance allows HTTP (port 80 or 8000) and HTTPS (port 443, if using SSL) traffic.

Step 8: Creating Your First Django App

Django projects are structured around individual apps. To create a new app within your project, use the startapp command:

python manage.py startapp myapp

Replace myapp with the name of your app. Then, add your app to the INSTALLED_APPS list in settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # Your app here
]

Conclusion

Congratulations! You’ve now installed and configured Django on your server or local environment. This setup will serve as the foundation for building robust and scalable web applications. For production, consider additional configurations such as setting up Gunicorn, Nginx, and SSL to ensure your application is secure and performant.

techlinux.in

I’m a DevOps engineer with 8 years of experience in automation and cloud infrastructure. I’m passionate about optimizing software delivery and love sharing practical DevOps tips.Outside of work, I enjoy hiking, cooking, and contributing to open-source projects.Thanks for visiting my blog