2015年、明けましておめでとうございます!
正月から、どんどん新しいことにトライしていこうと思います。
早速、いまさらではありますが、Windows7 PCにて、Vagrant環境を構築します。
とっかかりとして、仮想OSは、CentOS7(minimal)とし、簡単にWeb Serverを立ち上げてみることにしました。
(1) インテルVT機能の有効化
- CentOS7は64ビットなので、VT機能を有効しなければ仮想OSの起動に失敗してしまいますので、BIOSの設定にてVT機能を有効にしました。
BIOS設定
Intel Virtualization Technogy : Disabled => Enabled
(2) VirtualBoxインストール
- Oracle VM VirtualBoxダウンロードサイトより、今回、「VirtualBox-4.3.20-96997-Win.exe」をダウンロードしました。インストールは、デフォルトのまま行いました。
(3) Vagrantインストール
- VAGRANT公式サイトより、「vagrant_1.7.1.msi」をダウンロードしました。
- 個人的な設定として、Destination Folderを「C:\Vagrant」としています。
(4) Vagrant導入確認
> vagrant --version Vagrant 1.7.1
(5) boxの追加(CentOS7 minimal)
- boxは、仮想OSのイメージ・ファイルです。
- Vagrant boxにて、取得先のリンクをコピーしましょう。
> vagrant box add centOS7min https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box ==> box: Adding box 'centOS7min' (v0) for provider: box: Downloading: https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box box: Progress: 100% (Rate: 3990k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'centOS7min' (v0) for 'virtualbox'!
boxの保存場所
C:\Users\userName\.vagrant.d\boxes
(6) 仮想マシン初期化
- 事前にフォルダを作成しておき、その上で初期化します。
> cd C:\Vagrant\hosts\centOS7min > vagrant init centOS7min 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.
(7) 仮想マシンのIPアドレス設定
- (6)で作成されたVagrantfileを編集します。メモ帳などで開き、1行のコメント行を有効にします。
# using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" <<< 有効化(#を削除する)
(8) 仮想マシンを起動
- vagrant upコマンドにて、仮想OSを起動します。
> cd C:\Vagrant\hosts\centOS7min > vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if its present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => C:/Vagrant/hosts/centOS7min Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant The error output from the last command was: /sbin/mount.vboxsf: mounting failed with the error: No such device c:\Vagrant\hosts\centOS7min>
- 何だか、mountエラーが出ていますが・・・とりあえず、起動に成功できました!
- 【追記】vagrant upでmountエラーがでる場合の対処法でご紹介されている方法で、この問題は解決できました。感謝です!ありがとうございます!
# /etc/init.d/vboxadd setup Removing existing VirtualBox non-DKMS kernel modules [ OK ] Building the VirtualBox Guest Additions kernel modules Building the main Guest Additions module [ OK ] Building the shared folder support module [ OK ] Building the OpenGL support module [ OK ] Doing non-kernel setup of the Guest Additions [ OK ] Starting the VirtualBox Guest Additions [ OK ]
(9) SSHログイン
- Tera Termを使ってSSHログインします。
host: 127.0.0.1 port: 2222 user: vagrant pass: vagrant
(10) ファイアウォール無効化
# systemctl list-unit-files | grep firewall firewalld.service enabled # systemctl disable firewalld.service rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service' rm '/etc/systemd/system/basic.target.wants/firewalld.service' # systemctl list-unit-files | grep firewall firewalld.service disabled
(11) httpdインストール・起動
httpdインストール
# yum -y install httpd
インストール結果
Installed: httpd.x86_64 0:2.4.6-18.el7.centos Dependency Installed: httpd-tools.x86_64 0:2.4.6-18.el7.centos mailcap.noarch 0:2.1.41-2.el7
httpdの起動
# systemctl start httpd.service
httpdの有効化
# systemctl enable httpd.service
(12) 仮想OS再起動
# shutdown -r now
(13) ブラウザにて仮想OSのHTTP serverへアクセス
- 192.168.33.10へアクセスしてみます。無事、成功しました!
(おまけ)よく使うコマンド
仮想マシンの終了
vagrant halt
仮想マシンの再起動
vagrant reload
仮想マシンの状態確認
vagrant status
仮想マシンの削除
vagrant destroy
box一覧の確認
vagrant box list
boxの削除
vagrant box remove [box名]