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 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.
- 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.
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.
Step 3: Collect Static Files
Django provides a mechanism for collecting static files into one place so that they can be served easily.
Setting.py using the following command:
Activate the source and collect the static files using the following commands:
Step 4: Migrate the Database
Migrate the database using the
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.
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.
Add the below code to the file:
Step 7: Enable the Site
Now enable the above conf file using the
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.
Step 9: Restart the Apache Server
Restart the Apache server using the following command:
And you're done!
Opinions expressed by DZone contributors are their own.