VagrantのMacへの導入 & Apache, PHP, MySQL, memcachedのインストール

2014/04/24現在

 

①virtual Box のインストール

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

 

vagrant のインストール

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

 

③どこでもよいがvagrant用のフォルダを作成

今回(2014/04/24)は~の下にCentOSフォルダを作った

作ったらそのフォルダに移動

 

④OSイメージの追加(2013/09/05時点の記事)

$ vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box

 

仮想マシンの作成

$ vagrant init centos

 

⑥Vagrantfileの設定を変更(一番下の【Vagrantfileの設定】を参照)

 

仮想マシンの起動

$ vagrant up

 

仮想マシンにログイン

$ vagrant ssh

 

仮想マシンに色々インストール(以下、複数行のコマンドは一気に貼り付け可能)

sudo yum -y upgrade

# リポジトリ追加

sudo rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

sudo rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

sudo rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

sudo rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6

sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi

 

sudo vi /etc/yum.repos.d/remi.repo

# 下記を変更

[remi]

name=Les RPM de remi pour Enterprise Linux 6 - $basearch

#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/

mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror

enabled=0

enabled=1

 

# Apache(2.2.15-26)

sudo yum -y install httpd-devel

sudo yum -y install mod_ssl

 

# PHP(5.4.13-1)

sudo yum remove php php-*

sudo yum -y install php

sudo yum -y install php-devel

sudo yum -y install php-pdo

sudo yum -y install php-mysql

sudo yum -y install php-mbstring

sudo yum -y install php-mcrypt

sudo yum -y install php-gd

sudo yum -y install php-pear

sudo yum -y install php-pear-DB

sudo yum -y install php-pear-Auth-*

sudo yum -y install php-pecl-apc-devel

sudo yum -y install zlib-devel

 

# MySQL(5.5.30-1)

sudo yum -y install mysql-devel

sudo yum -y install mysql-server

 

# memcached(1.4.15-2)

sudo yum -y install memcached-devel

sudo yum -y install php-pecl-memcache

 

# リポジトリの無効化

sudo vi /etc/yum.repos.d/rpmforge.repo

# 下記を変更

[rpmforge]

name = RHEL $releasever - RPMforge.net - dag

baseurl = http://apt.sw.be/redhat/el6/en/$basearch/rpmforge

mirrorlist = http://apt.sw.be/redhat/el6/en/mirrors-rpmforge

#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge

enabled = 1

enabled = 0

 

sudo vi /etc/yum.repos.d/epel.repo

# 下記を変更

[epel]

name=Extra Packages for Enterprise Linux 6 - $basearch

#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch

failovermethod=priority

enabled=1

enabled=0



sudo vi /etc/yum.repos.d/remi.repo

# 下記を変更

[remi]

name=Les RPM de remi pour Enterprise Linux 6 - $basearch

#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/

mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror

enabled=1

enabled=0

 

# Apacheの設定

# 不要なコンフィグを読込まないようにする

sudo mv -i /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org

# 起動

sudo service httpd start

# 自動起動設定

sudo chkconfig httpd on

# /etc/httpd/conf/httpd.conf編集

sudo vi /etc/httpd/conf/httpd.conf

<Directory />

   Options FollowSymLinks

   AllowOverride All

</Directory>

 

# 上の設定で、ブラウザからアクセスした時に白い画面が表示される場合(というかやった方がいい)

<Directory “/var/www/html”>

   Options FollowSymLinks

   AllowOverride All

</Directory>

 

# PHPの設定

# エラーログ出力先作成

sudo mkdir /var/log/php

sudo chown apache /var/log/php

sudo chmod 755 /var/log/php

 

sudo vi /etc/php.ini

# 下記を変更

expose_php = On

expose_php = Off

 

;date.timezone =

date.timezone = 'Asia/Tokyo'

 

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

error_reporting = E_ALL & ~E_NOTICE

 

;error_log = php_errors.log

error_log = "/var/log/php/php_errors.log"

 

# セッションの保存先をmemcacheに設定

sudo vi /etc/php.d/memcache.ini

# 下記を変更

;session.save_handler=memcache

session.save_handler=memcache

 

;session.save_path="tcp://localhost:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

session.save_path="tcp://localhost:11211"

 

sudo vi /etc/httpd/conf.d/php.conf

# 下記を変更

php_value session.save_handler "files"

php_value session.save_path "/var/lib/php/session"

#php_value session.save_handler "files"

#php_value session.save_path "/var/lib/php/session"

 

# memcachedの設定

# 自動起動設定

sudo chkconfig memcached on

 

# 起動

sudo service memcached start

sudo service httpd restart

 

# MySQLの設定

# ログの出力先作成

sudo mkdir /var/log/mysql

sudo chown -R mysql:mysql /var/log/mysql

 

# 起動

sudo service mysqld start

 

# 自動起動設定

sudo chkconfig mysqld on

 

# 初期設定

sudo mysql_secure_installation

# 対話式で設定が始まります。

# rootにパスワードを設定する以外は、すべてエンターでOKです。

Set root password? [Y/n]

New password: 任意のパスワード

Re-enter new password: 任意のパスワード

# 以下略

 

sudo rm -rf /var/www/html

# /home/vagrant/srcを/var/www/htmlにシンボリックリンクを設定

sudo ln -fs /home/vagrant/src /var/www/html

 

# iptable設定

sudo iptables -F

sudo /etc/init.d/iptables save

sudo /etc/init.d/iptables restart

# 追加

sudo iptables -P INPUT ACCEPT

sudo iptables -P FORWARD ACCEPT

sudo iptables -P OUTPUT ACCEPT

sudo iptables -F

sudo service iptables save

sudo service iptables restart

 

# postfix

sudo yum -y install postfix

sudo /etc/rc.d/init.d/postfix start

sudo chkconfig postfix on



# apacheのキャッシュでcssが更新されない時

# http://jun-ya.hatenablog.com/entry/2013/08/09/170335

# http://syati.info/?p=2123

 

sudo vi /etc/httpd/conf/httpd.conf

# 3箇所書き換えた

 

<Directory />

...

# 以下を追記

EnableSendfile Off

...

</Directory>

 

<Directory “/var/www/html”>

...

# 以下を追記

EnableSendfile Off

...

</Directory>

 

<Directory “/home/vagrant/src”>

...

# 以下を追記

EnableSendfile Off

...

</Directory>

 

CentOS/src/index.htmlというファイルをおく。index.htmlの中身は何でもいい ex. hello

 

http://localhost:8080/ にアクセス

すると⑩で作ったindex.htmlの中身が表示される

 

----------------------------------------以下、CakePHPの場合の設定----------------------------------------

CakePHP のcore.php に mask を2箇所追記

Cache::config('_cake_core_', array(

'engine' => $engine,

'prefix' => $prefix . 'cake_core_',

'path' => CACHE . 'persistent' . DS,

'serialize' => ($engine === 'File'),

'duration' => $duration,

'mask' => 0666 ←追加する

));

Cache::config('_cake_model_', array(

'engine' => $engine,

'prefix' => $prefix . 'cake_model_',

'path' => CACHE . 'models' . DS,

'serialize' => ($engine === 'File'),

'duration' => $duration,

'mask' => 0666 ←追加する

));

 

参考(http://qiita.com/pakiln/items/0f3e3ca4dc67495c8218)

以上




●【Vagrantfileの設定】

# -*- mode: ruby -*-

# vi: set ft=ruby :

 

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!

VAGRANTFILE_API_VERSION = "2"

 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

 // この行を追加

 config.vbguest.auto_update = true

 # All Vagrant configuration is done here. The most common configuration

 # options are documented and commented below. For a complete reference,

 # please see the online documentation at vagrantup.com.

 

 # Every Vagrant virtual environment requires a box to build off of.

 config.vm.box = "base"

 

 # The url from where the 'config.vm.box' box will be fetched if it

 # doesn't already exist on the user's system.

 # config.vm.box_url = "http://domain.com/path/to/above.box"

 

 # Create a forwarded port mapping which allows access to a specific port

 # within the machine from a port on the host machine. In the example below,

 # accessing "localhost:8080" will access port 80 on the guest machine.

 // この行をコメントアウト

 config.vm.network :forwarded_port, guest: 80, host: 8080

 

 # Create a private network, which allows host-only access to the machine

 # using a specific IP.

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

 

 # Create a public network, which generally matched to bridged network.

 # Bridged networks make the machine appear as another physical device on

 # your network.

 # config.vm.network :public_network

 

 # If true, then any SSH connections made will enable agent forwarding.

 # Default value: false

 # config.ssh.forward_agent = true

 

 # Share an additional folder to the guest VM. The first argument is

 # the path on the host to the actual folder. The second argument is

 # the path on the guest to mount the folder. And the optional third

 # argument is a set of non-required options.

 // この行をコメントアウト

 config.vm.synced_folder "src/", "/home/vagrant/src", owner: 'vagrant', group: 'apache', mount_options: ['dmode=777', 'fmode=666']

 

 # Provider-specific configuration so you can fine-tune various

 # backing providers for Vagrant. These expose provider-specific options.

 # Example for VirtualBox:

 #

 # config.vm.provider :virtualbox do |vb|

 #   # Don't boot with headless mode

 #   vb.gui = true

 #

 #   # Use VBoxManage to customize the VM. For example to change memory:

 #   vb.customize ["modifyvm", :id, "--memory", "1024"]

 # end

 #

 # View the documentation for the provider you're using for more

 # information on available options.

 

 # Enable provisioning with Puppet stand alone.  Puppet manifests

 # are contained in a directory path relative to this Vagrantfile.

 # You will need to create the manifests directory and a manifest in

 # the file base.pp in the manifests_path directory.

 #

 # An example Puppet manifest to provision the message of the day:

 #

 # # group { "puppet":

 # #   ensure => "present",

 # # }

 # #

 # # File { owner => 0, group => 0, mode => 0644 }

 # #

 # # file { '/etc/motd':

 # #   content => "Welcome to your Vagrant-built virtual machine!

 # #               Managed by Puppet.\n"

 # # }

 #

 # config.vm.provision :puppet do |puppet|

 #   puppet.manifests_path = "manifests"

 #   puppet.manifest_file  = "site.pp"

 # end

 

 # Enable provisioning with chef solo, specifying a cookbooks path, roles

 # path, and data_bags path (all relative to this Vagrantfile), and adding

 # some recipes and/or roles.

 #

 # config.vm.provision :chef_solo do |chef|

 #   chef.cookbooks_path = "../my-recipes/cookbooks"

 #   chef.roles_path = "../my-recipes/roles"

 #   chef.data_bags_path = "../my-recipes/data_bags"

 #   chef.add_recipe "mysql"

 #   chef.add_role "web"

 #

 #   # You may also specify custom JSON attributes:

 #   chef.json = { :mysql_password => "foo" }

 # end

 

 # Enable provisioning with chef server, specifying the chef server URL,

 # and the path to the validation key (relative to this Vagrantfile).

 #

 # The Opscode Platform uses HTTPS. Substitute your organization for

 # ORGNAME in the URL and validation key.

 #

 # If you have your own Chef Server, use the appropriate URL, which may be

 # HTTP instead of HTTPS depending on your configuration. Also change the

 # validation key to validation.pem.

 #

 # config.vm.provision :chef_client do |chef|

 #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"

 #   chef.validation_key_path = "ORGNAME-validator.pem"

 # end

 #

 # If you're using the Opscode platform, your validator client is

 # ORGNAME-validator, replacing ORGNAME with your organization name.

 #

 # If you have your own Chef Server, the default validation client name is

 # chef-validator, unless you changed the configuration.

 #

 #   chef.validation_client_name = "ORGNAME-validator"

end





●トラブルシューティング

1. vagrant再起動($ vagrant reload)の時、以下のエラーメッセージが表示される

------------------------------------------------------------------------------------------

There are errors in the configuration of this machine. Please fix

the following errors and try again:

 

Vagrant:

* Unknown configuration section 'vbguest'.

------------------------------------------------------------------------------------------

# config.vbguest.auto_update = true //の行を以下のように修正

if Object.const_defined? 'VagrantVbguest'

   config.vbguest.auto_update = false

   config.vbguest.no_remote = true

end

 

参考(https://groups.google.com/forum/#!topic/vagrant-up/hNAyk57BLaM)



2. 1.で直してvagrant再起動($ vagrant reload)の時、以下のエラーメッセージが表示される

------------------------------------------------------------------------------------------

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 apache | cut -d: -f3`,dmode=777,fmode=666 /home/vagrant/src /home/vagrant/src

mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g apache`,dmode=777,fmode=666 /home/vagrant/src /home/vagrant/src

------------------------------------------------------------------------------------------

(★以下のことを実行する前提として、vagrantapacheがインストールされている必要がある)

①$ vagrant ssh

 

(効果があるかはわからないが、前もって

$ sudo mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group apache | cut -d: -f3`,dmode=777,fmode=666 /home/vagrant/src /home/vagrant/src

 

$ sudo mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g apache`,dmode=777,fmode=666 /home/vagrant/src /home/vagrant/src

の2つのコマンドを試した

)

 

②$ sudo /etc/init.d/vboxadd setup

実行終了したら $ exit

 

vagrant halt

 

vagrant up

 

参考(http://qiita.com/osamu1203/items/10e19c74c912d303ca0b)

 

3. http://localhost:8080/ にアクセスすると、「データを受信して​​いません」とブラウザに表示される

⑨の設定を全て行えば、このエラーは出ない

 

4. http://localhost:8080/ にアクセスすると、「Forbidden 403」とブラウザに表示される

/home/vagrant のアクセス権がないのが原因なので、以下のコマンドを入力

$ sudo chmod 755 /home/vagrant

参考(http://www.iscs.jp/tips/tips.php?id=22)

 

5. CakePHPのフォルダにアクセスしても「Not Found」と表示される

⑨の設定を全て行えば、このエラーは出ない