问题
I am trying to setup Meteor to use pm2 (https://github.com/Unitech/pm2) instead of forever as the node process monitor. I have had no luck in getting the environment variables that a Meteor application needs to be seen by 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 my Meteor application is complaining that it cannot find MONGO_URL
.
Is there a specific way that I need to do the exports in order to work with pm2?
回答1:
You can create process.json
(PM2 fleet configuration file) 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/"
}
}
]
}
for start: pm2 start processes.json
回答2:
Go and checkout pm2-meteor. Should help you with generating a pm2-env.json.
$ npm i -g pm2-meteor
$ cd myMeteorProject
$ pm2-meteor --settings meteor-settings.json
回答3:
as of 2018
I faced a big issue like this but now everthing is solved.
When you are using PM2 for your customised production of Meteor App, then you just place a file process.json
inside bundle
directory and run below command,
pm2 start process.json
Below are the parameters of file process.json
that I wanted to pass as METEOR_SETTINGS
in the 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 above code you can see how I have set METEOR_SETTINGS
.
回答4:
Its a bit weird with pm2. But something like this can work
pm2 kill
MONGO_URL="mongodb://localhost:27017/meteor" PORT=4000 ROOT_URL="https://beta.example.com/" pm2 start app.js --name MyMeteorApp
It happens because pm2 wraps everything into a new process of its own which can't see the variables of the original environment. I think theres also a way to put environment variables in the JSON file but i'm not sure how exactly as the docs are a bit empty
回答5:
After doing some digging I found the right answer. In pm2 anything that you place inside the JSON task definition that isn't one of the reserved keywords is exported to the process that you are running inside of pm2 as an environment variable.
来源:https://stackoverflow.com/questions/20522551/pm2-meteor-environment-setup