こんにちは。
朝食はどっち派?と問われればご飯派、mukaiyachiです。
以前からVagrant(ベイグラント)に興味があったのでちょっと試しに使ってみました。
Vagrantは仮想マシンの作成、セットアップ、破棄等を簡単なコマンドを実行することで操作できるツールです。
Vagrantの良い点は下記のようなものがあります。
- 本番環境と開発環境を統一できる。
- 開発環境をすぐに作ったり壊したりできる
- プロジェクト毎に開発環境を分けることができる。
- 複数人で開発しているとき「自分の環境だと動かないんだけど?」「いや私の環境では普通に動くけど?」のようなことがなくなる。
今回はVagrantを使って仮想マシンの作成、セットアップ、Webサーバを起動してブラウザに画面を表示するというところまでを行ってみました。
それぞれ順番に記していきます。
目次
1、Vagrantのインストール
こちらからダウンロードしてインストールします。
インストールができたらターミナルソフトでvagrantコマンドが使えるか確認してみます。
ここでは「vagrant -v」と入力してVagrantのバージョンを調べています。
vagrantコマンドはCUIでVagrantを操作するためのコマンドです。
Vagrantの全ての操作はこのコマンドで行います。
1 2 |
$ vagrant -v Vagrant 1.7.2 |
2、VirtualBoxのインストール
VirtualBoxはオラクルが開発している仮想化ソフトウェアです。
Vagrantを使う時の仮想化ソフトウェアとしてVirtualBoxが一般的に使用されています。
こちらからダウンロードしてインストールします。
3、Vagrantの初期化
「vagrant init」コマンドで初期化します。
「vagrant init hashicorp/precise32」 と入力して実行。
1 2 3 4 5 |
$ vagrant init hashicorp/precise32 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. |
precise32はUbuntu 32ビットのことで初期化時にそのBoxファイルを追加しました。
Boxファイルは仮想マシンのテンプレートです。
初期化するとコマンドを実行したディレクトリにVagrantfileができます。
Vagrantfileは仮想マシンの構築設定を記述する設定ファイルです。
さらにCentOSなどの別のBoxファイルを追加したい場合は「vagrant box add」コマンドで追加できます。
4、仮想マシンの起動
「vagrant up」コマンドで仮想マシンを起動します。
「vagrant up」と入力して実行。
初回はちょっと時間がかかります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'hashicorp/precise32' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'hashicorp/precise32' default: URL: https://atlas.hashicorp.com/hashicorp/precise32 ==> default: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox default: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box ==> default: Successfully added box 'hashicorp/precise32' (v1.0.0) for 'virtualbox'! ==> default: Importing base box 'hashicorp/precise32'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/precise32' is up to date... ==> default: Setting the name of the VM: user_default_1434055248970_3118 ==> 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: Warning: Remote connection disconnect. 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: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 4.3 ==> default: Mounting shared folders... default: /vagrant => /Users/user |
ちなみに追加されているBoxファイルの一覧を表示するには、「vagrant box list」というコマンドで確認できます。
1 2 |
$ vagrant box list hashicorp/precise32 (virtualbox, 1.0.0) |
また「vagrant status」コマンドで現在の仮想マシンの状態が確認できます。
1 2 3 4 5 6 7 8 9 |
$ vagrant status Current machine states: default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`. |
5、仮想マシンにSSH接続
「vagrant ssh」コマンドで仮想マシンにSSH接続します。
「vagrant ssh」と入力して実行。
1 2 3 4 5 6 7 8 9 |
$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686) * Documentation: https://help.ubuntu.com/ New release '14.04.2 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2 |
6、仮想マシンの停止
「vagrant halt」コマンドで仮想マシンを停止します。
「vagrant halt」と入力して実行。
1 2 |
$ vagrant halt ==> default: Attempting graceful shutdown of VM... |
「vagrant status」コマンドで状態を確認すると停止しているのがわかります。
1 2 3 4 5 6 |
$ vagrant status Current machine states: default poweroff (virtualbox) The VM is powered off. To restart the VM, simply run `vagrant up` |
7、仮想マシンの一時停止、再開
「vagrant suspend」コマンドで仮想マシンの一時停止が行えます。
「vagrant suspend」と入力して実行。
1 2 |
$ vagrant suspend ==> default: Saving VM state and suspending execution... |
「vagrant status」コマンドで状態を確認すると一時停止しているのがわかります。
1 2 3 4 5 6 |
$vagrant status Current machine states: default saved (virtualbox) To resume this VM, simply run `vagrant up`. |
「vagrant resume」コマンドで一時停止していた仮想マシンを再開できます。
8、プロビジョニング
Vagrantは自動プロビジョニングをサポートしていて、この機能を使うこと「vagrant up」実行時にソフトウェアを自動的にインストールして、繰り返し利用可能な状態にしてくれます。
今回は仮想マシンが起動した時にApacheも起動しているようにしてみました。
■bootstrap.shの作成
Vagrantfileがあるフォルダと同じ階層にbootstrap.shを作成。
1 |
vim bootstrap.sh |
中身はこのようにします。
1 2 3 4 5 6 7 |
#!/usr/bin/env bash apt-get update apt-get install -y apache2 if ! [ -L /var/www ]; then rm -rf /var/www ln -fs /vagrant /var/www |
■Vagrantfileの設定を変更
1 |
vim Vagrantfile |
1 2 3 |
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise32" config.vm.provision :shell, path: "bootstrap.sh" |
※ここでは見やすいようにコメント部分は除いてます。
■プロビジョニング
設定をしたら仮想マシンを作ってVagrantfileが自動的にbootstrap.shファイルを処理するようにします。
まだ仮想マシンを起動していない場合は「vagrant up」で起動、すでに起動している場合は「vagrant reload」で再起動します。
今回は「vagrant reload」を使用しました。
「vagrant reload –provision」と入力して実行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
vagrant reload --provision ==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'hashicorp/precise32' is up to date... ==> 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: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 4.3 ==> default: Mounting shared folders... default: /vagrant => /Users/user ==> default: Running provisioner: shell... default: Running: /var/folders/qm/zkfl7hns185_npxpx9ckn7qm0000gn/T/vagrant-shell20150612-24197-1m2mm85.sh ==> default: stdin: is not a tty ==> default: Ign http://security.ubuntu.com precise-security InRelease ==> default: Ign http://us.archive.ubuntu.com precise InRelease ==> default: Ign http://us.archive.ubuntu.com precise-updates InRelease ==> default: Ign http://us.archive.ubuntu.com precise-backports InRelease ==> default: Get:1 http://security.ubuntu.com precise-security Release.gpg [198 B] ==> default: Hit http://us.archive.ubuntu.com precise Release.gpg ==> default: Get:2 http://security.ubuntu.com precise-security Release [54.3 kB] ==> default: Get:3 http://us.archive.ubuntu.com precise-updates Release.gpg [198 B] ==> default: Get:4 http://us.archive.ubuntu.com precise-backports Release.gpg [198 B] ==> default: Hit http://us.archive.ubuntu.com precise Release ==> default: Get:5 http://us.archive.ubuntu.com precise-updates Release [196 kB] ==> default: Get:6 http://security.ubuntu.com precise-security/main Sources [130 kB] ==> default: Get:7 http://security.ubuntu.com precise-security/restricted Sources [3,759 B] ==> default: Get:8 http://security.ubuntu.com precise-security/universe Sources [42.7 kB] ==> default: Get:9 http://security.ubuntu.com precise-security/multiverse Sources [2,203 B] ==> default: Get:10 http://security.ubuntu.com precise-security/main i386 Packages [560 kB] ==> default: Get:11 http://us.archive.ubuntu.com precise-backports Release [54.3 kB] ==> default: Hit http://us.archive.ubuntu.com precise/main Sources ==> default: Hit http://us.archive.ubuntu.com precise/restricted Sources ==> default: Hit http://us.archive.ubuntu.com precise/universe Sources ==> default: Hit http://us.archive.ubuntu.com precise/multiverse Sources ==> default: Hit http://us.archive.ubuntu.com precise/main i386 Packages ==> default: Hit http://us.archive.ubuntu.com precise/restricted i386 Packages ==> default: Hit http://us.archive.ubuntu.com precise/universe i386 Packages ==> default: Hit http://us.archive.ubuntu.com precise/multiverse i386 Packages ==> default: Hit http://us.archive.ubuntu.com precise/main TranslationIndex ==> default: Hit http://us.archive.ubuntu.com precise/multiverse TranslationIndex ==> default: Hit http://us.archive.ubuntu.com precise/restricted TranslationIndex ==> default: Hit http://us.archive.ubuntu.com precise/universe TranslationIndex ==> default: Get:12 http://us.archive.ubuntu.com precise-updates/main Sources [490 kB] ==> default: Get:13 http://security.ubuntu.com precise-security/restricted i386 Packages [8,939 B] ==> default: Get:14 http://security.ubuntu.com precise-security/universe i386 Packages [130 kB] ==> default: Get:15 http://security.ubuntu.com precise-security/multiverse i386 Packages [2,874 B] ==> default: Get:16 http://security.ubuntu.com precise-security/main TranslationIndex [208 B] ==> default: Get:17 http://security.ubuntu.com precise-security/multiverse TranslationIndex [199 B] ==> default: Get:18 http://security.ubuntu.com precise-security/restricted TranslationIndex [202 B] ==> default: Get:19 http://security.ubuntu.com precise-security/universe TranslationIndex [205 B] ==> default: Get:20 http://security.ubuntu.com precise-security/main Translation-en [228 kB] ==> default: Get:21 http://us.archive.ubuntu.com precise-updates/restricted Sources [7,981 B] ==> default: Get:22 http://us.archive.ubuntu.com precise-updates/universe Sources [120 kB] ==> default: Get:23 http://security.ubuntu.com precise-security/multiverse Translation-en [1,408 B] ==> default: Get:24 http://security.ubuntu.com precise-security/restricted Translation-en [2,066 B] ==> default: Get:25 http://us.archive.ubuntu.com precise-updates/multiverse Sources [9,723 B] ==> default: Get:26 http://us.archive.ubuntu.com precise-updates/main i386 Packages [953 kB] ==> default: Get:27 http://security.ubuntu.com precise-security/universe Translation-en [76.6 kB] ==> default: Get:28 http://us.archive.ubuntu.com precise-updates/restricted i386 Packages [13.6 kB] ==> default: Get:29 http://us.archive.ubuntu.com precise-updates/universe i386 Packages [276 kB] ==> default: Get:30 http://us.archive.ubuntu.com precise-updates/multiverse i386 Packages [16.7 kB] ==> default: Get:31 http://us.archive.ubuntu.com precise-updates/main TranslationIndex [10.6 kB] ==> default: Get:32 http://us.archive.ubuntu.com precise-updates/multiverse TranslationIndex [7,613 B] ==> default: Get:33 http://us.archive.ubuntu.com precise-updates/restricted TranslationIndex [7,297 B] ==> default: Get:34 http://us.archive.ubuntu.com precise-updates/universe TranslationIndex [8,333 B] ==> default: Hit http://us.archive.ubuntu.com precise/main Translation-en ==> default: Get:35 http://us.archive.ubuntu.com precise-backports/main Sources [5,411 B] ==> default: Get:36 http://us.archive.ubuntu.com precise-backports/restricted Sources [28 B] ==> default: Get:37 http://us.archive.ubuntu.com precise-backports/universe Sources [42.1 kB] ==> default: Get:38 http://us.archive.ubuntu.com precise-backports/multiverse Sources [5,750 B] ==> default: Get:39 http://us.archive.ubuntu.com precise-backports/main i386 Packages [5,484 B] ==> default: Get:40 http://us.archive.ubuntu.com precise-backports/restricted i386 Packages [28 B] ==> default: Get:41 http://us.archive.ubuntu.com precise-backports/universe i386 Packages [43.6 kB] ==> default: Get:42 http://us.archive.ubuntu.com precise-backports/multiverse i386 Packages [5,413 B] ==> default: Get:43 http://us.archive.ubuntu.com precise-backports/main TranslationIndex [202 B] ==> default: Get:44 http://us.archive.ubuntu.com precise-backports/multiverse TranslationIndex [202 B] ==> default: Get:45 http://us.archive.ubuntu.com precise-backports/restricted TranslationIndex [193 B] ==> default: Get:46 http://us.archive.ubuntu.com precise-backports/universe TranslationIndex [205 B] ==> default: Hit http://us.archive.ubuntu.com precise/multiverse Translation-en ==> default: Hit http://us.archive.ubuntu.com precise/restricted Translation-en ==> default: Hit http://us.archive.ubuntu.com precise/universe Translation-en ==> default: Get:47 http://us.archive.ubuntu.com precise-updates/main Translation-en [398 kB] ==> default: Get:48 http://us.archive.ubuntu.com precise-updates/multiverse Translation-en [9,603 B] ==> default: Get:49 http://us.archive.ubuntu.com precise-updates/restricted Translation-en [2,982 B] ==> default: Get:50 http://us.archive.ubuntu.com precise-updates/universe Translation-en [158 kB] ==> default: Get:51 http://us.archive.ubuntu.com precise-backports/main Translation-en [4,911 B] ==> default: Get:52 http://us.archive.ubuntu.com precise-backports/multiverse Translation-en [4,838 B] ==> default: Hit http://us.archive.ubuntu.com precise-backports/restricted Translation-en ==> default: Get:53 http://us.archive.ubuntu.com precise-backports/universe Translation-en [34.7 kB] ==> default: Fetched 4,136 kB in 49710d 6h 28min 9s (0 B/s) ==> default: Reading package lists... ==> default: Reading package lists... ==> default: Building dependency tree... ==> default: Reading state information... ==> default: The following extra packages will be installed: ==> default: apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 ==> default: libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert ==> default: Suggested packages: ==> default: www-browser apache2-doc apache2-suexec apache2-suexec-custom ==> default: openssl-blacklist ==> default: The following NEW packages will be installed: ==> default: apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common ==> default: libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert ==> default: 0 upgraded, 10 newly installed, 0 to remove and 181 not upgraded. ==> default: Need to get 1,855 kB of archives. ==> default: After this operation, 5,324 kB of additional disk space will be used. ==> default: Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main libapr1 i386 1.4.6-1 [91.3 kB] ==> default: Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1 i386 1.3.12+dfsg-3 [75.4 kB] ==> default: Get:3 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1-dbd-sqlite3 i386 1.3.12+dfsg-3 [10.2 kB] ==> default: Get:4 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1-ldap i386 1.3.12+dfsg-3 [7,962 B] ==> default: Get:5 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2.2-bin i386 2.2.22-1ubuntu1.9 [1,335 kB] ==> default: Get:6 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2-utils i386 2.2.22-1ubuntu1.9 [90.7 kB] ==> default: Get:7 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2.2-common i386 2.2.22-1ubuntu1.9 [228 kB] ==> default: Get:8 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2-mpm-worker i386 2.2.22-1ubuntu1.9 [2,312 B] ==> default: Get:9 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2 i386 2.2.22-1ubuntu1.9 [1,492 B] ==> default: Get:10 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main ssl-cert all 1.0.28ubuntu0.1 [12.3 kB] ==> default: dpkg-preconfigure: unable to re-open stdin: No such file or directory ==> default: Fetched 1,855 kB in 6s (305 kB/s) ==> default: Selecting previously unselected package libapr1. ==> default: (Reading database ... ==> default: 29361 files and directories currently installed.) ==> default: Unpacking libapr1 (from .../libapr1_1.4.6-1_i386.deb) ... ==> default: Selecting previously unselected package libaprutil1. ==> default: Unpacking libaprutil1 (from .../libaprutil1_1.3.12+dfsg-3_i386.deb) ... ==> default: Selecting previously unselected package libaprutil1-dbd-sqlite3. ==> default: Unpacking libaprutil1-dbd-sqlite3 (from .../libaprutil1-dbd-sqlite3_1.3.12+dfsg-3_i386.deb) ... ==> default: Selecting previously unselected package libaprutil1-ldap. ==> default: Unpacking libaprutil1-ldap (from .../libaprutil1-ldap_1.3.12+dfsg-3_i386.deb) ... ==> default: Selecting previously unselected package apache2.2-bin. ==> default: Unpacking apache2.2-bin (from .../apache2.2-bin_2.2.22-1ubuntu1.9_i386.deb) ... ==> default: Selecting previously unselected package apache2-utils. ==> default: Unpacking apache2-utils (from .../apache2-utils_2.2.22-1ubuntu1.9_i386.deb) ... ==> default: Selecting previously unselected package apache2.2-common. ==> default: Unpacking apache2.2-common (from .../apache2.2-common_2.2.22-1ubuntu1.9_i386.deb) ... ==> default: Selecting previously unselected package apache2-mpm-worker. ==> default: Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.22-1ubuntu1.9_i386.deb) ... ==> default: Selecting previously unselected package apache2. ==> default: Unpacking apache2 (from .../apache2_2.2.22-1ubuntu1.9_i386.deb) ... ==> default: Selecting previously unselected package ssl-cert. ==> default: Unpacking ssl-cert (from .../ssl-cert_1.0.28ubuntu0.1_all.deb) ... ==> default: Processing triggers for man-db ... ==> default: Processing triggers for ureadahead ... ==> default: ureadahead will be reprofiled on next reboot ==> default: Processing triggers for ufw ... ==> default: Setting up libapr1 (1.4.6-1) ... ==> default: Setting up libaprutil1 (1.3.12+dfsg-3) ... ==> default: Setting up libaprutil1-dbd-sqlite3 (1.3.12+dfsg-3) ... ==> default: Setting up libaprutil1-ldap (1.3.12+dfsg-3) ... ==> default: Setting up apache2.2-bin (2.2.22-1ubuntu1.9) ... ==> default: Setting up apache2-utils (2.2.22-1ubuntu1.9) ... ==> default: Setting up apache2.2-common (2.2.22-1ubuntu1.9) ... ==> default: Enabling site default. ==> default: Enabling module alias. ==> default: Enabling module autoindex. ==> default: Enabling module dir. ==> default: Enabling module env. ==> default: Enabling module mime. ==> default: Enabling module negotiation. ==> default: Enabling module setenvif. ==> default: Enabling module status. ==> default: Enabling module auth_basic. ==> default: Enabling module deflate. ==> default: Enabling module authz_default. ==> default: Enabling module authz_user. ==> default: Enabling module authz_groupfile. ==> default: Enabling module authn_file. ==> default: Enabling module authz_host. ==> default: Enabling module reqtimeout. ==> default: Setting up apache2-mpm-worker (2.2.22-1ubuntu1.9) ... ==> default: * Starting web server apache2 ==> default: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName ==> default: ...done. ==> default: Setting up apache2 (2.2.22-1ubuntu1.9) ... ==> default: Setting up ssl-cert (1.0.28ubuntu0.1) ... ==> default: Processing triggers for libc-bin ... ==> default: ldconfig deferred processing now taking place ==> default: /tmp/vagrant-shell: line 8: syntax error: unexpected end of file The SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed. The output for this command should be in the log above. Please read the output to determine what went wrong. |
■SSH接続してWEBサーバが動いているか確認
1 2 3 4 5 |
$ wget -qO- 127.0.0.1 <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html> |
9、ネットワーキング
ネットワーキング機能でホストマシンのブラウザからアクセスできるようにします。
まずはVagrantfileの設定を下記のように変更します。
1 2 3 4 5 |
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise32" config.vm.provision :shell, path: "bootstrap.sh" config.vm.network :forwarded_port, host: 4567, guest: 80 end |
次に再起動を行います
1 |
vagrant reload --provision |
・ホストマシンのブラウザで確認
http://127.0.0.1:4567にアクセスしてみます。
ページが表示できることが確認できました。
10、仮想マシンの削除
「vagrant destroy」コマンドで仮想マシンを削除します。
「vagrant destroy」と入力して実行。
1 2 3 4 5 |
$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives... ==> default: Running cleanup tasks for 'shell' provisioner... |
「vagrant status」コマンドで状態を確認すると削除されているがわかります。
1 2 3 4 5 6 7 8 9 |
$ vagrant status Current machine states: default not created (virtualbox) The environment has not yet been created. Run `vagrant up` to create the environment. If a machine is not created, only the default provider will be shown. So if a provider is not listed, then the machine is not created for that environment. |
Vagrantはすぐに仮想マシンを作ったり消したりできて便利だと思いました。
みなさんもぜひお試しください。