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

pm2 start app.js # Start the app.js application pm2 app.js -i 4 # cluster mode mode starts 4 application examples of app.js pm2 start app.js --name= "api" # Start the application and name it "api" pm2 start app.js --watch # Auto restart the application when the file changes pm2 start script.sh # Start bash script pm2 list # List all applications started by PM2 pm2 show [app-name] # Display all information about the application pm2 logs # Displays logs for all applications pm2 logs [app-name] # Shows logs for the specified application pm2 flush pm2 stop all # Stop all applications pm2 stop 0 # Stop the specified application with id 0 pm2 restart all # Restart all applications pm2 reload all # Restart all applications under cluster pm2 gracefulReload all # Graceful reload all apps in cluster mode pm2 delete all # Close and delete all applications pm2 delete 0 # Deletes the specified application id 0 pm2 scale api 10 # Extends the application named api to 10 instances pm2 reset [app-name] # Reset the number of restarts pm2 startup # Create bootstrap command pm2 save # saves the list of current applications pm2 resurrect # Reload the list of saved apps pm2 update # Save processes, kill PM2 and restore processes pm2 generate # Generate a sample json configuration file pm2 start app. js -- -- node - args = "� max - old - space - size = 1024"

Related articles: