LoginSignup
2
3

More than 5 years have passed since last update.

vagrantでwebページを表示する(メモ)

Last updated at Posted at 2018-06-21

仮想環境でwebページを表示しよう!

はじめに

  • 共有用兼自分メモ
  • 初めて仮想環境を構築する方に向けて、参考になればと思います。
  • macでの構築を対象にしています。

仮想環境?

  • 本番サーバと同じ環境を用意して開発を行える
  • テスト→本番の移行をしても正しく動作する

Vagrant?

  • 仮想環境を簡単に立ち上げることができるソフトウェア

仮想環境を構築する

準備
  • VirtualBox
    →仮想化ソフト

  • Vagrant
    →VirtualBox簡単操作用

  • VagrantBox
    box一覧
    →仮想に立ち上げるOSイメージ

インストール&設定

1. VirtualBoxとVagrantをインストール

各インストーラの指示に従いインストール
VirtualBoxはインストール完了すればアプリケーションに追加される
コマンドラインでvagrant -vでvagrantのインストール確認。

2. Boxの追加

1.でVirtualBoxとVagrantのインストール完了後コマンドラインでvagrant box add bento/centos-6.7実行

==> box: Loading metadata for box 'bento/centos-6.7'
    box: URL: https://vagrantcloud.com/bento/centos-6.7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice:
  • ここでは2) virtualboxを選択
Enter your choice: 2
==> box: Adding box 'bento/centos-6.7' (v2.2.7) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/bento/boxes/centos-6.7/versions/2.2.7/providers/virtualbox.box
==> box: Successfully added box 'bento/centos-6.7' (v2.2.7) for 'virtualbox'!
3. 追加BOXの確認

コマンドラインでvagrant box list

bento/centos-6.7  (virtualbox, 2.2.7)
4. 環境構築用ディレクトリを作成

コマンドラインで作成したいディレクトリに移動した後mkdir centos/6_7

ex VirtualBoxとVagrantBoxのバージョンを最適化するプラグイン

コマンドラインでvagrant plugin install vagrant-vbguest

5. boxの初期化

コマンドラインでvagrant init bento/centos-6.7

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.
6. Vagrantを起動

コマンドラインで操作

vagrant up // VMを起動
vagrant ssh // ステータス確認
vagrant halt // VMを終了
vagrant destroy // VMを削除

vagrant upができない場合

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=500,gid=500 vagrant /vagrant

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

なんかこんなエラーが出て起動できない!
→共有フォルダのマウントに失敗している。
→ホストとゲストのバージョンが違うからエラーになる

vbguestで最適化されない?

vagrant vbguestを実行すると以下メッセージが出る・・・

(略)

An error occurred during installation of VirtualBox Guest Additions 5.2.12. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-2.6.32-573.el6.x86_64
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
Unmounting Virtualbox Guest Additions ISO from: /mnt

メッセージにもあるがkernel-devel kernel-devel-2.6.32-573.el6.x86_64をインストールすればよい

コマンドラインでvagrant ssh

kernel-develをインストール

sudo yum  -y install ftp://mirror.switch.ch/pool/4/mirror/scientificlinux/6.3/x86_64/updates/security/kernel-devel-2.6.32-573.el6.x86_64.rpm

kernelはここ(http://rpm.pbone.net/)で検索できる

インストール完了後vagrant vbguestを実行!

この後に一度vagrantを再起動!

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-6.7' 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 (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: Machine booted and ready!
[default] GuestAdditions 5.2.12 running --- OK.

webサーバの構築

apacheのインストール

sudo yum -y install httpd

インストールの確認
rpm -qa | grep httpd

httpd-tools-2.2.15-60.el6.centos.6.x86_64
httpd-2.2.15-60.el6.centos.6.x86_64
apache起動

sudo /etc/init.d/httpd start

起動確認
ps aux | grep httpd

root      2342  0.0  0.7 175312  3772 ?        Ss   09:02   0:00 /usr/sbin/httpd
apache    2344  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2345  0.0  0.4 175312  2476 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2346  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2347  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2348  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2349  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2350  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
apache    2351  0.0  0.4 175312  2460 ?        S    09:02   0:00 /usr/sbin/httpd
vagrant   2353  0.0  0.1 103336   916 pts/0    S+   09:02   0:00 grep httpd

自動起動設定
sudo chkconfig httpd on


html設置

ドキュメントルートの確認
vi /etc/httpd/conf/httpd.conf

vi内で/DocumentRoot

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"
ページを作成

ドキュメントルートに移動
cd /var/www/html/

html作成
sudo vi index.html

vi内で以下

<html>
<head></head>
<body>
        <h1>HELLO!!</h1>
</body>
</html>
ページにアクセス

ローカルからアクセスする

Vagrantfileの以下部分をコメントを外す

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

ブラウザから192.168.33.10にアクセス!

2
3
0

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
2
3