CentOS6にCakePHP2の環境を用意したVagrant Box

* 2014/02/21にBoxファイルをアップデートしました。

CentOSにCakePHP2の環境を用意したVagrant Boxを作成してみました。CakePHP2の開発やお試しに使えるものです。「http://202.133.112.84/cent6-cakephp2.box」からダウンロードできます。良かったら、使ってみてください。開発用なのであしからず。Vagrantをインストールしていれば、下記のコマンドを実行するだけでCakePHP2の環境が手に入ります。

$ mkdir -p ~/workspace/vagrant/cent6-cakephp2
$ vagrant box add cent6-cakephp2 http://202.133.112.84/cent6-cakephp2.box
$ cd ~/workspace/vagrant/cent6-cakephp2
$ vagrant init cent6-cakephp2
$ vagrant up

ライセンスについて
CentOSの利用ライセンス、EPELリポジトリ、remiリポジトリの利用ライセンスに同意が必要です。また、Apache、MySQL、PHP、CakePHP、VirtualBoxのGuest Addition、これらのソフトウェアの利用ライセンスに同意できる人だけ、利用をしてください。GNU Public License v2、PHPライセンス、Apacheライセンス、VirtualBox_PUEL – Oracle VM VirtualBoxのライセンスに同意が必要です。大雑把にいうと、個人用途、評価用途であれば使えるということです。


ホストオンリーネットワーク
ホストオンリーネットワークとして192.168.91.10を使います。すでに使っているネットワークとぶつかる場合は調整をしてください($HOME/.vagrand.d/にこのBox用 _Vagrantfile があるはずなので、そちらも修正が必要になるはずです)。Vagrantによるホストオンリーネットワークの追加がうまくいかない場合は、VirtualBoxの環境設定を開いてネットワークの項目でホストオンリーネットワークを追加してください。まずは、IP v4には192.168.91.1、IP v4のネットマスクには255.255.255.0を指定したものを用意してください(192.168.91.1 であって、192.168.91.10 ではないので気をつけてください)。

sshアカウント
veeweeを使ってベースとなるBoxをCentOS6.4 minimalのDVDイメージから作成しました。そこへgroupinstallでデスクトップや日本語サポートのパッケージを追加してあります。アカウントは、vagrantアカウントを用意してあります。パスワードはアカウント名と同じです。このアカウントはsudo権限も所有しています。sshはホストの2222番ポートをポートフォワードして仮想マシンの22番ポートへつなげるように起動しています(複数のvagrant boxを利用している場合はvagrantが調整する事もあるようです。vagrant sshでログインするのが確実でしょう)。

vagrant sshを使う場合は、Vagrantfileがあるディレクトリで下記コマンドを実行します。

$ vagrant ssh

普通にsshコマンドで接続する方法もあります。

$ ssh -l vagrant -p 2222 localhost

公開鍵を使う場合は、「パスフレーズなし」の秘密鍵が、ホームディレクトリの .vagrant.d/insecure_private_key にありますから、それを指定して接続しても良いでしょう。よくわからない場合はパスワードで接続してください。

Apache
/etc/httpd/conf/httpd.conf の最後に下記を追加して、192.168.91.10:80 のバーチャルホストを追加してあります。

NameVirtualHost 192.168.91.10:80
<VirtualHost 192.168.91.10:80>
  DocumentRoot /vagrant/www/192.168.91.10/webroot
  ServerName 192.168.91.10
  ErrorLog logs/192.168.91.10-error_log
  CustomLog logs/192.168.91.10-access_log common
  <Directory /vagrant/www/192.168.91.10/webroot>
    AllowOverride All
  </Directory>
  <Location />
    Options Indexes FollowSymLinks
    Order allow,deny
    Allow from all
  </Location>
</VirtualHost>

Vagrantfileがあるディレクトリは共有フォルダになっています。そこへwww/192.168.91.10/webroot/を作成してください。ここが、http://192.168.91.10/ のドキュメントルートとなっています。

サンプルCakePHP2アプリ
サンプルCakePHP2アプリ用には下記設定を/etc/httpd/conf.d/cakephp.confにしてあります。

Alias /cakephp2_apps/user_app /var/www/cakephp2_apps/cakephp-2.4.2_apps/user_app/app/webroot
<Directory /var/www/cakephp2_apps/cakephp-2.4.2_apps/user_app/app/webroot>
    AllowOverride All
</Directory>
<Location /cakephp2_apps/user_app>
    Options Indexes FollowSymLinks
    Order allow,deny
    Allow from all
</Location>

設定ファイルを見てわかるように、/var/www/cakephp2_apps/cakephp-2.4.2_apps/user_app が実体です。Webブラウザからは下記で接続できます。CakePHP2のcake bakeコマンドでuser_appデータベースのusersテーブルから自動生成してあります。

  • http://192.168.91.10/cakephp2_apps/user_app/
  • http://192.168.91.10/cakephp2_apps/user_app/users/

MySQL
MySQLデータベースの管理をするには、vagrant sshでマシンへログインして、MySQLデータベース管理者アカウントrootでパスワード「なし」でログインします。user_appデータベースにusersテーブルがあります。

$ mysql -u root 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.34 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| user_app           |
+--------------------+
5 rows in set (0.05 sec)

mysql> use user_app;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------+
| Tables_in_user_app |
+--------------------+
| users              |
+--------------------+
1 row in set (0.00 sec)

mysql> desc users;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| username | tinytext | NO   |     | NULL    |                |
| password | tinytext | NO   |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
同じタグの記事: CakePHP2
同じタグの記事: CentOS
同じタグの記事: Vagrant
同じタグの記事: VirtualBox
同じカテゴリの記事: Linux
関連書籍: CentOS
関連書籍: VirtualBox