ChefでVagrantの仮想マシンにLAMP環境を構築してみる
LAMP環境のCookbookをとりあえず作成してみる
の続き
前回作成したLAMP環境のCookbookをVagrantで作成した仮想マシンに
適用してみる。
Vagrantで仮想マシンを作成
$ cd 適当なディレクトリ $ mkdir sample $ cd sample $ vagrant init centos64
仮想マシンのVagrantfileの設定を行う。
後ほど設置するCakePHPでは、書き込み権限がないとエラーになるので、
config.vm.synced_folderに書き込み権限を設定する。
また、hostnameを設定しないと、Apacheの起動時に
Starting httpd: httpd: apr_sockaddr_info_get() failed for vagrant-centos64.vagrantup.com
のエラーが出るので、config.vm.hostnameの設定をする。
$ vi Vagrantfile
・・・ config.vm.network "private_network", ip: "192.168.33.10" ←コメントを解除してIPアドレスを設定 ・・・ config.vm.synced_folder "./", "/vagrant", :mount_options => ["dmode=777,fmode=777"] ←追加 config.vm.hostname = "localhost" ←追加
仮想マシンを起動する。
$ vagrant up
仮想マシンにホスト名指定でアクセスできるようにしておく。
$ vagrant ssh-config --host sample >> ~/.ssh/config
chef-soloでCookbookを仮想マシンに反映
chef-repoのディレクトリに移動し、
$ cd chef-repoのディレクトリ
仮想マシンにchefの設定を行う。
$ knife solo prepare vagrant@sample
nodesのjsonを設定し、
$ vi nodes/sample.json
{ "db": { "rootpass": "", "user": "test", "pass": "test" }, "run_list": [ "recipe[lamp]" ] }
仮想マシンに反映する。
$ knife solo cook vagrant@sample
CakePHPを設置してみる
CakePHPをサイトからダウンロードして、sampleディレクトリに展開する。
sampleのディレクトリに移動し、
$ cd sampleのディレクトリ
databaseの設定を行う。
今回は、とりあえずtestデータベースにtestユーザーでアクセスするよう設定する。
$ mv cakephp/app/Config/database.php.default cakephp/app/Config/database.php
$ vi cakephp/app/Config/database.php
・・・ public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'test', 'password' => 'test', 'database' => 'test', 'prefix' => '', //'encoding' => 'utf8', ; ・・・
core.phpをセキュリティのやつに適当な文字列や数字を設定して、
$ vi cakephp/app/Config/core.php
/** * A random string used in security hashing methods. */ Configure::write('Security.salt', '適当な文字列'); /** * A random numeric string (digits only) used to encrypt/decrypt strings. */ Configure::write('Security.cipherSeed', '適当な数字');
http://192.168.33.10/cakephp/
にアクセスすると、CakePHPのトップページが表示される。