・準備:VirtualBoxと Vagrantのインストール
Vagrantとは何か
仮想マシンを簡単に立ち上げるツール
ローカル開発環境を整えるために仮想マシンを手作業でやっていたことをVagrantで1回設定してしまえば、何度でも同じ構成でマシンを立ち上げることができる。
VagrantはRubyで書かれている。
仮想マシンを起動するまでのながれ
- Boxの取得
- 仮想マシンの初期化
- 起動
1. Boxの取得
# Boxの取得
vagrant box Boxの名前 Vagrantboxの公式サイトにあるURL
# Boxリストの確認
vagrant box list
# boxの削除
vagrant box remove
# vagrantのコマンド確認
vagrant box -h
2. 仮想マシンの初期化
1つのテンプレートからいくつでも仮想マシンを作ることができる。
# 仮想マシンごとにディレクトリを作成
mkdir ディレクトリ名
# そのディレクトリに入る
cd ディレクトリ名
# 初期化
vagrant init Boxの名前
初期化するとVagrantfileというファイルができる。
中身はRubyの設定ファイルになっており、仮想マシンを設定できる。
3. 起動
# 仮想マシンの立ち上げ
vagrant up
時間がかかるが、VisualBoxで仮想マシンが立ち上がっていることがわかる。
これでBoxから仮想マシンを作ることができた。
仮想マシンの停止・再起動・削除
# 仮想マシンの状態をみる
vagrant status
# 仮想マシンの復帰
vagrant resume
# 仮想マシンのスリープ
vagrant suspend
# 仮想マシンの立ち上げ
vagrant up
# 仮想マシンの終了
vagrant halt
# 仮想マシンの再起動
vagrant reload
# 仮想マシンの削除
vagrant destroy
仮想マシンへの接続
# 仮想マシンへの接続
vagrant ssh
webページを作って、ブラウザを見る
# webサーバのインストール
sudo yum -y install httpd
# webサーバの立ち上げ
sudo service httpd start
# 仮想マシンを再起動してもwebサーバが立ち上がるようにする
sudo chkconfig httpd on
# ローカル環境なのでファイアウォールを切る
sudo service iptables stop
# 仮想マシンを再起動してもファイアウォールを立ち上げないようにする
sudo chkconfig iptables off
# CentOS だと「/var/www/html」に Web ページを置くのでここに移動
cd /var/www/html
# index.htmlを作成
sudo vi index.html
# index.html内に<h1>Hello world!</h1>と入力
# ここで仮想マシンから出る
Vagrantfileの中にネットワークの設定があるので設定を変える。
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"
:private_network のコメントを外して192.168.33.10でアクセスできるようにする。
# 設定を変えたので仮想マシンを再起動
vagrant reload
ブラウザで192.168.33.10とするとHello World!と表示される。
共有フォルダ
前回は仮想マシン上でindex.htmlを編集しましたが、windows上で編集できるようにします。
共有フォルダの設定は
windows内のVagrantfileがあるフォルダとvagrant sshしたときの「/vagrant」フォルダとリンクしている。
Provisioningについて
Provisioningとは「vagrant up」をしたあとに、自動的に実行される一連の処理のこと
ProvisioningするにはVagrantfileの中で設定していきます。
今回は先ほどしたwebサーバの設定をしていきます。
config.vm.provision :shell, :path => "provision.sh"を追加してprovision.shというファイルを別途作る。
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos64"
config.vm.provision :shell, :path => "provision.sh"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# 保存したあとにファイルを作る
vi provision.sh
# webサーバのインストールの設定
sudo yum -y install httpd
sudo service httpd start
sudo chkconfig httpd on
# Provisionだけ再実行する
vagrant provision
これでwebサーバを手動でインストールしなくても自動的にインストールされる。
自作のBoxを作成
# 現在の状態をBox化
vagrant package
# lsとするとpackage.boxができている。
# これをシステムに追加
vagrant box add boxの名前 package.box
pluginでの導入の仕方
# 現在の状態をBox化
vagrant plugin install pluginの名前
# 導入されているリストを見れる
vagrant plugin list
# pluginのアンインストール
vagrant plugin uninstall pluginの名前
# plugin関係のコマンド確認
vagrant plugin -h