Hosting Procedure for Django Application on AWS Ubuntu Server with Ubuntu, Gunicorn, and Nginx

This article serves as an all-encompassing guide for seamlessly deploying a Django application on an AWS Ubuntu server, leveraging the power of Gunicorn and Nginx to ensure a robust and scalable production environment. The step-by-step instructions encompass essential aspects of the deployment process, from updating system packages and configuring PostgreSQL for database management to setting up virtual environments, installing dependencies, and integrating Django with Gunicorn. Additionally, the guide walks through the configuration of Nginx as a reverse proxy to optimize performance and enhance security. It concludes with crucial steps for firewall management and security rules to streamline the application’s accessibility. By following this comprehensive guide, users can navigate the intricacies of deploying Django applications on AWS, empowering them to establish a resilient and efficient web hosting environment. Whether you’re a seasoned developer or a newcomer to Django deployment, this repository provides detailed insights and instructions, making the deployment process accessible and efficient.
1. Create an Instance in AWS:
#Launch an EC2 instance with Ubuntu as the operating system.
#Copy the key from AWS and open the terminal in the .pem file directory.
2. Initial Setup:
#Update the system and install essential packages.
#This code updates the system and installs essential packages including Python 3, pip, development libraries, PostgreSQL, Nginx, and curl.
sudo apt update
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
3. Database Setup:
#Connect to the PostgreSQL database.
#This command enters the PostgreSQL interactive terminal.
sudo -u postgres psql
#Create a Database and User.
#These SQL commands create a PostgreSQL database, a user, and grant privileges to the user on the created database.
CREATE DATABASE yourdatabase;
CREATE USER yourdatabaseuser WITH PASSWORD ‘yourpassword’;
GRANT ALL PRIVILEGES ON DATABASE yourdatabase TO yourdatabaseuser;
\q
4. Update PIP and Install Virtualenv:
#Update PIP and install Virtualenv for Python environment isolation.
#These commands update pip to the latest version and install the virtual environment tool.
sudo -H pip3 install — upgrade pip
sudo -H pip3 install virtualenv
5. Project Setup:
#Create a directory (optional) and a virtual environment.
#These commands create a directory, set up a virtual environment, and activate it.
mkdir ~/yourprojectdir
cd ~/yourprojectdir
virtualenv yourprojectenv
source yourprojectenv/bin/activate
6. Clone the Django Project:
#Clone your Django project from GitHub.
7. Configure Django Settings:
#Open the settings file and update configurations.
#Database Configuration:
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘yourdatabase’,
‘USER’: ‘yourdatabaseuser’,
‘PASSWORD’: ‘yourpassword’,
‘HOST’: ‘localhost’,
‘PORT’: ‘’,
}
}
#Static Configuration:
STATIC_ROOT = os.path.join(BASE_DIR, ‘static/’)
8. Install Dependencies and Migrate:
#Install project dependencies and perform migrations.
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
9. Create Superuser and Collect Static:
#These commands create a superuser for the Django admin and collect static files.
python manage.py createsuperuser
python manage.py collectstatic
10. Deactivate Virtual Environment:
#Deactivate the virtual environment.
deactivate
11. Gunicorn Setup:
#These commands create a superuser for the Django admin and collect static files.
sudo nano /etc/systemd/system/gunicorn.socket
#Add the following content:
#This is the systemd socket unit file for Gunicorn, configuring it to listen on a Unix domain socket.
[Unit]
Description=gunicorn socket[Socket]
ListenStream=/run/gunicorn.sock[Install]
WantedBy=sockets.target
#Opens a text editor to create/edit the Gunicorn service systemd unit file.
sudo nano /etc/systemd/system/gunicorn.service
#Add the following content:
#This is the systemd service unit file for Gunicorn, specifying its configuration, working directory, and how it should be started.
#These configurations set up Gunicorn as a systemd service with a socket for Nginx communication.
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/yourprojectdir
ExecStart=/home/ubuntu/yourprojectenv/bin/gunicorn \
— access-logfile — \
— workers 3 \
— bind unix:/run/gunicorn.sock \
yourproject.wsgi:application[Install]
WantedBy=multi-user.target
# Start and enable Gunicorn socket
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
# Checking Gunicorn socket status
sudo systemctl status gunicorn.socket
# Checking existing Gunicorn socket file
file /run/gunicorn.sock
# Checking Gunicorn socket with journalctl
sudo journalctl -u gunicorn.socket
# Testing Gunicorn socket activation
sudo systemctl status gunicorn
# Test the Gunicorn socket activation with curl
curl — unix-socket /run/gunicorn.sock localhost
# Gunicorn status
sudo systemctl status gunicorn
# Gunicorn check with journalctl
sudo journalctl -u gunicorn
# Systemd reload (optional)
sudo systemctl daemon-reload
# Restarting systemd (optional)
sudo systemctl restart gunicorn
# Configure Nginx
sudo nano /etc/nginx/sites-available/myproject
12. Nginx Configuration:
#This opens a text editor to create/edit the Nginx server block configuration file.
#This Nginx configuration file defines a server block, specifying how Nginx should handle requests for the Django application.
server {
listen 80;
server_name server_domain_or_IP;location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/yourprojectdir;
}location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
#This command creates a symbolic link to enable the Nginx server block configuration.
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled
13. Test Nginx Configuration and Restart:
#These commands test the Nginx configuration for syntax errors and restart Nginx.
sudo nginx -t
sudo systemctl restart nginx
14. Remove Firewall Rule and Allow Nginx:
#These commands remove a firewall rule and allow Nginx traffic.
sudo ufw delete allow 8000
sudo ufw allow ‘Nginx Full’
#This guide provides a step-by-step process for hosting a Django application on an AWS Ubuntu server.

By following these detailed instructions, users, whether seasoned developers or newcomers to Django deployment, can successfully navigate the complexities of deploying Django applications on AWS. This guide empowers users to establish a resilient and efficient web hosting environment, optimizing performance and enhancing security.