12月から新しい環境でお仕事をさせていただくことになり、配属されるプロジェクトがVagrantを使ったPHPのプロジェクトということだったので思い出しもかねて環境構築してみた。
Vagrantの導入
VirturalBoxのインストール
Downloads – Oracle VM VirtualBox
Vagrantのインストール
Download - Vagrant by HashiCorp
Boxの導入
ワークディレクトリの作成
$ mkdir vagrantwork $ cd vagrantwork
boxの追加
$ vagrant box add CentOS7 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box $ vagrant init
Vagrantfileの編集
# 追記 config.vm.network "private_network", ip: "192.168.33.10"
仮想OSを起動
$ vagrant up
仮想OSにssh
$ vagrant ssh
PHP環境の準備
$ sudo yum -y install httpd $ sudo systemctl start httpd.service $ sudo systemctl enable httpd.service $ sudo systemctl stop firewalld $ sudo systemctl disable firewalld $ sudo rm -rf /var/www/html $ sudo ln -fs /vagrant /var/www/html $ sudo yum -y install epel-release $ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm $ sudo yum -y install --enablerepo=remi,remi-php71 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt php-pear $ sudo pecl install xdebug $ sudo systemctl restart httpd.service
Xdebug設定を/etc/php.iniに追記
[xdebug] zend_extension=/opt/remi/php70/root/usr/lib64/php/modules/xdebug.so ; Xdebugモジュールのパス xdebug.remote_enable=On xdebug.remote_autostart=On xdebug.remote_host="PHPStorm側のPCのIP" xdebug.remote_port=9000 xdebug.idekey="PHPSTORM"
PHPStormの設定
PHPStorm>Preferences>言語 & フレームワーク>PHP>デバッグ
デバッグ・ポートの設定をphp.iniに記載したxdebug.remote_portの値に設定
言語 & フレームワーク>PHP>サーバー
+ボタンを押して新しくサーバーを追加する。
名前:任意のもの
ホスト:Vagrantfileに記載したprivate_networkのIPアドレス
[パス・マッピングを使用する]にチェックを入れ、サーバ上の絶対パスを設定する。
実行> 構成の編集
+ボタンを押してPHP Remote Debugを追加する。
名前:任意のもの
サーバー:上記で登録したサーバーを選択
ideキー(セッションid):php.iniに追記したxdebug.idekeyに設定した値を設定する
Xdebugの実行
行にブレークポイントを設定し、エディタ右上の受話器ボタンをアクティブ状態にして対象のWebサーバにアクセスをするとデバッグ実行ができる。