phpenvを導入してphpのバージョン管理をする

2016年2月4日

動作環境

サーバー: さくら 1G SSD
OS: CentOS 6.5

 

テストサーバーの存在

どんなシステムを作るにしてもテスト用の環境は必要だと思います。

私は主にPHPを利用してWEBアプリを開発しているのですが、レンタルサーバー毎にPHPのバージョンが微妙に違っていて、開発環境では動くけどサーバーでは動かないって事がよくあります。(クロージャとかもにょもにょ

そこで、レンタルサーバーに合わせてPHPのバージョンを好きな時に自由に入れ替えれるツール無いかなと調べてみました。

 

phpenvとか言う謎の存在

いや、謎でも何でもなく有名なやつですよ。
私が知らないだけです。

■phpenv
https://github.com/CHH/phpenv

簡単に言うとPHPのバージョンを変更を簡単に行えるやつです。

すごいヤツです。

phpenv設定手順

$ cd
$ git clone https://github.com/CHH/phpenv.git
$ cd phpenv/bin
$ sh phpenv-install.sh
$ vi ~/.bashrc

~/.bashrcの最後に以下を追記

 
~~~~~~~~省略~~~~~~~~~~~~
export PATH="$HOME/.phpenv/bin:$PATH" 
eval "$(phpenv init -)"

以下のもので設定

$ source ~/.bashrc

うまくいってるかを動作確認

$ phpenv
rbenv 1.0.0-16-gd6ab394
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

 

php-build設定手順

$ git clone https://github.com/CHH/php-build.git ~/.phpenv/plugins/php-build

 

もろもろ必要なものを設定

$ sudo yum install -y httpd httpd-devel
$ sudo yum -y --enablerepo=epel install re2c
$ sudo yum -y --enablerepo=epel install libxml2-devel bison bison-devel openssl-devel curl-devel libjpeg-devel libpng-devel libmcrypt-devel readline-devel libtidy-devel libxslt-devel libmcrypt libmcrypt-devel

 

PHPを設定

まずは導入可能なバージョンを確認

$ phpenv install --list

今回は5.6.17をインストールしてみます。

$ phpenv install 5.6.17
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 5.6.17 into /home/vagrant/.phpenv/versions/5.6.17
[Downloading]: https://secure.php.net/distributions/php-5.6.17.tar.bz2
[Preparing]: /tmp/php-build/source/5.6.17
[Compiling]: /tmp/php-build/source/5.6.17
[xdebug]: Installing version 2.3.3
[xdebug]: Compiling xdebug in /tmp/php-build/source/xdebug-2.3.3
[xdebug]: Installing xdebug configuration in /home/vagrant/.phpenv/versions/5.6.17/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
[Info]: Enabling Opcache...
[Info]: Done
[Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.5.6.17.20160204041403.log
[Success]: Built 5.6.17 successfully.

インストールしたPHPの影響範囲を設定する。

$ phpenv global 5.6.17
$ phpenv local 5.6.17

設定されたPHPを確認する

$ php -v
PHP 5.6.17 (cli) (built: Feb  4 2016 04:23:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans

というわけで、PHPの設定が無事できました。

 

おまけ

phpenvをFreeTypeに対応させる

phpenvとapacheを連携する

  • この記事を書いた人

カバノキ

印刷会社のWEB部隊に所属してます。 WEB制作に携わってから、もう時期10年になります。 普段の業務では、PHPをメインにサーバーサイドの言語を扱っています。 最近のお気に入りはJavascriptです。 Vue.jsを狂喜乱舞しながら、社内に布教中です。

-レンタルサーバー
-, , ,