LoginSignup
2

More than 5 years have passed since last update.

Vagrantを使ってSinatraを秒で試す

Last updated at Posted at 2017-04-22

概要

VagrantでSinatraを動かすための数ステップの備忘録

本編

0. 1行でまとめると

Vagrantfileにconfig.vm.network "private_network", ip: "*:*:*:*"書いて,サーバ起動時にruby main.rb -o *:*:*:*すればホストからアクセスできる.

1. Vagrantで仮想マシンをつくる

仮想マシンの概要を書く
vagrant init ubuntu/trustyしたときにできるVagrantfileにconfig.vm.network "private_network", ip: "192.168.33.10"を追記しただけです.

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "private_network", ip: "192.168.33.10"
end

起動してSSHで入る.(Windowsの人はPuTTYとかで)

vagrant up
vagrant ssh

updateとかもしておく.

sudo apt-get update
sudo apt-get upgrade

2. Sinatraのインストール

gem install sinatra
gem install sinatra-contrib

3. 起動

Sinatraのメインのファイルを書く.

main.rb
require 'sinatra'

get '/' do
  'hello world'
end

起動する.

ruby main.rb -o 192.168.33.10

ブラウザで以下のアドレスにアクセスして'hello world'が出たらOK
http://192.168.33.10:4567

4. 追記

set :environment, :productionを追記するだけで-oオプションがなくてもよくなる.

main.rb
require 'sinatra'

set :environment, :production

get '/' do
  'hello world'
end

宣伝

フォローお願いします:dancer_tone4: @redshoga

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2