Deploy Django + PostgreSQL on EC2 using Apache2 from scratch

It’s fun to build apps as personal projects, but what if you want it to deploy it and showcase it to others? And not only that it should be able to handle multiple requests simultaneously. Well, in this article we’ll do this. Create Django apps, use PostgreSQL as a database and use Apache2 for handling multiple requests and some tips for logging and debugging errors.

Setup EC2 instance

Setup PostgreSQL

sudo apt-get updatesudo apt-get install python-dev libpq-dev postgresql postgresql-contrib
sudo su — postgrespsqlCREATE DATABASE djangodemodb;CREATE USER adesh WITH PASSWORD ‘mysecretpass’;GRANT ALL PRIVILEGES ON DATABASE djangodemodb TO adesh;\qexit

Setup Django

mkdir djangodemo 
cd djangodemo
virtualenv -p python3 djangodemo
source djangodemo/bin/activate 
pip install django==2.2
python -m django startproject djangodemo_UI
python manage.py makemigrations && python manage.py migratepython manage.py runserver
ALLOWED_HOSTS = [‘*’]Change DATABASES to this:DATABASES = {‘default’: {‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,‘NAME’: ‘djangodemodb’,‘USER’: ‘adesh’,‘PASSWORD’: ‘mysecretpass’,‘HOST’: ‘localhost’,‘PORT’: ‘’,}}
pip install psycopg2
pip install psycopg2-binary
python manage.py createsuperuser

Setup Apache2

sudo apt-get updatesudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
vim /etc/apache2/sites-available/000-default.conf
000-default.conf
vim /etc/apache2/apache2.conf
apache2.conf
sudo chown :www-data djangodemo_UI
sudo service apache2 restart

Logging and Debugging

add to settings.py
tail -n 100 /var/log/apache2/error.log

Notes

Machine/Deep Learning, Amateur Photographer