How to serve static file over https in pm2?
Asked Answered
S

1

7

we can serve our static files (like a frontend app) over http with a simple command in PM2 :

pm2 serve <path> <port>

How i can serve static files with SSL using the same command pm2 serve ? is it possible ?

Or any alternatives using PM2 ?

Sympathize answered 23/7, 2020 at 5:40 Comment(2)
You can't do that with pm2. But you can use nginx or apache.Kathrinekathryn
Thats not possible: pm2.keymetrics.io/docs/usage/exposeTravancore
T
1

A workaround to serve a static site with HTTPS using PM2 is to add serve as a dependency to your project, then create a npm script to run serve, and have PM2 run that script instead of serving your site directly.

So, for example:

Add serve to your project:

npm -i serve

Add a script to your package.json, where 'build' is your build directory, and '8080' is the port you want to serve on:

  "scripts": {
    "serve-build": "serve -l 8080 -s build --ssl-cert 'path_to/your_certificate.crt' --ssl-key 'path_to/your_key.key'"
  },

Then instead of calling pm2 serve <path> <port>, you would instead tell PM2 to run npm and point at your script:

pm2 start npm --name your-pm2-process-name -- run serve-build

This will make PM2 run your script, which in turn runs serve which supports https when certificates are supplied. The process will then run in the same manner as if PM2 were serving the static site itself.

Tribe answered 21/9, 2022 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.