环境切换
正式开发中分为不同的环境(开发环境、测试环境、生产环境),我们需要根据不同的情景来切换各种环境
pm2通过在配置文件中通过env_xx
来声明不同环境的配置,然后在启动应用时,通过--env
参数指定运行的环境
环境配置定义,在应用中,可以通过process.env.REMOTE_ADDR等来读取配置中生命的变量:
"env": { "NODE_ENV": "production", "REMOTE_ADDR": "http://www.example.com/" }, "env_dev": { "NODE_ENV": "development", "REMOTE_ADDR": "http://wdev.example.com/" }, "env_test": { "NODE_ENV": "test", "REMOTE_ADDR": "http://wtest.example.com/" }
启动指定的环境:pm2 start app.js --env development
pm2会自动在process.env 设置一些参数,比如process.env.name 就是pm2启动的appname
其他参数参考如下:
{
YARN_VERSION: '1.6.0',
HOSTNAME: '952779b5ff4e',
PWD: '/app/yunsiweiservice',
HOME: '/root',
NODE_VERSION: '10.1.0',
'affinity:container': '=bd260641c08bb00736d69b225ec0977ddf3fbed3604bda36d593e2eb29890469',
TERM: 'xterm',
SHLVL: '1',
PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
_: '/usr/local/bin/pm2',
PM2_USAGE: 'CLI',
PM2_HOME: '/root/.pm2',
SILENT: 'true',
windowsHide: 'true',
username: 'root',
instance_var: 'NODE_APP_INSTANCE',
exec_mode: 'cluster_mode',
watch: 'false',
treekill: 'true',
autorestart: 'true',
automation: 'true',
pmx: 'true',
vizion: 'true',
instances: '1',
name: 'yunsiweiservice',
args: '-f,-P,8052,-p,/app/yunsiweiservice',
node_args: '',
pm_exec_path: '/usr/local/lib/node_modules/visop/bin/visop-start',
env: '[object Object]',
pm_cwd: '/app/yunsiweiservice',
exec_interpreter: 'node',
pm_out_log_path: '/root/.pm2/logs/yunsiweiservice-out-11.log',
pm_err_log_path: '/root/.pm2/logs/yunsiweiservice-error-11.log',
pm_pid_path: '/root/.pm2/pids/yunsiweiservice-11.pid',
km_link: 'false',
vizion_running: 'false',
NODE_APP_INSTANCE: '0',
status: 'launching',
pm_uptime: '1529724769233',
axm_actions: '',
axm_monitor: '[object Object]',
axm_options: '[object Object]',
axm_dynamic: '[object Object]',
created_at: '1529724769233',
pm_id: '11',
restart_time: '0',
unstable_restarts: '0',
_pm2_version: '2.10.4'
}