Setting up the PM2 + environment

I am trying to configure Meteor to use pm2 ( https://github.com/Unitech/pm2 ) instead of forever working as a node process monitor. I’m out of luck with the environment variables that Meteor should see in the pm2 process.

Here is my process:

export MONGO_URL="mongodb://localhost:27017/meteor" export PORT=4000 export ROOT_URL="https://beta.example.com/" pm2 start main.js --name MyMeteorApp 

In the error log from pm2, I see that the Meteor application complains that it cannot find MONGO_URL .

Is there any specific way I need to do for export in order to work with pm2?

+7
meteor
source share
5 answers

After some digging, I found the correct answer. In pm2, everything that you put inside the JSON task definition, which is not one of the reserved keywords, is exported to the process that you use inside pm2 as an environment variable.

0
source share

You can create a process.json configuration process.json (PM2 fleet configuration file) where you can specify environment variables.

For example:

 { "apps": [ { "name": "MyMeteorApp", "script": "./main.js", "log_date_format": "YYYY-MM-DD", "exec_mode": "fork_mode", "env": { "PORT": 4000, "MONGO_URL": "mongodb://localhost:27017/meteor", "ROOT_URL": "https://beta.example.com/" } } ] } 

to start: pm2 start processes.json

+7
source share

A bit strange with pm2. But something like this might work

 pm2 kill MONGO_URL="mongodb://localhost:27017/meteor" PORT=4000 ROOT_URL="https://beta.example.com/" pm2 start app.js --name MyMeteorApp 

This is because pm2 wraps everything in a new native process that the source environment variables cannot see. I think theres also a way to put environment variables in a JSON file, but I'm not sure how exactly the documents are a bit empty

+1
source share

Go and check out the pm2 meteor. Should help you create pm2-env.json.

 $ npm i -g pm2-meteor $ cd myMeteorProject $ pm2-meteor --settings meteor-settings.json 
+1
source share

since 2018

I faced such a big problem, but now everything is solved.

When you use PM2 for your custom Meteor App development, simply place the process.json file inside the bundle and run the command below.

 pm2 start process.json 

The following are the parameters for the process.json file that I wanted to pass as METEOR_SETTINGS in a production environment,

 { "apps": [ { "name": "My APP", "script": "./main.js", "log_date_format": "YYYY-MM-DD", "exec_mode": "fork_mode", "env": { "PORT": 3000, "MONGO_URL": "mongodb://username: password@127.0.0.1 :27017/dbname", "ROOT_URL": "http://hostname/", "METEOR_SETTINGS": { "MAIL_URL": "smtps://<your username>:<your password>@smtp.gmail.com:465", "AUTHKEY": "185938A0asmD231231231231e4992", "HOSTNAME": "some example", "public": { "COMPANY_NAME": "Some Company Name" } } } } ] } 

So, in the above code, you can see how I set METEOR_SETTINGS .

0
source share

All Articles