[osx] vagrant + centos58 + php4.4.9 + MySQL5.1 + xdebug

# 前提
- vagrantの基礎知識がある
- osxにvagrant導入済み

```
$ vagrant -v
Vagrant 1.7.4
```

# インストール手順

## centos5.8の仮想環境作成

```
$ vagrant box add centos58 https://dl.dropbox.com/u/17738575/CentOS-5.8-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos58' (v0) for provider: 
    box: Downloading: https://dl.dropbox.com/u/17738575/CentOS-5.8-x86_64.box
==> box: Successfully added box 'centos58' (v0) for 'virtualbox'!
```

## 作業ディレクトリの作成

```
$ mkdir ~/hoge/project
$ cd ~/hoge/project
$ mv ~/Downloads/php-4.4.9.tar.gz ./
```

## 仮想環境の作成(vagrant初期化)

```
$ vagrant init centos58
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
```

## 仮想環境の定義ファイルの編集

Vagrantfileを以下に編集します。

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

Vagrant.configure(2) do |config|
  config.vm.box = "centos58"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder ".", "/var/www"
end
```


## 仮想環境の起動

```
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: adplats_default_1458004622317_345
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

:
```

もし以下のエラーが発生したら後述のコマンドを叩いて復旧させます。

```
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 vagrant | cut -d: -f3` var_www /var/www
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` var_www /var/www

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device
```

```
$ vagrant ssh
$ cd /home/vagrant/
$ wget http://vault.centos.org/5.8/updates/x86_64/RPMS/kernel-devel-2.6.18-308.13.1.el5.x86_64.rpm
$ sudo yum -y localinstall ./kernel-devel-2.6.18-308.13.1.el5.x86_64.rpm 
$ exit
$ vagrant reload
```

## パッケージ追加

```
$ vagrant ssh
$ sudo -s -H
# yum -y install httpd
# yum -y install bison
# yum -y install flex
# yum -y install libjpeg-devel
# yum -y install libpng-devel
# yum -y install httpd-devel
# ln -s /usr/lib64/libjpeg.so /usr/lib/
# ln -s /usr/lib64/libjpeg.so /usr/lib/
```

## MySQL5.1のインストール

```
# cd /var/www/
# wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-14.ius.el5.noarch.rpm
# wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -Uvh *-release*rpm
# yum -y install mysql51 mysql51-devel mysql51-server mysqlclient15
```

## PHP-MySQL連携の準備
```
# cd /var/www/
# mkdir mysql
# cd mysql
# ln -s /usr/include/mysql include
# ln -s /usr/lib64/mysql lib
```
- php連携時にmysqlのlibを参照する設定です。
- 何か他にいい方法があれば教えて下さい！
- mysqlのlibを参照出来ない場合は以下のエラーが発生します。

> 
File 'NONEXISTENT/charsets/?.conf' not found (Errcode: 2)
Character set '#33' is not a compiled character set and is not specified in the 'NONEXISTENT/charsets/Index' file 

## PHP4.4.9のインストール

```
# cd /var/www/
# wget http://museum.php.net/php4/php-4.4.9.tar.gz
# tar xvzf php-4.4.9.tar.gz
# cd php-4.4.9
# ./configure --with-apxs2=/usr/sbin/apxs --enable-mbstring --with-iconv --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib --with-zlib-dir=/usr/lib --with-mysql=/var/www/mysql --with-gd --enable-xdebug
# make  
# make install
# cp /var/www/php-4.4.9/php.ini-recommended /usr/local/lib/php.ini
# vi /etc/httpd/conf/httpd.conf
```
httpd.confのphp_moduleをコメントアウト

```
LoadModule php4_module        /usr/lib64/httpd/modules/libphp4.so
↓
#LoadModule php4_module        /usr/lib64/httpd/modules/libphp4.so
```


httpd.confに追記

```
# For PHP4
LoadModule php4_module modules/libphp4.so
AddType application/x-httpd-php .php
```

## xdebugのインストール

```
# cd /var/www/
# mkdir xdebug
# cd xdebug/
# wget --no-check-certificate https://xdebug.org/files/xdebug-2.0.2.tgz
# tar xvzf xdebug-2.0.2.tgz 
# cd xdebug-2.0.2
# /usr/local/bin/phpize 
# ./configure --with-php-config=/usr/local/bin/php-config --enable-xdebug
# make
# make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20020429/
# vi /usr/local/lib/php.ini
```

以下を追記

```
zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20020429/xdebug.so"

;;;;;;;;;;;;;;;;;;
; xdebug         ;
;;;;;;;;;;;;;;;;;;
[xdebug]
xdebug.default_enable= On
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.remote_host = 192.168.33.1
xdebug.remote_port = 9000
xdebug.profiler_enable = On
xdebug.profiler_output_dir = "/var/log/httpd/xdebug.log"
xdebug.remote_autostart = On
xdebug.idekey=ECLIPSE_DBGP
```
- EclipseなどのIDEからリモートデバッグ可能


## デーモンの起動

```
# /etc/init.d/mysqld start
# /etc/init.d/httpd start
```

## 動作確認
```
# cd /var/www/html
# echo "<?php phpinfo(); ?>" > info.php
```

アクセス
http://192.168.33.10/info.php

## Cli
```
# /usr/local/bin/php -v
PHP 4.4.9 (cli) (built: Mar 24 2016 22:48:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
    with Xdebug v2.0.2, Copyright (c) 2002-2007, by Derick Rethans
```