LoginSignup
27
27

More than 5 years have passed since last update.

Mac OS 10.9+chef+vagrantでLaravel環境(LAMP)を構築するまでの手順

Last updated at Posted at 2014-08-16

作業環境

mac os 10.9.4
ruby 2.0 (mac os 標準搭載)
gem 2.0.14 (mac os 標準搭載)

chef install

Chef 11.14.2 インストール

通常Chef公式サイトから紹介されている次のコマンドからインストールする。

curl -L https://www.opscode.com/chef/install.sh | sudo bash

しかし、mac os 10.9 の場合はcurl経由でchefをインストールするとknife soloが正しく動かない場合があるので以下のgemコマンドでchefをインストールする

sudo gem install chef

ERROR: Failed to build gem native extension のエラーになる場合は下段補足1の対応をする
パスワードを聞かれるがあるので自分のmacアカウントパスを入力。
バージョン確認 : chef-client -v

knife-solo インストール

sudo gem install knife-solo

knife-soloコマンド設定
とりあえず、デフォルト設定。すべてEnterでいい

knife configure

仮想環境用ツール Install

Vagrant 1.6.3

次の公式サイトからダウンロードしてインストール。
http://www.vagrantup.com/downloads.html

VirtualBox 4.3.14

次の公式サイトからダウンロードしてインストール。
https://www.virtualbox.org/wiki/Downloads

CentOS6.5の仮想環境を作成する

Vagrantのcentos6.5のテンプレートを取得する
※centos6.4でphpをインストールするとバージョン5.3.3がインストールされるため、centos6.5を取得するのがおすすめ。

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

それ以外のテンプレートを取得したい場合は以下のサイトを参照
http://www.vagrantbox.es

作業業ディレクトリを作成しその中でVagrantの初期化を行う

vagrant init centos65

ローカル環境から接続できるようにVagrant設定ファイルからネットワークの設定を変更

27行あたりの以下のコメントアウトを外す

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

centos6.5の仮想環境を起動

vagrant up

その他の主要コマンド(http://docs.vagrantup.com/v2/cli/index.html)

  • vagrant status : 状態確認
  • vagrant halt : 停止
  • vagrant reload : 再起動
  • vagrant destroy : 削除

vagrant ssh コマンドで仮想環境に接続できるか確認

別名を指定する

vagrant ssh-config --host laravel >> ~/.ssh/config

ssh laravel コマンドで接続できるかを確認

chef設定

chef-repoディレクトリ配下で以下のコマンドを実行。

リポジトリを作成

knife solo init chef-repo 

node(Vagrantで作成した仮想環境:別名laravel)をchefに対応

仮想マシンが動いてる状態で実行しないとnetworkエラーになるので注意。

knife solo prepare laravel

cookbook作成

knife cookbook create laravel -o site-cookbooks/

レシピ作成

~/chef-repo/site-cookbooks/laravel/recipes/default.rbファイルを編集

#
# Cookbook Name:: laravel
# Recipe:: default
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

# remi repository導入
remote_file "/tmp/remi-release-6.rpm" do
  source "http://rpms.famillecollet.com/enterprise/remi-release-6.rpm"
  not_if { ::File.exists?("/tmp/remi-release-6.rpm") }
end

package "remi-release-6.rpm" do
  action :install
  provider Chef::Provider::Package::Rpm
  source "/tmp/remi-release-6.rpm"
end

=begin
php-devel : phpを拡張するために必要
php-mbstring : php 日本語環境に関する設定
php-mcrypt : WEB通信に暗号化モジュール
=end

%w{php php-devel php-mbstring php-mysql mysql-server php-mcrypt httpd vim-enhanced}.each do |pkg|
  package pkg do
    action :install
    options "--enablerepo=remi --enablerepo=remi-php55"
  end
end

execute "install_laravel" do
  command "composer create-project laravel/laravel fw_laravel --prefer-dist"
  creates "/var/www/html/fw_laravel"
  action :run
end

execute "move_laravel" do
  command "mv fw_laravel/ /var/www/html/fw_laravel"
  creates "/var/www/html/fw_laravel"
  action :run
end

execute "chmod_laravel_public" do
  command "chmod -R 777 /var/www/html/fw_laravel/app/storage"
  action :run
end

service "iptables" do
  action [:stop, :disable]
end

service "httpd" do
  action [:start, :enable]
end

node設定

~/chef-repo/nodes/laravel.jsonファイルを編集

{
  "run_list": [
    "recipe[laravel]"
  ],
  "automatic": {
    "ipaddress": "laravel"
  }
}

【必読】cookbookをnodeに反映する前の便利技

  • 作業前に起動中の仮想マシンを停止させる[vagrant halt]
  • saharaというvagrantプラグインをインストール
vagrant plugin install sahara

vagrant sahara plugin : OS状態を記録しrollbackができる
vagrant plugin listコマンドでsaharaが表示されるとOK

  • vagrant 再起動 [vagrant up]
  • vagrantの状態記録 [vagrantディレクトリ配下でコマンド実行]
vagrant sandbox on

cookbookをnodeに反映 [chef-repoディレクトリ配下でコマンド実行]

knife solo cook laravel
  • 仮想サーバーに接続し、正しくインストールされたか状態の確認を行う [ssh laravel]

異常の場合

vagrant sandbox rollback

~/chef-repo/site-cookbooks/laravel/recipes/default.rbファイルを編集し、cookbookをnodeに反映する [knife solo cook laravel]
※ 正常になるまで繰り返し作業するのがポイント

正常の場合

vagrant sandbox commit
  • vagrant状態記録を中止 [vagrantディレクトリ配下でコマンド実行]
vagrant sandbox off

完成です。お疲れさまでした!!

補足1

homebrewをインストール
apple-gcc42インストール

27
27
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
27
27