与Heroku一起使用PM2
本节指导你一步步将PM2与Heroku集成。
我们会用到Git和Heroku CLI。
准备应用
配置ecosystem.config.js
创建ecosystem.config.js:
pm2 init
修改ecosystem.config.js模版:
module.exports = {
apps : [{
name: "app",
script: "./app.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
学习更多关于生态系统文件。
我们建议在Heroku中使用集群模式,因为每个dyno有多核CPU。
学习更多关于集群模式。
安装PM2
安装PM2到项目依赖:
npm install --save pm2
# with yarn
yarn add pm2
配置package.json
修改package.json中的scripts字段的start脚本:
"scripts": {
"start": "pm2-runtime start ecosystem.config.js --env production"
}
使用Heroku部署
创建Heroku账户
安装CLI
然后,运行heroku login将CLI连接到你的帐户。
初始化Heroku应用
我们先创建一个空的Heroku应用,并且关联一个空的Git仓库。
在应用的根目录下运行:
heroku create
Creating app... done, ⬢ guarded-island-32432
https://guarded-island-32432.herokuapp.com/ | https://git.heroku.com/guarded-island-32432.git
现在你有了一个名为heroku的git remote。 如果你有push到这个远程仓库,你的代码会自动部署在给定的URL。
在Heroku上部署
添加并提交你所有的改动,然后运行:
git push heroku master
Initializing repository, done.
updating 'refs/heads/master'
remote: Compressing source files... done.
remote: Building source:
...
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/aqueous-temple-78487.git
准备好了
这样就结束了,部署的最后一行会给你应用程序可用的URL。
下一步
完成生态系统文件配置
使用PM2 Plus在仪表板上监控你的应用
问题
我们很乐于帮你解决你可能遇到的问题。搜索或查看FAQ。你也可以在PM2的GitHub仓库提交问题或评论。