DEV Community

Mohammad Anisur Rahman
Mohammad Anisur Rahman

Posted on • Edited on

How To Deploy a Django Application with Apache2 on Ubuntu in AWS EC2 instance?

In this tutorial, you’ll deploy a Django application on Ubuntu EC2 instance running Apache2 step by step.

Prerequisites
  • Install apache2
~ sudo apt update

~ sudo apt install apache2

~ sudo service apache2 start

Enter fullscreen mode Exit fullscreen mode
  • Apache Error watch
~ watch tail -n 15 /var/log/apache2/error.log 

Enter fullscreen mode Exit fullscreen mode
  • Check python version
~ python3 –version
Enter fullscreen mode Exit fullscreen mode
  • Install pip
~ sudo apt install python3-pip

~ sudo apt install python3-venv
Enter fullscreen mode Exit fullscreen mode
  • Create project directory

~ mkdir demo

  • Change the ownership to ubuntu. Example:
~ sudo chown -R ubuntu:ubuntu demo

~ sudo chmod 777 demo

~ sudo chmod 755 /home/<user>

Enter fullscreen mode Exit fullscreen mode
  • Add GitHub Repository. If you need generate SSH key for GitHub, you can visit this link.
~ git init

~ git remote add origin repository_link

~ git pull origin main

Enter fullscreen mode Exit fullscreen mode
  • Change the ownership of the media directory. Example:
~ sudo chown -R www-data:www-data demo/media
Enter fullscreen mode Exit fullscreen mode
  • Create virtual env at demo
~ cd demo

~ python3 -m venv env

~ source env/bin/activate #Activate virtual env

Enter fullscreen mode Exit fullscreen mode
  • Install packages

~ pip3 install -r requirements.txt


Apache Server Configurations
  • Make a demo.conf file on /etc/apache2/sites-available and configure following way
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /home/ubuntu/demo
    <Directory /home/ubuntu/demo>
        AllowOverride all
        Require all granted
        Options FollowSymlinks
        Allow from all
    </Directory>

    Alias /media /home/ubuntu/demo/media
    Alias /static /home/ubuntu/demo/static

    <Directory /home/ubuntu/demo/static>
        Require all granted
    </Directory>

    <Directory /home/ubuntu/demo/media>
    Require all granted
    </Directory>

    <Directory /home/ubuntu/demo/demo>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    WSGIDaemonProcess demo python-path=/home/ubuntu/demo python-home=/home/ubuntu/demo/env
    WSGIProcessGroup demo
    WSGIScriptAlias / /home/ubuntu/demo/demo/wsgi.py

</VirtualHost>

Enter fullscreen mode Exit fullscreen mode
  • Make migrate and collect static
~ source env/bin/activate

~ python3 manage.py makemigrations

~ python3 manage.py migrate

~ python3 manage.py collectstatic
Enter fullscreen mode Exit fullscreen mode
  • Change permission and ownership
~ sudo chmod 664 db.sqlite3

~ sudo chown :www-data db.sqlite3

~ sudo chown :www-data demo

~ sudo chown :www-data demo/demo

Enter fullscreen mode Exit fullscreen mode
  • Enable apache site configuration file
~ sudo a2ensite demo.conf

~ sudo a2dissite default.conf

Enter fullscreen mode Exit fullscreen mode
  • Install wsgi mod in apache
~ sudo apt-get install libapache2-mod-wsgi-py3

~ sudo a2enmod wsgi

~ sudo service apache2 restart

Enter fullscreen mode Exit fullscreen mode
  • Apache Server log
~ watch tail -n 15 /var/log/apache2/error.log 
Enter fullscreen mode Exit fullscreen mode

Done! Now you can browse your application with the domain name.

Top comments (0)