Deploy Django project on AWS with Apache2 and mod_wsgi module.

Author: neptune | 13th-May-2022 | views: 41
#Python #Django

Steps to Deploy Django Project on Apache2 Server and Ubuntu OS.

In this blog, I use the AWS Ubuntu 18.22 instance as a Hosting platform and used an Apache2 server with mod_wsgi for configurations.

Step 1. Run these below cmd's to get the latest updates.

$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get install -y python3-pip

$ sudo python3 -m pip install --upgrade pip

Step2. Now let's make a new directory for our project.

$ mkdir /var/www

Move into the directory:

$ cd /var/www

Step 3. Now, Create a quick project or Import or clone an existing django project.

I will prefer to create a small project and then import or clone your existing project to make sure other things are working as expected.

Check that the Python is installed 

$ python3 --version

$ sudo python3 -m pip install django


$ sudo python3 -m pip uninstall django==2.2

Check the Django version.

$ python3 -m django --version

Create a project using django admin.

$ sudo django-admin startproject mysite

Change directory into that project:

$ cd mysite/

Create an app using 

$ sudo python3 startapp website

Migrate the changes to create db instances.

$ sudo python3 migrate

If you face any issue in migration run the below cmd:

$ sudo python3 migrate --run-syncdb

Run the django server.

$ sudo python3 runserver

Step 4. Now install apache2 and mod_wsgi for Python3.

$ sudo apt-get install -y apache2 libapache2-mod-wsgi-py3

Now let's setup the apache config file for our website:

$ sudo nano /etc/apache2/sites-available/mysite.conf


<VirtualHost *:80>


ErrorLog ${APACHE_LOG_DIR}/mysite-error.log 

CustomLog ${APACHE_LOG_DIR)/mysite-access.log combined

WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite 

WSGIProcessGroup mysite

WSGIScriptAlias / /var/www/mysite/mysite/ 

Alias /robots.txt /var/www/mysite/static/robots.txt

Alias /favicon.ico /var/www/mysite/static/favicon.ico

Alias /static/ /var/www/mysite/static/

Alias /static/ /var/www/mysite/media/

<Directory /var/www/mysite/mysite>


      Require all granted



<Directory /var/www/mysite/static> 

   Require all granted 


<Directory /var/www/mysite/media>

    Require all granted



<Directory /var/www/mysite>

        WSGIApplicationGroup %{GLOBAL}

        Order deny,allow

        Allow from all


ServerName, WSGIProcessGroup and also change Directory as per the project.


Remove the default site if you wish to remove it.

$ sudo rm /etc/apache2/sites-available/000-default.conf

Now enable our new site “mysite.conf” and reload the Apache server.

$ sudo a2ensite mysite

$ sudo systemctl reload apache2

Check the status using below cmd.

$ sudo systemctl status apache2.service

Finally, we can try the url or IP that we configured in “mysite.conf” in ServerName. 

Example: ServerName

But we find that we can't access the Images and not able to login to application because our database is read-only for our user.

In this case, using apache, our "user" is www-data. We need to give this user permission to edit these files for us. 

$ cd /var/www/

$ sudo chown www-data mysite/

$ sudo chown www-data mysite/db.sqlite3

$ sudo chown www-data mysite/*

$ sudo chown www-data mysite/media/*

$ sudo chown www-data mysite/static/*

Then reload server:

$ sudo service apache2 reload 


$ sudo systemctl reload apache2

You can check the users using:

$ ls -l

You can check the status of your apache server:

$ sudo systemctl status apache2.service

To start or stop apache service use below cmds:

$sudo systemctl start apache2.service

$sudo systemctl stop apache2.service

After successful run now you can import your existing project.

$ sudo git clone

Then install all the required applications like:

$ sudo pip install django-taggit==1.3.0

$ sudo pip install django-crispy-forms==1.10.0

$ sudo pip install django-summernote==

$ sudo apt install Pillow==8.1.0

Run this cmd to get all the static files in a path that you configured in so that django can use them.

$ sudo python3 collectstatic

To check the logs use below cmds:

$ cd /var/log/apache2

Move to the directory or directly access using below nano cmd:

$ sudo nano /var/log/apache2/mysite-error.log

$ sudo nano /var/log/apache2/mysite-access.log

Thanks for Reading !!!

anonymous | May 13, 2022, 4:22 p.m.

I was looking Apache2 and Flask but this also worked

Related Blogs
How to extract Speech from Video using Python?
Author: neptune | 30th-Apr-2022 | views: 1383
Simple and easy way to convert video into audio then text using Google Speech Recognition API...
How to download video from youtube using python module ?
Author: neptune | 24th-Apr-2022 | views: 809
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..
Python 3.9 new amazing features ?
Author: neptune | 24th-Apr-2022 | views: 637
We are going to explore the newest features of Python 3.9 really amazing features here list of some features like Dict union, Type hinting etc. with examples...
Mostly asked Python Interview Questions - 2021.
Author: neptune | 22nd-Feb-2022 | views: 606
#Python #Interview
Python interview questions for freshers. These questions asked in 2021 Python interviews...
Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 24th-Apr-2022 | views: 522
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...
Best Python package manager and package for virtual environment ?
Author: neptune | 15th-Apr-2022 | views: 463
#Python #Anaconda #Virtualenv #Pip
Which is the best package manager for python and Virtual environment management using Virtualenv and Anaconda...
Will, AI kills developers and programmers job ?
Author: neptune | 15th-Apr-2022 | views: 384
#Python #Machine learning #AI
GPT-3’s performance has convinced that Artificial intelligence is closer or at least AI-generated code is closer than we think. It generates imaginative, insightful, deep, and even excellent content...
Do you know Jupyter is now full-fledged IDE?
Author: neptune | 15th-Apr-2022 | views: 314
#Python #Jupyter
Jupyter is a widely used tool by Data scientists. So developers from institutions like Two Sigma, Bloomberg and convert it into IDE lets see..
What exactly you can do with Python?
Author: neptune | 15th-Apr-2022 | views: 287
Well, it's a tricky question to answer because there are lots of application of Python. But, I will tell you about 3 main applications of Python...
How to reverse string in Python ?
Author: neptune | 16th-May-2022 | views: 221
We are going to explore different ways to reverse string in Python...
Introduction to Apache Spark
Author: neptune | 09th-May-2022 | views: 65
#Python #Apache Spark
Apache Spark is an open-source, distributed processing system that utilizes in-memory caching and optimized query execution for faster queries...
Datatypes in Python.
Author: neptune | 15th-May-2022 | views: 50
Python have different types of datatypes like Numbers, Strings, Lists, Tuples, Dictionary, Set, Frozenset, Bool, Mutable, and Immutable...
Core Python Syllabus for Interviews
Author: neptune | 14th-May-2022 | views: 42
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...
View More