web-technical-blog

web開発に関する技術メモ

WindowsにVagrantを入れたときの設定手順メモ

Windows上で、Git-bashを使います。その他のクライアントソフトでもOKです。
https://git-scm.com/downloads

VirtualBoxインストー
OSに合ったVirtualBoxをダウンロードしてインストー
https://www.virtualbox.org/wiki/Downloads

Vagrantのインストー
OSに合ったVagrantをダウンロードしてインストー
https://www.vagrantup.com/downloads.html

$ vagrant box add {title} {url}
$ vagrant init {title}
$ vagrant up

VirtualBoxにboxファイルを登録する

$ vagrant box add centos6-7 https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos6-7' (v0) for provider:
box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
box:
==> box: Successfully added box 'centos6-7' (v0) for 'virtualbox'!

▼他のOSの場合は、下記から選択する
http://www.vagrantbox.es/

■boxのダウンロードが終わったら、下記コマンドで利用可能なbox一覧を表示

$ vagrant box list
centos6-7 (virtualbox, 0)

■Vagrantfileの作成

$ vagrant init centos6-7

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

$ vi Vagrantfile
# VMのホスト名
config.vm.hostname = "centos6-7"
# VMのIPアドレスを設定
config.vm.network "private_network", ip: "192.168.33.10"
# GUIモードの設定をONにする場合は設定(不要ならコメントアウトのままでOK)
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
vb.name="192.168.33.10"
end

VM起動

$ vagrant up
初期パスワード user:vagrant, password:vagrant
sudo su - でrootになれる。

SSHで接続する場合
vargrant upで起動した後で、
Vagrantfileがあるディレクトリで、下記コマンドで接続することも可能

$ vagrant ssh

Teratermなど、他のクライアントソフトで接続する場合は、
Vagrantfileのconfig.vm.networkで設定したIPアドレスで接続できる。

$ ssh -lvagrant 192.168.33.10

# エラーが発生する場合は、鍵を作成する
ssh-keygen -R 192.168.33.10

# 192.168.33.10
vi ~.ssh/authorized_keys/id_rsa.pub
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxx

■フォルダ共有
CentOSの特定ディレクトリをWindowsと共有することができる

■共有フォルダの指定

$ vi Vagrantfile
# config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.synced_folder "../hello", "/var/www/html/hello"

■ソースフォルダ・ファイルの作成

C:¥Users¥xxxx¥hello¥index.html
<h1>Hello Vagrant!</h1>

■設定の再読み込み

vagrant reload

■フォル共有の確認

[vagrant@localhost hello]$ ls -l /var/www/html/hello/
total 1
-rwxrwxrwx 1 vagrant vagrant 23 Dec 14 07:45 index.html

■Webサーバーの起動
CentOSにログインし、httpd(Apache)をインストール、起動する。

[vagrant@vagrant-centos65 ~]$ sudo yum -y install httpd
[vagrant@vagrant-centos65 ~]$ sudo chkconfig httpd on
[vagrant@vagrant-centos65 ~]$ sudo service httpd start

■コマンドメモ

vagrant box add [box名] [boxファイル]: ローカルにボックスファイルをダウンロードする
vagrant box list: box一覧確認
vagrant box remove [box名]
vagrant init box名: Vagrantfileの作成
vagrant up: VagrantfileからVMを起動する
vagrant status: 起動しているVM一覧確認
vagrant halt: VM停止
vagrant destroy: VM削除(Vagrantfileのあるディレクトリで行う)
vagrant ssh: 簡単にSSHログイン出来る

■VagrantFileのサンプル

Vagrant.configure("2") do |config|
    config.vm.box = "bento/centos-6.8"
    config.vm.hostname = "192.168.56.200"
    config.ssh.insert_key = false
    config.vm.network "private_network", ip: "192.168.56.200"
    config.vm.provider "virtualbox" do |vb|
    vb.name = "hostname"
    vb.memory = "2048"
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
    vb.customize ["setextradata", :id, "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled", 0]
end

config.vm.provision "shell", inline: <<-SHELL

yum update -y
yum install -y zsh vim tree telnet dstat git tig

SHELL

end

▼参考URL
qiita.com