life goes on

ローカル開発環境の構築 〜仮想マシンの立ち上げ〜

 Webアプリを作る環境や、検証用の環境がほしいやるとき、何かしらの開発環境がなければならないですよね?

VMwareかEC2か、さくらVPSか・・・他色々

さくら系やEC2だとお金かかるしなーと諸々思案し、色々調べてVagrantVirtualBoxを利用して環境構築してみました。

どこかでまた構築する事があるかもしれないので、セットアップ〜仮想マシンの立ち上げまで手順をメモっときます。

 

■テーマ:ローカル開発環境をつくる

 

Vagrantのインストール

URL:

http://www.vagrantup.com/downloads.html 

にアクセスすると、この画面が表示されるので Mac OSの私は

赤線をクリックしてダウンロードし、インストール

f:id:tsasaki9999:20140113151957p:plain

 

Oracle Virtual Boxのインストール

URL:

https://www.virtualbox.org/wiki/Downloads

URLにアクセス、MacなのでOS Xを落とすことに 赤線のとこ

ダウンロードしたら、手順に従ってインストールするのみ

 

f:id:tsasaki9999:20140113151958p:plain

 

③Box(テンプレート)の取得

URL:

http://docs.vagrantup.com/v2/getting-started/boxes.html

にアクセス Boxの追加方法が記載されている。赤枠のところ

f:id:tsasaki9999:20140113151956p:plain

 

ローカル端末に戻りMacであれば、ターミナルを開いてbox追加のコマンドを実行

赤枠と同じコマンド(再掲)

$ vagrant box add precise32 \
    http://files.vagrantup.com/precise32.box

 

実行


$ vagrant box add precise32 \
>     http://files.vagrantup.com/precise32.box
Downloading box from URL: http://files.vagrantup.com/precise32.box
Extracting box...te: 16602/s, Estimated time remaining: 0:00:01)
Successfully added box 'precise32' with provider 'virtualbox'!
 

・・・・・終わったか よし! 

 

次にCentOS仮想マシン作りたいので、各種ディストリビューション(色んなテンプレートが置いてある)が配布されているサイトへ行き URLをコピる

URL:

http://www.vagrantbox.es/

アクセスして ここから、入れたいOSを選んで、コピーボタンを押す。

私は赤枠のものを選んだ

f:id:tsasaki9999:20140113151955p:plain

 

 

コピったら、ローカルのターミナルに戻ってbox追加のコマンドを打つ

$ vagrant box add [box名] [boxのURL]

 

はい 実行


$ vagrant box add centos64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box
Downloading box from URL: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box
Extracting box...te: 440k/s, Estimated time remaining: 0:00:01))
Successfully added box 'centos64' with provider 'virtualbox'!

・・・追加された か すっげー長かった 3時間くらいかかりましたよ(笑)

 

④作業用フォルダの作成と初期化

 次に、作業フォルダを作って、初期化する。

コマンドとしては

 

$ mkdir CentOSVM  ・・・適当にフォルダ作って

$ cd CentOSVM/    ・・・中に入って

$ vagrant init [box名]   ・・・centos64をbox名にして初期化

 

はい 順に実行


$ mkdir CentOSVM
$ cd CentOSVM/
hogehoge-no-macbook:CentOSVM hogehoge$ pwd
/Users/hogehoge/CentOSVM
hogehoge-no-macbook:CentOSVM hogehoge$ vagrant init centos64
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.
hogehoge-no-macbook:CentOSVM hogehoge$ ls
Vagrantfile
hogehoge-no-macbook:CentOSVM hogehoge$ less vagrantfile

 ・・・すると、Vagrantfileが作成されます。

設定ファイルみたいなものです。

ターミナル上でlessで読んでもいいし、テキストか何かで開いて読んでもいい。

一度は、見てみることをオススメします。

 

仮想マシンの立ち上げ

$ vagrant up   ・・・起動

 

起動してみる


hogehoge-no-macbook:CentOSVM hogehoge$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
prevent things such as shared folders from working properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.16
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

・・・・うまくいったか!?

 VirtualBoxにもCentOSVMが作成されて「実行中」と表示されていましたので

起動成功!OKOK

 

■その他

vagrantで使える主要なコマンドメモ

 

$vagrant staus ・・・・状態を見る

$vagrant susp・・・・スリープ 

$vagrant resume・・・・ 復帰

$vagrant halt ・・・・終了

$vagrant up ・・・・立ち上げ

$vagrant reload ・・・・再起動

$vagrant destroy ・・・・仮想マシン削除 。VirtualBoxからも無くなります。

 

 ってな感じ