In this lesson, you will learn how to configure node apps using pm2 and a json config file.
We're going to take PM2 into a more advanced use case by creating a config file that PM2 will use to launch our apps. To start with, we're going to SSH into our production server. Once in there, I'm going to switch to the node user that's the user that's running our PM2 process.
Inside of my home directory for the node user, I've got two different folders, one for each app that we're going to be running. We can take a look at those if we do a cat on those files. In the output of those two files, we're just creating an HTTP server.
For app one, it's running on port 3000, and then it's just going to return "Hello from App One" to the browser. This is the file for app number two. It's just going to return "Hello from App Two." It's running on port 3001. That's all we're doing there.
We could launch these using the node command, but since we want to use PM2, there's an easier way to do it. That way is by creating a JSON file that contains configuration information for PM2. The name of the file doesn't matter. It can be anything as long as it's a valid JSON file because you're going to pass it in as an argument to the PM2 command.
We're going to start with a set of curly braces, and then we're going to create an apps object inside of there, that is an array of objects. Our first object is going to be app one. We set that up by defining a name object. We're going to call it "App One." That's the name that we will see in the console output of PM2.
We're going to add a script object here. That's going to be the path that PM2 is going to look to to find the code for this app. That's enough to get started. We can do the same thing for app two. I think that looks right. We'll try it out.
If we hit start, you can see that the PM2 daemon has successfully spawned. That's the process that's responsible for keeping all the apps running, and then two processes were launched, one for each app that we defined in our config.
In the table output, you can see each of them listed by name, which is the name that we defined, and the process ID, or PID, that the operating system has defined for that. If we switch over to our browser here and load that up, we get "Hello from App One." In this page, we get "Hello from App Two."