LoginSignup
33

More than 5 years have passed since last update.

MacでVirtualBox+CentOS6の開発環境を構築する(Vagrant利用)

Last updated at Posted at 2016-05-01

概要

MacでVagrantを利用して、VirtualBox+CentOS6の開発環境を構築する。

環境

OSX 10.11.4
VirtualBox 5.0.18
CentOS 6.7

VirtualBoxをインストール

Vagrantをインストール

CentOSを使えるようにする

参考
https://www.vagrantup.com/docs/getting-started/

まずは、以下のページでCentOS 6.7のboxを探す。(boxとはVagrantで利用できるOSイメージ)
https://atlas.hashicorp.com/boxes/search

bento/centos-6.7

が見つかった。

Vagrantにboxを追加する。(virtualboxを選択)

$ vagrant box add bento/centos-6.7


1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2

boxを追加できたかどうか確認する。

$ vagrant box list
bento/centos-6.7 (virtualbox, 2.2.6)

作業ディレクトリを任意の場所に作成する。

$ mkdir -p ~/vagrant/centos6
$ cd ~/vagrant/centos6

初期化コマンドを実行する。

$ vagrant init bento/centos-6.7

Vagrantfileという設定ファイルが作成される。

VMを起動する。

$ vagrant up

sshで接続する。

$ vagrant ssh

VMから抜けるときは$ exitでOK。

VMを終了する。

$ vagrant halt

おまけ

boxについて

boxはこちらからでも探せる。
http://www.vagrantbox.es

vagrant up時にエラーが発生

vagrant upの際、

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

のようなエラーが発生する場合、プラグインをいれると解決する。
https://github.com/dotless-de/vagrant-vbguest

$ vagrant plugin install vagrant-vbguest

MacのブラウザからCentOS上のWebサーバにアクセス

Vagrantfileを修正する。(コメントアウトをはずす)

config.vm.network "private_network", ip: "192.168.33.10"

VMを再起動する。

$ vagrant reload

sshで接続する。

$ vagrant ssh

こちらを参考に、PHP、httpdでやってみる。
http://qiita.com/okmn/items/cbd1f97f02c6d9943111#おまけ

上記ページ内の手順を実施後、Macのブラウザでアクセスできるかを確認する。
http://192.168.33.10/info.php

コマンド

$vagrant -h
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Atlas
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

よく使いそうなコマンドは、
init(初期化)
box(box操作)
up(起動)、halt(終了)、reload(再起動)、status(VMのステータスを確認)
destroy(VMの削除)
ssh(sshで接続)
あたり。

各コマンドのさらに詳細な説明は、

$ vagrant コマンド -h

で確認できる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33