DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build a Simple GitHub Action To Deploy a Django Application to the Cloud
  • Emulating the History Command Within a Bash Script
  • Micro Frontends Architecture
  • How To Use the Node Docker Official Image

Trending

  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Why Stable RAG Answers Can Still Hide Unstable Evidence
  • Identity in Action
  1. DZone
  2. Coding
  3. Frameworks
  4. Learn How to Deploy a Django Application

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.

By 
Shital Kat user avatar
Shital Kat
·
Dec. 31, 20 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free


In 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

  1. Install Apache2.
  2. List out the project's folder and file's path.
  3. Collect static files.
  4. Migrate the database.
  5. Change the permission and ownership of the database files and other folders.
  6. Make changes in the Apache config file.
  7. Enable the site.
  8. Install WSGI mod in Apache2.
  9. Restart the Apache Server.

Step 1: Install Apache 2

 The following are the commands to install the Apache 2 server on the Ubuntu instance.

Shell
 




x


 
1
sudo apt update
2
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.

Plain Text
 




xxxxxxxxxx
1
12


 
1
Directories 
2

          
3
Folder Name - /home/ubuntu/Demo
4

          
5
Project Name - DemoProject
6
Project Path - /home/ubuntu/Demo/DemoProject
7

          
8
Application Path - DemoApp
9
Application Path - /home/ubuntu/Demo/DemoProject/DemoApp
10

          
11
Environment Folder Path - /home/ubuntu/Demo/demo_env
12
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

Python
 




xxxxxxxxxx
1


 
1
# Add below code in settings.py file
2
import os 
3
STATIC_URL = '/static/' 
4
STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
5
STATICFILES=[STATIC_ROOT]


Activate the source and collect the static files using the following commands:

Shell
 




xxxxxxxxxx
1


 
1
source Demo/my_env/bin/activate
2
python Demo/DemoProject/manage.py collectstatic


Step 4: Migrate the Database

Migrate the database using the MakeMigration and Migrate command:

Shell
 




xxxxxxxxxx
1


 
1
python Demo/DemoProject/manage.py makemigrations
2
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.

Shell
 




xxxxxxxxxx
1


 
1
chmod 664 ~/Demo/DemoProject/db.sqlite3
2

          
3
sudo chown :www-data ~/Demo/DemoProject/db.sqlite3
4

          
5
sudo chown :www-data ~/Demo/DemoProject
6

          
7
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.

Shell
 




xxxxxxxxxx
1


 
1
# Go to the location - 
2
cd /etc/apache2/sites-available
3

          
4
# Take a backup of file
5
sudo cp 000-default.conf 000-default.conf_backup
6

          
7
# Open conf file using Vi
8
sudo vi 000-default.conf



Add the below code to the file:

XML
 




xxxxxxxxxx
1
26


 
1
Alias /static /home/ubuntu/Demo/DemoProject/static
2

          
3
        <Directory /home/ubuntu/Demo/DemoProject/static>
4

          
5
            Require all granted
6

          
7
        </Directory>
8

          
9
        <Directory /home/ubuntu/Demo/DemoProject/DemoProject>
10

          
11
            <Files wsgi.py>
12

          
13
                Require all granted
14

          
15
            </Files>
16

          
17
        </Directory>
18

          
19
        WSGIPassAuthorization On
20

          
21
        WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env
22

          
23
        WSGIProcessGroup DemoProject
24

          
25
        WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
26

          
25
        WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py


After adding the above snippet, the config file will look like this:
Shell
 




x
47


 
1
<VirtualHost *:80>
2

          
3
        # The ServerName directive sets the request scheme, hostname and port that
4
        # the server uses to identify itself. This is used when creating
5
        # redirection URLs. In the context of virtual hosts, the ServerName
6
        # specifies what hostname must appear in the request's Host: header to
7
        # match this virtual host. For the default virtual host (this file) this
8
        # value is not decisive as it is used as a last resort host regardless.
9
        # However, you must set it for any further virtual host explicitly.
10
        #ServerName www.example.com
11

          
12
        ServerAdmin webmaster@localhost
13
        DocumentRoot /var/www/html
14

          
15
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
16
        # error, crit, alert, emerg.
17
        # It is also possible to configure the loglevel for particular
18
        # modules, e.g.
19
        #LogLevel info ssl:warn
20

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

          
24
        # For most configuration files from conf-available/, which are
25
        # enabled or disabled at a global level, it is possible to
26
        # include a line for only one particular virtual host. For example the
27
        # following line enables the CGI configuration for this host only
28
        # after it has been globally disabled with "a2disconf".
29
        #Include conf-available/serve-cgi-bin.conf
30

          
31
        Alias /static /home/ubuntu/Demo/DemoProject/static
32
        <Directory /home/ubuntu/Demo/DemoProject/static>
33
            Require all granted
34
        </Directory>
35

          
36
        <Directory /home/ubuntu/Demo/DemoProject/DemoProject>
37
            <Files wsgi.py>
38
                Require all granted
39
            </Files>
40
        </Directory>
41

          
42
        WSGIPassAuthorization On
43
        WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env
44
        WSGIProcessGroup DemoProject
45
        WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
46

          
47
</VirtualHost>
17
        # It is also possible to configure the loglevel for particular


Step 7: Enable the Site

Now enable the above conf file using the a2ensite command.

Shell
 




xxxxxxxxxx
1


 
1
cd /etc/apache2/sites-available/
2

          
3
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.

Shell
 




xxxxxxxxxx
1


 
1
sudo apt-get install libapache2-mod-wsgi-py3
2
sudo a2enmod wsgi


Step 9: Restart the Apache Server

Restart the Apache server using the following command:

Shell
 




xxxxxxxxxx
1


 
1
sudo service apache2 restart


And you're done!

Django (web framework) application Command (computing) shell

Opinions expressed by DZone contributors are their own.

Related

  • How To Build a Simple GitHub Action To Deploy a Django Application to the Cloud
  • Emulating the History Command Within a Bash Script
  • Micro Frontends Architecture
  • How To Use the Node Docker Official Image

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook