ひとり勉強ログ

ITエンジニアの勉強したことメモ

LAMP環境構築その2 – Vagrantを使用してCentOS7を立ち上げる

前回、VirtualBoxVagrantのダウンロード、インストールを行いました。

今回はVagrantを使用してCentOS7を立ち上げるところまでを行います。

コマンドプロンプトを起動

Vagrantコマンドプロンプトから使っていくので、コマンドプロンプトを起動しておきます。

Vagrant用のフォルダ「MyVagrant」を作成、移動

[bash] mkdir MyVagrant cd MyVagrant [/bash]

CentOS用のディレクトリを作成、移動

[bash] mkdir mycentos cd mycentos [/bash]

CentOSのboxファイルを追加

コマンドプロンプトで下記のように設定することができます。 [bash] vagrant box add {title} {url} [/bash] で追加することが可能です。

{title}はboxの名前で、なんでもよいです。

{url}は Vagrantbox.esから選びます。

今回、「centos」という名前でcentos7.0の64bitを使うということで設定していくので、下記をコマンドプロンプトで実行します。

[bash] vagrant box add centos https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box [/bash]

boxファイルのダウンロードに時間がかかりますが(私は10分待ちました)、下記のように表示されればOKです。

[bash] ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'centos' (v0) for provider: box: Downloading: https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box box: Progress: 100% (Rate: 1204k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'centos' (v0) for 'virtualbox'! [/bash]

Vagrantの初期化

boxファイルをダウンロードしたら、mycentosにいることを確認してVagrantfileの初期化を行います。

コマンドは [bash] vagrant init {title} [/bash] なので [bash] vagrant init centos [/bash] と実行します。

下記のようにコマンドに表示されると、mycentosフォルダ内にVagrantfileができています。 [bash] 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. [/bash]

Vagrantfileの編集

Vagrantfileのバックアップを取って、必要部分を編集していきます。

ブラウザで表示させるためのIPアドレス設定している部分のパウンド記号(#)を消去します。

修正前 [bash] # config.vm.network "private_network", ip: "192.168.33.10" [/bash]  ↓ 修正後 [bash] config.vm.network "private_network", ip: "192.168.33.10" [/bash]

CentOS7の起動

では、CentOS7の起動を行います。Vagrantfileがあるディレクトリにいることを確認した上で、下記のコマンドを実行します。

[bash] vagrant up [/bash]

下記のように表示されれば起動OKです。 [bash] Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: mycentos_default_1468742916197_27606 ==> 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: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (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: 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 it's 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.3.28 default: VirtualBox Version: 5.0 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => C:/Users/username/MyVagrant/mycentos [/bash]

Vagrantのコマンド

主に使用するVagrantのコマンドをまとめました。

Vagrantの起動 [bash] vagrant up [/bash]

Vagrantのシャットダウン [bash] vagrant halt [/bash]

Vagrantの状況確認 [bash] vagrant status [/bash]

Vagrantfileの再読み込み、リロード [bash] vagrant reload [/bash]

仮想マシンの削除 [bash] vagrant destroy [/bash]