takafumi blog

日々の勉強メモ

【PHP】 PHPBrewでPHP7をインストール

環境   CentOS 7.0 phpbrew 1.20.5 PHP 7.0.1

PHPBrewを使って、PHP7を試してみる。

事前準備

PHPBrewを使うには、php-cliの5.3以上が必要になる。

$ sudo yum install -y php-cli

そして、他に必要になるモジュールを追加。
Apacheと連携するので、httpdも追加。
httpdをインストールすると、同時にapxs(apacheに拡張モジュールを、コンパイルしてくれるツール)もインストールされる。

$ sudo yum install -y httpd httpd-devel

$ sudo yum install -y bzip2-devel
$ sudo yum install -y libxml2-devel
$ sudo yum install -y libxslt-devel 

apxsでモジュールを追加しるため、以下のディレクトリにパーミッションを付与。

sudo chmod -R oga+rw /usr/lib64/httpd/modules
sudo chmod -R oga+rw /etc/httpd/conf

そして、GitHubからPHPBrewを取得する。PHPBrewはphpr形式で提供されているので、ファイルを取ってくれば動作する。

cd /usr/local/bin
sudo curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
sudo chmod +x phpbrew

必要に応じて、. ~/.bash_profileをしておく。


PHPBrewを使ったPHPのインストール

まず初期化する

$ phpbrew init

インストールできるPHPのバージョンを確認

$ phpbrew known
===> Fetching release list...
[==================================================================] 1.31/1.31KB 100%
7.0: 7.0.1, 7.0.0 ...
5.6: 5.6.16, 5.6.15, 5.6.14, 5.6.13, 5.6.12, 5.6.11, 5.6.10, 5.6.9 ...
5.5: 5.5.30, 5.5.29, 5.5.28, 5.5.27, 5.5.26, 5.5.25, 5.5.24, 5.5.23 ...
5.4: 5.4.45, 5.4.44, 5.4.43, 5.4.42, 5.4.41, 5.4.40, 5.4.39, 5.4.38 ...
5.3: 5.3.29, 5.3.28, 5.3.27, 5.3.26, 5.3.25, 5.3.24 ...
You can run `phpbrew update` to get a newer release list.

このバージョンを選択できる。今回は最新の7.0.1をインストール。

yum installとかだと、php-pdo,php-mysqlyumで導入できるが、phpbrewだと独自のオプションを選択し導入する。
選択可能なオプションはいかのように確認できる。

$ phpbrew variants
Variants:
  all, apxs2, bcmath, bz2, calendar, cgi, cli, ctype, curl, dba, debug, dom,
  embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, gmp, hash,
  iconv, icu, imap, inifile, inline, intl, ipc, ipv6, json, kerberos, libgcc,
  mbregex, mbstring, mcrypt, mhash, mysql, opcache, openssl, pcntl, pcre,
  pdo, pgsql, phar, phpdbg, posix, readline, session, soap, sockets, sqlite,
  static, tidy, tokenizer, wddx, xml, xml_all, xmlrpc, zip, zlib, zts


Virtual variants:
  dbs: sqlite, mysql, pgsql, pdo
  mb: mbstring, mbregex
  neutral:
  default: bcmath, bz2, calendar, cli, ctype, dom, fileinfo, filter, ipc,
  json, mbregex, mbstring, mhash, mcrypt, pcntl, pcre, pdo, phar, posix,
  readline, sockets, tokenizer, xml, curl, openssl, zip
  everything: dba, ipv6, dom, calendar, wddx, static, inifile, inline, cli,
  fpm, ftp, filter, gcov, zts, json, hash, exif, mbstring, mbregex, libgcc,
  pdo, posix, embed, sockets, debug, phpdbg, zip, bcmath, fileinfo, ctype,
  cgi, soap, pcntl, intl, phar, session, tokenizer, opcache, imap, tidy,
  kerberos, xmlrpc, pcre, mhash, mcrypt, zlib, curl, readline, gd, icu,
  openssl, mysql, sqlite, pgsql, xml, xml_all, gettext, iconv, bz2, ipc,
  gmp


Using variants to build PHP:

  phpbrew install php-5.3.10 +default
  phpbrew install php-5.3.10 +mysql +pdo
  phpbrew install php-5.3.10 +mysql +pdo +apxs2
  phpbrew install php-5.3.10 +mysql +pdo +apxs2=/usr/bin/apxs2

今回はPDOとMYSQLを選択し、PHP7.0.1を使えるようにする。

$ phpbrew install 7.0.1  +pdo +mysql +apxs2=/usr/bin/apxs
===> phpbrew will now build 7.0.1
===> Loading and resolving variants...
Checking distribution checksum...
Checksum matched: c999d29cb8861aa71574a11e8c635691
===> Distribution file was successfully extracted, skipping...
Found existing Makefile, running make clean to ensure everything will be rebuilt.
You can append --no-clean option after the install command if you don't want to rebuild.
===> Running make clean: make -C /home/vagrant/.phpbrew/build/php-7.0.1 --quiet clean
===> Applying patch - apxs2 module version name ...
Found existing build.log, renaming it to /home/vagrant/.phpbrew/build/php-7.0.1/build.log.1451999982
===> Configuring 7.0.1...


Use tail command to see what's going on:
   $ tail -F /home/vagrant/.phpbrew/build/php-7.0.1/build.log


===> Building...
Build finished: 3.3 minutes.
Installing...
---> Creating php-fpm.conf
---> Creating php.ini
---> Copying /home/vagrant/.phpbrew/build/php-7.0.1/php.ini-development
Found existing /home/vagrant/.phpbrew/php/php-7.0.1/etc/php.ini.
---> Found date.timezone is not set, patching...
Congratulations! Now you have PHP with 7.0.1 as php-7.0.1
To use the newly built PHP, try the line(s) below:

    $ phpbrew use php-7.0.1

Or you can use switch command to switch your default php to php-7.0.1:

    $ phpbrew switch php-7.0.1

Enjoy!

これでphp7.0.1がコンパイルされる。 現在PHPBrewで使えるバージョンは以下で確認

$ phpbrew list
* (system)
  php-7.0.1

ここで以下のコマンドを打っておく

$ . ~/.phpbrew/bashrc

一時的にバージョンを切り替える場合は

$ phpbrew use 7.0.1

デフォルトを変更するなら

$ phpbrew switch 7.0.1

とする。

$ php -v
PHP 7.0.1 (cli) (built: Jan  5 2016 23:15:33) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies


Apacheと連携

特に特別な事はなし。

httpd.confに以下の行を追加。

<IfModule mime_module>

    # etc.

    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
</IfModule>

AddType~の2行を、に追加する。

その後、httpdをrestartすれば完了。

f:id:takafumi-s:20160105232148j:plain