在Heroku上部署(托管)Rails项目

①、安装heroku:

$ gem install heroku

②、先要生成一个公钥,使用命令:

$ ssh-keygen -t rsa
 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
a6:88:0a:bb:74:70:c6:e0:d5:49:65:e3:04:d5:6c:3e user@workstation.local

把公钥加入到Heroku

$ heroku keys:add
Uploading ssh public key /home/user/.ssh/id_rsa.pub

如果想删除公钥,可以用命令:

$ heroku keys:remove user@workstation.local

查看公钥,用命令:

$ heroku keys


③、现在就可以创建一个项目了(预先要安装好rails)。

$ rails myapp            (如果是已有的应用,这一步可以跳过)
$ cd myapp
$ git init
$ git add .
$ git commit -m "my first commit" 
Created initial commit 5df2d09: my first commit
44 files changed, 8393 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 Rakefile
create mode 100644 app/controllers/application.rb
...



④、把这个项目创建到Heroku上。

$ heroku create myapp      
Enter your Heroku credentials.
Email: user@example.com
Password: 
Uploading ssh public key /home/user/.ssh/id_rsa.pub

(当第一次运行heroku命令时,要求输入Email,password,这个是你在Heroku注册的Email和密码,以便创建~/.heroku/credentials文件,这个文件记录了你输入的用户名和密码,以后就不再需要输入了。)



⑤、接下来运行:

$ git push heroku master
 
Counting objects: 65, done.
Compressing objects: 100% (58/58), done.
Writing objects: 100% (65/65), 80.54 KiB, done.
Total 65 (delta 14), reused 0 (delta 0)
-----> Heroku receiving push
-----> Rails app detected
       Compiled slug size is 0.1MB
-----> Launching....... done
       App deployed to Heroku
To git@heroku.com:vivid-mountain-91.git
* [new branch]      master -> master



⑥、再做一个数据库迁移:

$ heroku run rake db:migrate


现在就可以像平常使用Rails一样编写程序了。

更新代码可以用:

$ git push heroku master

将本地数据更新到Heroku网站上:

要打开网页浏览,就可以用

$ heroku open


备注:

因为heroku是用postgresql作为数据库,而且css编译时会用到gem,所以记得在production环境中加入pg和rails_12factor这两个gem,如下:

group :production do
 gem 'pg'
 gem 'rails_12factor'
end


转载于:https://my.oschina.net/H7QMCSeOLOxu/blog/184227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值