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?
Many of us know that normally when we run a Django project, the default host is localhost, and the port number is 8000.
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.
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.
Steps
npm install npm@latest -g
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With