LoginSignup
472
479

More than 5 years have passed since last update.

Vagrant セットアップ (Mac)

Last updated at Posted at 2013-11-16

1. Vagrant のインストール

公式サイト
http://www.vagrantup.com/

上記公式サイトから、 vagrant_1.8.1.dmg をダウンロードし インストール

$ vagrant --version

Vagrant 1.8.1

2. VirtualBox を公式サイトからインストール

VirtualBox 公式サイト
https://www.virtualbox.org/

VirtualBox-5.0.20-106931-OSX.dmg をダウンロードし インストール

3. 初期化と起動

Centos6.7の例です。

$ mkdir -p ~/Vagrant/CentOS67/
$ cd ~/Vagrant/CentOS67/

$ vagrant init bento/centos-6.7
$ vagrant up --provider virtualbox

その他のBoxは下記にあるので、他のBoxを探して利用することも可能です。
https://atlas.hashicorp.com/boxes/search

例)ubuntuの例

$ vagrant init ubuntu/trusty64
$ vagrant up --provider virtualbox

以前からある vagrantbox.es からBoxを探して利用することも可能です。
http://www.vagrantbox.es/

例)

$ vagrant box add centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

$ mkdir -p ~/Vagrant/CentOS65
$ cd ~/Vagrant/CentOS65

$ vagrant init centos65
$ vagrant up

4. 仮想マシンの操作

起動するには、up

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

[default] VirtualBox VM is already running.

すでに起動してます。

状態を見るには、 status

$ vagrant status

default                   running (virtualbox)

一時停止するには、suspend

$ vagrant suspend

[default] Saving VM state and suspending execution...

5. 仮想マシンへの接続

$ vagrant ssh

Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 

6. ネットワークの設定

$ vim Vagrantfile
# config.vm.network :private_network, ip: "192.168.33.10"
 ↑ ここのコメントアウトを削除

vagrant のリロード

$ vagrant reload

これで、192.168.33.10 というIPでアクセスできる

% ping 192.168.33.10
PING 192.168.33.10 (192.168.33.10): 56 data bytes
64 bytes from 192.168.33.10: icmp_seq=0 ttl=64 time=0.383 ms
64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=0.624 ms
64 bytes from 192.168.33.10: icmp_seq=2 ttl=64 time=0.794 ms

7. プロビジョニング

Vagrantには chef や ansible のようなサーバーの初期設定をするしくみが備わっています。
大きく分けて、Vagrantfile に直接記述する inlineの方法と、外部ファイル(shell script)に記述する方法があります。

7.1 プロビジョニング/inlie

Vagrantfile に直接記述します。

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: <<-SHELL
     sudo yum update
     sudo yum install -y git screen tree wget
  SHELL
end

変更したら、provisionを実行

$ vagrant provision

7.2 プロビジョニング/外部ファイル

外部のshell scriptを実行します。

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "script.sh"
end

このscript.sh はホストOS側にあります。

7.3 その他

他にも、指定したURLのスクリプトを実行したり、いろいろできます。
詳しくはこちら
https://www.vagrantup.com/docs/provisioning/shell.html

補足

Vagrantのsharedfolder使ってて良くはまるのが、CSSとかJSとかHTMLといった静的なコンテンツを更新しても
反映されない事があります。
そんな現象が起きた時は下記を参考にしてapacheやnginxの設定を見直すと良いかもです。

VagrantでCSSの更新が反映されない場合の対処法
http://qiita.com/shoyan/items/12389d5beaa8695b1a53

472
479
0

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
472
479