How to deploy Django on Ubuntu with Postgresql and apache and python3.9 (Production level).

Step 1: Install the required libraries

i) To install Apache Server

“sudo apt-get install apache2”

ii) To install wsgi library

“sudo apt-get install libapache2-mod-wsgi-py3”

iii)To install python 3.9

a) Download python 3.9.1

wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz

b) Extract it

tar -xf Python-3.9.1.tgz 

c) Go to the extracted directory

cd Python-3.9.1./configure --enable-optimizations 

d) Install make

sudo apt install build-essential

e) Now, Install it using following command

sudo make altinstall

f) Check installation

python3.9 --version

g) Set it as alternatives

sudo update-alternatives --install /usr/bin/python3 python /usr/bin/python3.7 1 sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.9 2

h) Now Set Alternatives

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python (providing /usr/bin/python3).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/bin/python3.9 2 auto mode
1 /usr/bin/python3.7 1 manual mode
2 /usr/local/bin/python3.9 2 manual mode

Press <enter> to keep the current choice[*], or type selection number:

i) Set python3 as python

nano ~/.bashrc
alias python=python3
source ~/.bashrc

Step 2: Changing the configuration file of apache2

i) To open the configuration file

“sudo nano /etc/apache2/sites-available/000-default.conf”

ii) Add following lines at the bottom of this file (Change the project name according to your project.)

WSGIDaemonProcess www-data python-home=/djangoProject/venv python-path=/djangoProject/myFirstProject
WSGIProcessGroup www-data
WSGIScriptAlias / /djangoProject/myFirstProject/myFirstProject/wsgi.py

<Directory /djangoProject/myFirstProject/myFirstProject>
Require all granted
</Directory>

Alias /media/ /djangoProject/myFirstProject/media/
Alias /static/ /djangoProject/myFirstProject/static/

<Directory /djangoProject/myFirstProject/static>
Require all granted
</Directory>

<Directory /djangoProject/myFirstProject/media>
Require all granted
</Directory>

After adding it will look as follows:

<VirtualHost *:80>

ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

WSGIDaemonProcess www-data python-home=/djangoProject/venv python-path=/djangoProject/myFirstProject
WSGIProcessGroup www-data
WSGIScriptAlias / /djangoProject/myFirstProject/myFirstProject/wsgi.py

<Directory /djangoProject/myFirstProject/myFirstProject>
Require all granted
</Directory>

Alias /media/ /djangoProject/myFirstProject/media/
Alias /static/ /djangoProject/myFirstProject/static/

<Directory /djangoProject/myFirstProject/static>
Require all granted
</Directory>

<Directory /djangoProject/myFirstProject/media>
Require all granted
</Directory>

</VirtualHost>

Step 3: Check the syntax error

sudo apache2ctl configtest

Step 4: Restart Apache Server

sudo systemctl restart apache2

Hosting of Django Project on Apache Server is that much easy.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store