Deploying Django to Production: Gunicorn + Nginx + Supervisor on Ubuntu Server

Deploying your Django web application to production may be an exciting as well as stressful process. Picture this: your Django masterpiece, polished and perfected, ready to break free from the confines of your development environment and venture into the vast expanse of the internet.

In this guide, we will be using the powerful trio – Gunicorn, the agile stallion of WSGI servers; Nginx, the reverse-proxy expert; and Supervisor, the vigilant guardian ensuring your processes stay on their best behavior. Get ready, as we explore the key components and strategies to guarantee a seamless transition from development to production.

Prerequisites

Before we start, make sure you have the following:

  1. An Ubuntu server with SSH access.
  2. A Django project with a working Gunicorn configuration.
  3. Basic knowledge of the Linux command line.

Step 1: Update the System

Ensure your system packages are up-to-date by running the following commands:

Step 2: Install Dependencies

Install the required software components:

Step 3: Install Gunicorn

Gunicorn (Green Unicorn) is a WSGI HTTP server for Python web applications. It will serve as the gateway between your Django application and the outside world. Install it using pip:

Step 4: Configure Gunicorn

Navigate to your Django project directory and create a Gunicorn service file. For example, create a file named gunicorn.service:

Replace username, groupname, /path/to/your/django/project, and myproject with your actual values.

Enable and start the Gunicorn service:

Step 5: Install and Configure Nginx

Nginx acts as a reverse proxy, handling client requests and forwarding them to Gunicorn. Install Nginx and configure it to pass requests to Gunicorn.

Create an Nginx server block configuration for your Django project. For example, create a file named /etc/nginx/sites-available/myproject:

Replace your_domain_or_ip and /path/to/your/django/project with your actual values.

Create a symbolic link to the sites-enabled directory and test the Nginx configuration:

Step 6: Implement SSL with Let’s Encrypt (Optional but Recommended)

Enhance the security of your deployment by implementing SSL with Let’s Encrypt. This step is crucial for securing data transmission between clients and your server.

Step 7: Install Supervisor

Supervisor ensures that Gunicorn is always running, even after server reboots. Install Supervisor and configure it to manage your Gunicorn processes.

Create a Supervisor configuration file, for example, /etc/supervisor/conf.d/myproject.conf:

Replace username and /path/to/your/django/project with your actual values.

Restart Supervisor to apply the changes:

Conclusion

Congratulations! You’ve successfully deployed your Django web application to production using Gunicorn, Nginx, and Supervisor on an Ubuntu Server. This combination ensures a solid, scalable, and reliable deployment that can handle the demands of the digital world. Remember to secure your server, configure your Django settings for production, and regularly update your system and dependencies for a secure and stable deployment. Happy coding!

Share this post If you find it useful. You may also check our guide to secure your Django app with Nginx, SSL, and CORS post.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *