Django Deployment Steps

beginner
11 min

Django Deployment Steps

Welcome to the Django Deployment guide! ๐ŸŽฏ This tutorial is designed to guide you through the process of deploying your Django web applications. By the end of this guide, you'll have a working understanding of how to deploy your Django projects to a production environment.

Introduction

Deployment is the process of making your Django application accessible to the public. In this tutorial, we'll cover the steps to deploy a Django application on a Linux-based server using Gunicorn and Nginx.

Before we dive in, make sure you have the following prerequisites:

  • A Django project that you've built and tested locally
  • A server with Python, Gunicorn, and Nginx installed
  • SSH access to your server

Preparing Your Django Project for Deployment

Creating a WSGI file

In Django, the WSGI (Web Server Gateway Interface) file acts as a bridge between your web application and the web server. To create a WSGI file, follow these steps:

  1. Navigate to your Django project's root directory.
  2. Create a new file named wsgi.py.
  3. Add the following code to wsgi.py:
python
""" WSGI config for myproject project. It exposes the WSGI callable as a module-level variable named `application`. """ import os import sys from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') application = get_wsgi_application()

Replace myproject with the name of your Django project.

Configuring Django's Static and Media Files

To serve your Django project's static and media files, follow these steps:

  1. In your project's settings.py file, ensure you have the following settings:
python
STATIC_URL = '/static/' MEDIA_URL = '/media/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  1. Collect the static files by running the following command:
bash
python manage.py collectstatic

Deploying with Gunicorn and Nginx

Installing Gunicorn

Gunicorn is a WSGI HTTP Server for Python. To install Gunicorn on your server, run:

bash
pip install gunicorn

Configuring Gunicorn

Create a new file named gunicorn_config.sh in your project's root directory with the following content:

bash
#!/bin/bash gunicorn myproject.wsgi:application -b 0.0.0.0:8000 -w 3 -k gthread -D

Replace myproject with the name of your Django project.

Make the script executable:

bash
chmod +x gunicorn_config.sh

Configuring Nginx

Create a new Nginx configuration file for your Django project:

bash
sudo nano /etc/nginx/sites-available/myproject

Add the following content to the file:

nginx
server { listen 80; server_name example.com; location / { include proxy_params; proxy_pass http://localhost:8000; } location /static { alias /path/to/your/project/static; } location /media { alias /path/to/your/project/media; } }

Replace example.com with your domain name and /path/to/your/project with the actual path to your Django project on the server.

Create a symbolic link from sites-available to sites-enabled:

bash
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/

Test the Nginx configuration:

bash
sudo nginx -t

Restart Nginx:

bash
sudo systemctl restart nginx

Starting Gunicorn

Start your Django application with Gunicorn:

bash
./gunicorn_config.sh

Your Django project is now live and accessible at the specified domain name!

Conclusion

Congratulations! You've successfully deployed your Django project to a production environment. Deployment might seem daunting at first, but with practice, it will become second nature.

๐Ÿ“ Note: To stop Gunicorn, use Ctrl+C in the terminal where it's running.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the WSGI file in Django?