Total Pageviews

Sunday 6 May 2018

在Heroku上部署Rails App

https://devcenter.heroku.com/articles/getting-started-with-rails5
在Heroku平台上部署非常简单,而且Heroku提供5个免费的App部署名额,所以如果觉得常规部署Rails应用的方式太过麻烦,那么Heroku是个比较好的选择,那么Heroku部署虽然简单,但是还是有一些要注意的细节,在此记录下来。
部署前的准备工作
Heroku对于本地环境的要求:
  1. 免费的Heroku Account
  2. 本地ruby版本 >= 2.2.5
  3. 本地需要安装bundler gem install bundler
部署之前,首先要安装CLI(Heroku Command Line Interface), 以Ubuntu为例:
# Run this from your terminal.
# The following will add our apt repository and install the CLI:
sudo add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./"
curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install heroku
然后修改你的Gemfile
# 首先加上ruby版本的指定
ruby '2.3.1'
# 然后修改数据库(如果你的开发环境使用的是sqlite的话, 如果使用sqlite3会出现app crashed error), 删除
gem 'sqlite3'
# 加上
group :test, :development do
  gem 'sqlite3'
end
group :production do 
  gem 'pg'
end
然后 bundle install 生成新的Gemfile
至于为什么不能在生产环境使用sqlite3数据库,参见教程
下面开始部署的步骤
  1. 在Heroku服务器上创建一个App heroku create myApp (如果没有指定App的名称,Heroku会帮你生成一个随机的名称)
  2. 将本地的Git项目提交到Heroku的服务器 git push heroku master
  3. 现在项目已经部署好了,需要进行数据库的迁移
heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:seed
  1. 在项目的根目录创建Procfile(Procfile的作用是声明启动应用需要执行的命令),如:
    web: bundle exec rails server -p 3000
    注意:文件名只能是Procfile,像Procfile.txt这种都不行,另外,必须创建在项目根目录下!
  2. 至此,项目已经部署完毕,可以执行 heroku open 访问
CLI的一些命令
  1. 打开生产环境的Rails Console heroku run rails c
  2. 查看正在运行的dyno(dyno可视为Procfile中指定的命令的一个轻量级的容器) heroku ps
  3. 查看日志 heroku logs --tail
  4. 查看Heroku config variable heroku config --app myApp
  5. 删除Heroku上的App Appheroku apps:destroy --app example
链接:https://www.jianshu.com/p/867848205956
 
https://www.jianshu.com/p/9ddb636c79cc

No comments:

Post a Comment