天天看點

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

①、安裝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 [email protected]                

把公鑰加入到Heroku

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

如果想删除公鑰,可以用指令:

檢視公鑰,用指令:

③、現在就可以建立一個項目了(預先要安裝好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: [email protected]
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 [email protected]:vivid-mountain-91.git
* [new branch]      master -> master                

⑥、再做一個資料庫遷移:

現在就可以像平常使用Rails一樣編寫程式了。

更新代碼可以用:

将本地資料更新到Heroku網站上:

要打開網頁浏覽,就可以用

備注:

因為heroku是用postgresql作為資料庫,而且css編譯時會用到gem,是以記得在production環境中加入pg和rails_12factor這兩個gem,如下:

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

版權聲明:本文為CSDN部落客「weixin_33754913」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_33754913/article/details/92615317