How to Install and Deploy Django for a Production Environment
This article will provide thorough explanations, descriptions and a walk-through of the installation of Django, Celery, Redis, Nginx
with an SSL Certificate for a web app. Sign up and receive the Django on Ubuntu for Production on Linux script. Stability is one of the primary reasons for deploying
a production Django. Linux is also an ideal environment for running Celery for asynchronous task execution, Redis as a broker and backend
for Celery and Nginx as your web server.
Complications can arise with the installation of even just a few of the necessary application components. Components such as the asynchronous
task-master, Celery, Redis as the Celery messages broker and back end, along with NGINX to serve up the web pages. These complications are
greatly minimized when going with a Linux system versus Windows. In fact, the general convention and wisdom is that you don't deploy Django on
Windows to use for production. Only Linux for Django and celery and all the rest of the components to work in harmony together.
Installation Prerequisites - Python 3.10+
You will need an Ubuntu 22.04+ box with Python 3.10+ installed. Our script is based on the installation Ubuntu 24.04 and Python 3.12.
Install PIP
Check your Python version and install PIP with the particular version you have installed:
curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.12
Install Gunicorn
pip3 install gunicorn
Configure Gunicorn
Create the socket file for Gunicorn...
sudo vi /etc/systemd/system/gunicorn.socket
and copy and paste this code.
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Create the service file...
sudo vi /etc/systemd/system/gunicorn.service
and copy and paste this code.
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project
ExecStart=/home/ubuntu/my_venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--timeout 600 \
--bind unix:/run/gunicorn.sock \
my_project.wsgi:application
[Install]
WantedBy=multi-user.target
Install & Activate Virtual Environment
sudo pip3 install virtualenv
virtualenv -p python3.12 mrolive
source /home/ubuntu/mrolive/bin/activate
Install Django, Celery, Redis and Helpers
pip3 install django==4.2.4
pip3 install django-redis==5.2.0
pip3 install redis==4.5.5
pip3 install celery==5.2.7
pip3 install kombu==5.2.4
Set up Logging for Celery
sudo mkdir /var/log/celery
sudo chown -R ubuntu: /var/log/celery/
sudo chmod -R 775 /var/log/celery/
Install Redis
sudo apt-get install redis-server
redis-cli ping
You should get back:
Pong
Now set up auto-restart upon system reboot for Redis service."
sudo systemctl enable redis-server.service
Install Nginx
sudo apt-get install nginx
Configure Nginx
sudo vi /etc/nginx/sites-available/my_project
server {
listen 80;
server_name ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
autoindex on;
alias /var/www/my_project/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Start Gunicorn
Start the Gunicorn socket.
sudo systemctl start gunicorn.socket
Enable the Gunicorn socket.
sudo systemctl enable gunicorn.socket
Check the Gunicorn socket status.
sudo systemctl status gunicorn.socket
Check the Journal to Troubleshoot Problems
sudo journalctl -u gunicorn.socket
Start the Gunicorn service.
sudo systemctl start gunicorn
Confirm it started and if not check for issues in the status.
sudo systemctl status gunicorn
The most common reason I have found for issues with Gunicorn starting or running continuously is that the IP address is not in the settings.py file found
in the Django project directory. It should be listed in the "Allowed Hosts" section in settings.py. Configuration files for Nginx and Gunicorn
are necessary for Django's WSGI server to talk to Gunicorn and serve HTML templates, CSS, images and other static files.
file.
Collect & Copy Static Files to Static Directory
python manage.py collectstatic