How to run npm start using PM2 on CentOS
To run npm start
using PM2 on CentOS, you can follow these steps:
-
Open a terminal or SSH into your CentOS server.
-
Install Node.js and npm if they are not already installed. You can use a package manager like
yum
to install them:sudo yum install -y nodejs
-
Install PM2 globally:
sudo npm install -g pm2
-
Navigate to the directory where your Node.js project is located. For example:
cd /path/to/your/node/project
-
Run the following command to start your Node.js application with PM2:
pm2 start npm --name "myapp" -- start
Replace
"myapp"
with the desired name for your PM2 process. This command tells PM2 to usenpm
as the command andstart
as the argument for running your application.Note: The double dashes (
--
) are important to separate the PM2 options from the npm command and its arguments. -
PM2 will start your Node.js application as a background process and give it the name you specified. You can view the status of your PM2 processes by running:
pm2 list
-
To monitor the logs of your application, you can use the command:
pm2 logs myapp
Replace
"myapp"
with the name you assigned in step 5.
That's it! Your Node.js application should now be running using PM2 on CentOS. PM2 will automatically restart your application if it crashes or if the system restarts.