Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use pm2 with Django

Tags:

python

django

pm2

I have just changed from Express with NodeJS to Django with Python. The only thing I miss about NodeJS was the wonderful process manager pm2. Can I use pm2 with Django?

like image 299
ruttydm Avatar asked Jan 06 '17 14:01

ruttydm


People also ask

What port does Django run on?

Many of us know that normally when we run a Django project, the default host is localhost, and the port number is 8000.

What is a Django app?

A Django application is a Python package that is specifically intended for use in a Django project. An application may use common Django conventions, such as having models , tests , urls , and views submodules.


2 Answers

You can define a pm2 script e.g

pm2{name}.json

with the following contents:

 {
  "apps": [
    {
      "name": "{name}",
      "cwd": "/srv/{name}",
      "args": "runserver",
      "script": "manage.py",
      "exec_mode": "fork",
      "exec_interpreter": "python"
    }
  ]
}

and run it with pm2 start {name}

obviously you need to replace {name} with your project's name.

like image 103
Richard Miles Avatar answered Nov 01 '22 09:11

Richard Miles


Steps

  • Install Node from given link
  • Install npm using this command in command prompt / terminal reference
npm install npm@latest -g
  • Run this command for execute django application non stop on server machine
     pm2 start echosystem.config.json
     Description of echosystem.config.json
{       
   apps:            
      [{
        name: "djnago_with_pm2",          
        script: "manage.py",
        args: ["runserver", "127.0.0.1:8000"], 
        exec_mode: "fork", 
        instances: "1", 
        wait_ready: true, 
        autorestart: false, 
        max_restarts: 5, 
        interpreter : "python" 
      }] 
}    
  • If we have to change the port number of our django application, then we can change the port no in args attribute of echosystem.config.json file. After done the changes in echosystem.config.json file, stop the server using

  • List item

    pm2 command like this:

    • pm2 stop all // this command will stop all server.
    • pm2 stop 0 // if you know the pm2 id then execute command like this.
    • pm2 start echosystem.config.json // again start the django application.

Download complete sample project from here django_with_pm2

like image 3
Sunil Malhotra Avatar answered Nov 01 '22 11:11

Sunil Malhotra