Deploying Django with PM2 and Gunicorn
- ##django
- 20 Nov 2021
There are several tutorials on how to run django with pm2 but all use manage.py to deploy and this doesn’t seem like a good idea.
Even if behind Nginx, you should not use the django deployment server in production but you can run django with pm2 and gunicon.
I couldn’t find anything recent and reliable so here is my solution to deploy your django project using pm2 and gunicorn.
Create a file ecosystem.config.js
The first step is to create the ecosystem.config.js file that is used by pm2 to start your process:
module.exports = {
apps : [{
name: 'YourProjectName',
script: ' gunicorn django_project.wsgi -w 10 --threads=3 --bind 127.0.0.1:8081; ',
args: 'one two',
merge_logs: true,
autorestart: true,
log_file: "logs/combined.outerr.log",
out_file: "logs/out.log",
error_file: "logs/err.log",
log_date_format : "YYYY-MM-DD HH:mm Z",
append_env_to_name: true,
watch: false,
max_memory_restart: '5G',
}],
};
And to run it just do pm2 start ecosystem.config.js
you will then see your django project running through gunicorn available on pm2. This example assumes that you have configured nginx or apache to proxy port 8081 to your domain.