Use of PM2 for Django Applications

Deploying Django Apps Efficiently with PM2

Leomahendradev



Django is a powerful Python web framework commonly used for building Multi-Page Applications (MPAs) and REST APIs. It comes with a built-in admin dashboard, making it easy to manage and moderate data.

When deploying Django applications, I usually rely on Gunicorn and Supervisor to manage processes efficiently. However, since I often host both frontend and backend applications on the same VPS, I wanted a single process manager to handle everything seamlessly.

Why PM2?

After trying different approaches, I found PM2 to be the best solution for managing both frontend and backend services under a single manager. It keeps applications running 24/7, provides detailed logs, and makes restarts super easy. Since I often need to delete and re-initiate services (because, let’s be honest, mistakes happen!), PM2 simplifies the entire process.


Installing PM2

Before using PM2, make sure Node.js and npm are installed:

sudo apt update && sudo apt install nodejs npm

Once installed, install PM2 globally:

npm install -g pm2

Verify the installation:

pm2 --version

Running a Django App with PM2

There are two ways to configure PM2 for Django:

  1. Without Gunicorn (For development or quick testing)
  2. With Gunicorn (Recommended for production)

1. Running Django Without Gunicorn

Create a configuration file (appname.config.js) in your project root:

module.exports = {
  apps: [
    {
      name: '<Project name>',
      script: '/bin/bash',
      args: '-c "source <path-to-env>/activate && python <path-to-manage.py> runserver 0.0.0.0:8000"',
      cwd: '<path-to-project-directory>',
      autorestart: true,
      env: {
        PYTHONBUFFERED: "1",
        DJANGO_SETTINGS_MODULE: '<main-app>.settings',
      },
    },
  ],
};

While this method works, it’s not ideal for production because Django’s built-in server is not optimized for handling multiple requests efficiently.


2. Running Django with Gunicorn (Recommended for Production)

For a production-ready setup, use Gunicorn, a robust WSGI server:

Create the PM2 configuration file (appname.config.js):

module.exports = {
  apps: [
    {
      name: '<Project name>',
      script: 'gunicorn',
      args: '<project_name>.wsgi --bind 0.0.0.0:8000 --workers 3 --threads 2 --preload',
      interpreter: 'python3',
      interpreter_args: '-m venv <venv_name>/bin/activate',
      cwd: '<path-to-project-directory>',
      autorestart: true,
      env: {
        PYTHONBUFFERED: "1",
        DJANGO_SETTINGS_MODULE: '<main-app>.settings',
      },
    },
  ],
};

Running PM2

Once your config file is set up, start the application:

pm2 start appname.config.js

To check running processes:

pm2 status

To view logs:

pm2 logs <app-name-in-config.js>

Why PM2 Over Supervisor?

  • Single Manager for Frontend & Backend: No need for separate process managers.
  • Easy Restarts: If something goes wrong, restarting is a breeze.
  • Detailed Monitoring: PM2 provides real-time logs and monitoring tools.
  • Automatic Restarts: Ensures services restart automatically in case of failure.

Post a Comment

0 Comments