LoginSignup
191
192

More than 5 years have passed since last update.

Mac 上で Vagrant を使って CentOS + Apache をセットアップ

Last updated at Posted at 2014-06-04

MacBookAir にて仮想環境を構築したく、またプロビジョニングにて常に必要な Apache などを入れられたらなと思いました。

Vagrant の使い方は素敵な方法がありましたので下記を参考にさせていただきました。とてもスムーズな導入ができました。本当にありがとうございます。

参考

Vagrant体験入門ハンズオン手順 - 2014/04/24 DevLove関西

事前準備

  • 上記の参考より、VirtualBox と Vagrant をインストールしてください。

Vagrant 基本セットアップ

自分の環境でのメモ踏まえて記載してまいります。
OS は CentOS6.5を採用します。まずはディレクトリを作成して、対象の box を追加します。

$ mkdir CentOS65
$ cd centOS65
$ vagrant box add CentOS65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

ここで box の list を確認します。「CentOS65」あります。

$ vagrant box list
CentOS65 (virtualbox, 0)

次に初期設定にて Vagrantfile を生成します。

$ vagrant init CentOS65
$ ls
Vagrantfile

ここで Vagrantfile を編集します。

  • config.vm.box
  • config.vm.network
  • config.vm.provider

network は ssh でアクセスするために設定します。provider は GUI モードを有効にしているだけなので、設定しなくても問題ありません。

-*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "CentOS65"

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

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
  end
end

設定後、仮想環境を起動します。

$ vagrant up

仮想環境にログインします。

$ vagrant ssh

ログインできればこれで基本設定は終了です。

Shell を使って Apache をインストール

Vagrantfile を編集して shell で iptables と apache インストールを provision にて設定する

  • Shell でプロビジョニング実施
  • タイムゾーン設定
  • ローカルな VM のため iptables を停止
  • httpd を yum でインストール
-*- mode: ruby -*-

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "CentOS65"

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

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
  end

  config.vm.provision "shell", inline: <<-EOT
        # timezone
        cp -p /usr/share/zoneinfo/Japan /etc/localtime
        # iptables off
        /sbin/iptables -F
        /sbin/service iptables stop
        /sbin/chkconfig iptables off
        # Apache
        yum -y install httpd
        /sbin/service httpd restart
        /sbin/chkconfig httpd on
  EOT
end

仮想環境を再起動します。

$ vagrant reload

プロビジョニングを実施します。

$ vagrant provision

ブラウザで http://192.168.33.10にアクセスし、Apache の Welcome ページが表示されれば成功です。

$ open http://192.168.33.10

HTML ファイルを作成し DocumentRoot を変更する

表示する HTML を作成する

$ echo "Hello!" >> index.html
$ ls
Vagrantfile index.html

httpd.conf を変更します。
DocumentRoot は synced_folder の /vargrant に変更します。

  • 仮想マシンにログイン
  • httpd.conf をコピー
[vagrant]$ cp -a /etc/httpd/conf/httpd.conf /vagrant/
[vagrant]$ exit
$ ls
Vagrantfile httpd.conf index.html

httpd.conf に VirtualHost を追記して DocumentRoot を /vagrant にします。

$ vim httpd.conf

<VirtualHost *:80>
    DocumentRoot /vagrant
</VirtualHost>

次に Vagrantfile を編集し、httpd.conf を /etc/httpd/conf/ へコピーします。

-*- mode: ruby -*-

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "CentOS65"

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

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
  end

  config.vm.provision "shell", inline: <<-EOT
        # timezone
        cp -p /usr/share/zoneinfo/Japan /etc/localtime
        # iptables off
        /sbin/iptatbles -F
        /sbin/service iptables stop
        /sbin/chkconfig iptables off
        # Apache
        yum -y install httpd
        cp -a /vagrant/httpd.conf /etc/httpd/conf/  # --- ここを追加
        /sbin/service httpd restart
        /sbin/chkconfig httpd on
  EOT
end

再度プロビジョニングを実行します。

$ vagrant provision

ブラウザで http://192.168.33.10にアクセスし、index.html の内容が表示されれば成功です。

$ open http://192.168.33.10

これで環境設定ができました。自分は不慣れですのでかなり苦労しましたが、少しでも参考になれば幸いです。

191
192
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
191
192