[Rails] 使用 Vagrant 快速 Deploy 至 Linode
本文使用 Mac OS X EI Capitan 10.11.6 / Ruby 2.3.1 / Rails 5.0.0
前言
本文有以下幾個重點:
- 安裝相關套件
- Vagrant在本機使用
- Vagrant發佈至Linode
- 其他方便工具介紹
安裝相關套件
第一步請先安裝VirtualBox及Vagrant,請參考下面網頁步驟安裝
接下來再安裝以下兩個Vagrant Plugin
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-librarian-chef-nochef
使用Vagrant在本機開發專案
先到你的專案目錄下,開始使用vagrant,並安裝Cheffile
vagrant init
touch Cheffile
現在目錄下會發現,產生了兩個新檔案:Vagrantfile、Cheffile
我們先設定Cheffile
site "http://community.opscode.com/api/v1"
cookbook 'apt'
cookbook 'build-essential'
cookbook 'mysql', '5.5.3'
cookbook 'ruby_build'
cookbook 'nodejs'
cookbook 'rbenv', git: 'https://github.com/aminin/chef-rbenv'
cookbook 'vim'
再設定Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
config.vm.network :forwarded_port, guest: 3000, host: 3000
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ["cookbooks", "site-cookbooks"]
chef.add_recipe "apt"
chef.add_recipe "nodejs"
chef.add_recipe "ruby_build"
chef.add_recipe "rbenv::user"
chef.add_recipe "rbenv::vagrant"
chef.add_recipe "vim"
chef.add_recipe "mysql::server"
chef.add_recipe "mysql::client"
chef.json = {
rbenv: {
user_installs: [{
user: 'vagrant',
rubies: ["2.2.1"],
global: "2.2.1",
gems: {
"2.2.1" => [
{ name: "bundler" }
]
}
}]
},
mysql: {
server_root_password: ''
}
}
end
end
設定完成後即可開始使用,up的指令會把虛擬機架起來, 首次up的時候會下載iso檔,需要等久一點
vagrant up
vagrant provision
ssh連至虛擬機
vagrant ssh
補安裝一些東西
rbenv install 2.3.1
rbenv global 2.3.1
gem install bundler
這時候專案目錄會在/vagrant,先到此目錄bundle安裝相關套件
cd /vagrant
rbenv rehash
bundle
rails db:migrate
rails s -b 0.0.0.0
這時候可以使用http://localhost:3000/查看
使用Vagrant發佈專案至Linode
- gem Vagrant-linode
留言