Learn How to Deploy a Django Application
In this tutorial, an experienced software developer demonstrates how to deploy a Django application on a production server.
Join the DZone community and get the full member experience.
Join For FreeIn this post, we'll go over the steps to deploy a Django application on a production server. I am using an AWS ec2 server, an Ubuntu 20.04 instance, and Python 3.8. The steps are the same for most versions of Ubuntu and Python, however, the syntax might differ based on the version you are using.
Steps
- Install Apache2.
- List out the project's folder and file's path.
- Collect static files.
- Migrate the database.
- Change the permission and ownership of the database files and other folders.
- Make changes in the Apache config file.
- Enable the site.
- Install WSGI mod in Apache2.
- Restart the Apache Server.
Step 1: Install Apache 2
The following are the commands to install the Apache 2 server on the Ubuntu instance.
sudo apt update
sudo apt install apache2
Step 2: List Out the Project's Folder/File Path
It is important to list the project path in order to follow the next step. List your Django project's name and path, application name and path, environment's location path, and WSGI file path.
xxxxxxxxxx
Directories
Folder Name - /home/ubuntu/Demo
Project Name - DemoProject
Project Path - /home/ubuntu/Demo/DemoProject
Application Path - DemoApp
Application Path - /home/ubuntu/Demo/DemoProject/DemoApp
Environment Folder Path - /home/ubuntu/Demo/demo_env
Wsgi File Path - /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
Step 3: Collect Static Files
Django provides a mechanism for collecting static files into one place so that they can be served easily.
Open Setting.py
using the following command:
vi Demo/DemoProject/DemoProject/settings.py
xxxxxxxxxx
# Add below code in settings.py file
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES=[STATIC_ROOT]
Activate the source and collect the static files using the following commands:
xxxxxxxxxx
source Demo/my_env/bin/activate
python Demo/DemoProject/manage.py collectstatic
Step 4: Migrate the Database
Migrate the database using the MakeMigration
and Migrate
command:
xxxxxxxxxx
python Demo/DemoProject/manage.py makemigrations
python Demo/DemoProject/manage.py migrate
Step 5: Change Permission and Ownership
If you are using a SQLite database, then change the permissions of the SQLite file. Also, change the ownership of the Django project folders.
The following commands will change the permission and ownership of the files and folders.
xxxxxxxxxx
chmod 664 ~/Demo/DemoProject/db.sqlite3
sudo chown :www-data ~/Demo/DemoProject/db.sqlite3
sudo chown :www-data ~/Demo/DemoProject
sudo chown :www-data ~/Demo/DemoProject/DemoProject
Step 6: Changes in Apache Config File
We need to make a few changes in the 000-default.conf file. Before that, though, make backup of the file. The following are the commands to open the file and create backup of the file.
xxxxxxxxxx
# Go to the location -
cd /etc/apache2/sites-available
# Take a backup of file
sudo cp 000-default.conf 000-default.conf_backup
# Open conf file using Vi
sudo vi 000-default.conf
Add the below code to the file:
xxxxxxxxxx
Alias /static /home/ubuntu/Demo/DemoProject/static
<Directory /home/ubuntu/Demo/DemoProject/static>
Require all granted
</Directory>
<Directory /home/ubuntu/Demo/DemoProject/DemoProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPassAuthorization On
WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env
WSGIProcessGroup DemoProject
WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/ubuntu/Demo/DemoProject/static
<Directory /home/ubuntu/Demo/DemoProject/static>
Require all granted
</Directory>
<Directory /home/ubuntu/Demo/DemoProject/DemoProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPassAuthorization On
WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env
WSGIProcessGroup DemoProject
WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
</VirtualHost>
Step 7: Enable the Site
Now enable the above conf file using the a2ensite
command.
xxxxxxxxxx
cd /etc/apache2/sites-available/
sudo a2ensite 000-default.conf
Step 8: Install WSGI mod in Apache 2
Install the WSGI mod library for the Apache 2 server using the following command. After installation, enable the WSGI.
xxxxxxxxxx
sudo apt-get install libapache2-mod-wsgi-py3
sudo a2enmod wsgi
Step 9: Restart the Apache Server
Restart the Apache server using the following command:
xxxxxxxxxx
sudo service apache2 restart
And you're done!
Opinions expressed by DZone contributors are their own.
Comments