Skip to content

How to serve with PM2

Louis Cruz edited this page Aug 5, 2016 · 2 revisions

#How to serve with PM2

##Introduction Once you want your project to be served in production, you will want to ensure that it is continuously served. There are two issues with making this happen:

  1. Starting a server through npm start might be fine (aside from non-production ready code), but the process would cease when the shell is closed.

  2. In many application setups, you can expect there to be a single file, such as app.js to run. In these cases, node app.js might do the trick. However, Webpack divvies up our dependencies. Because of this, we will actually need to run our server process on the whole /dist folder once it has been built.

With these two things in mind, let's get started.

##Build the application

Before we begin, you will need to build the application. On your server, run:

npm run build:prod

##Install PM2 on server

To install PM2, run the following on your server:

npm install pm2 -g

##Finally, run PM2

Now you can serve your application with PM2. To serve your application on port 80, enter the following:

pm2 start http-server --name app -- dist -p 80

In this command, you are using PM2 to start an instance of the http-server where the name is app. Everything after the -- is an argument passed into that http-server instance. By placing dist in this command, you are asking it to serve the dist folder. By putting -p 80, you are asking it to use port 80.

##Optional

At this point, you'll probably want a web server to catch the port and serve it. It is fairly simple to set up an nginx server to proxy_pass your traffic to the appropriate location and port.