An example of how to deploy an python project using PM2+nginx
- 2021-01-22 05:14:56
- OfStack
Deploying an django project using pm2+nginx1 was cumbersome earlier. This section describes deploying an django project using pm2+nginx1
The main features of PM2:
Built-in load balancing (using Node cluster cluster module) The background No downtime is required for maintenance and upgrade. Launch scripts with Ubuntu and CentOS Stop unstable processes (avoid infinite loops) Console check Provide HTTP API Remote control and real-time interface API (Nodejs module, allowing interaction with PM2 process manager)
1. Install PM2
1. Install nodejs
sudo apt-get install -y nodejs
2. Install pm2
sudo npm install pm2 -g
2. Deploy the django project using PM2
Create a file start.sh in the root directory of django
python manage.py runserver 0.0.0.0:9000
2. Use pm2 to start this file
pm2 start start.sh
3, Check the startup status of the service pm2 list
(django_env) root@iZ941w016mwZ:/etc/nginx# pm2 list
┌ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
│ Name │ id │ mode │ status │ ↺ │ cpu │ memory │
├ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤
│ start │ 0 │ N/A │ fork │ online │ 690 │ 0% │ 2.7 MB │
└ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
Use `pm2 show <id|name>` to get more details about an app
(django_env) root@iZ941w016mwZ:/etc/nginx#
4, Client connection ip address: port number (9000)
3. Set agent to port 9000 in combination with nginx
1, first install nginx according to the previous method
2, Go to nginx package file (/etc/nginx) and modify nginx.conf
...
server {
listen 80;
server_name www.yst168.cn yst168.cn http://www.yst168.cn; # This is your domain name
location / {
proxy_pass http://localhost:9000; # Agent to your local project
}
}
...
3. Restart nginx server
sudo service nginx restart
4. Supplements to basic commands for pm2