How to Deploy a Django Application: Step-by-Step Guide

How to Deploy a Django Application: Step-by-Step Guide

Deploying a Django app on an EC2 instance involves several steps. Here's a detailed guide to help you through the process:

1. Set Up Your EC2 Instance

  1. Launch an EC2 instance:

    • Log in to your AWS Management Console.

    • Navigate to the EC2 Dashboard and click "Launch Instance".

    • Choose an Amazon Machine Image (AMI) such as Ubuntu Server.

    • Select an instance type (e.g., t2.micro for free tier).

    • Configure instance details, add storage, and tag your instance if necessary.

    • Configure security group rules to allow HTTP (port 80) and SSH (port 22) traffic.

    • Review and launch the instance. Make sure you create a new key pair or use an existing one to access your instance.

  2. Connect to your EC2 instance:

    • Open your terminal (or use PuTTY if on Windows).

    • Use the SSH command to connect to your instance:

        ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-ip
      

2. Set Up Your Environment

  1. Update the package lists:

     sudo apt update
    
  2. Install necessary packages:

     sudo apt install python3-pip python3-dev libpq-dev nginx git
    
  3. Install virtualenv:

     sudo pip3 install virtualenv
    

3. Deploy Your Django Application

  1. Clone your Django project:

    • Navigate to the directory where you want to store your project and clone it from your version control system (e.g., GitHub):

        git clone https://github.com/your-username/your-repo.git
      
  2. Create and activate a virtual environment:

     cd your-repo
     virtualenv venv
     source venv/bin/activate
    
  3. Install the required Python packages:

     pip install -r requirements.txt
    
  4. Set up your Django project:

    • Make necessary migrations and create a superuser:

        python manage.py makemigrations
        python manage.py migrate
        python manage.py createsuperuser
      

4. Configure Gunicorn

  1. Install Gunicorn:

     pip install gunicorn
    
  2. Test Gunicorn:

     gunicorn --workers 3 your_project_name.wsgi:application
    

5. Configure Nginx

  1. Create an Nginx server block configuration:

    • Open a new configuration file in Nginx:

        sudo nano /etc/nginx/sites-available/your_project_name
      
    • Add the following configuration:

        server {
            listen 80;
            server_name your_domain_or_IP;
      
            location / {
                proxy_pass http://127.0.0.1:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
      
            location /static/ {
                alias /home/ubuntu/your-repo/static/;
            }
      
            location /media/ {
                alias /home/ubuntu/your-repo/media/;
            }
        }
      
  2. Enable the server block configuration:

     sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled
    
  3. Test and restart Nginx:

     sudo nginx -t
     sudo systemctl restart nginx
    

6. Set Up Gunicorn to Start on Boot

  1. Create a systemd service file for Gunicorn:

     sudo nano /etc/systemd/system/gunicorn.service
    
  2. Add the following configuration:

     [Unit]
     Description=gunicorn daemon
     After=network.target
    
     [Service]
     User=ubuntu
     Group=www-data
     WorkingDirectory=/home/ubuntu/your-repo
     ExecStart=/home/ubuntu/your-repo/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/your-repo/gunicorn.sock your_project_name.wsgi:application
    
     [Install]
     WantedBy=multi-user.target
    
  3. Start and enable the Gunicorn service:

     sudo systemctl start gunicorn
     sudo systemctl enable gunicorn
    

7. Security and Maintenance

  1. Configure firewall settings:

     sudo ufw allow 'Nginx Full'
     sudo ufw enable
    
  2. Monitor your application logs:

     sudo journalctl -u gunicorn
     sudo tail -f /var/log/nginx/error.log
    

By following these steps, you should be able to deploy your Django app on an EC2 instance successfully. If you encounter any issues, make sure to check the logs and troubleshoot accordingly.