マソム自宅サーバ構築の記録

  • PHP5.2をソースからインストール
  • 作成日:08/2/28    更新日:09/08/07

前置き

最新版(2008年2月現在)の5.2.5をインストールすることにします。
DBはMySQLとPostgreSQLを使えるようにします。

Apache2のインストール

先にApacheのインストールを済ませておきます。

MySQL5.0のインストール

先にMySQL5.0のインストールを済ませておきます。

PostgreSQL8.3.0のインストール

先にPostgreSQL8.3.0のインストールを済ませておきます。

libmcryptのインストール

MySQLの操作にphpMyAdminを使う為、Mcrypt暗号化関数を使えるようにPHPをインストールします。
その為、libmcryptをインストールします。
私はユーザディレクトリにソース置き場を作ってあるので、そこにダウンロードします。/usr/local/srcでも良いと思います。
$ cd /home/ユーザ名/src
ダウンロード。
$ wget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0
ソースを展開します。
$ tar xzf libmcrypt-2.5.8.tar.gz
ソースディレクトリに移動しコンパイルします。
$ cd libmcrypt-2.5.8
$ ./configure --disable-nls --disable-posix-threads 2>&1 |tee configure.log
$ make 2>&1 |tee make.log
インストールします。
$ su # make install 2>&1 |tee install.log

PHP5.2のソースからのインストール

PHP5.2.5 をインストールするディレクトリを作成しておきます。
$ su
# mkdir /usr/local/php5
# exit
PHP5.2.5 をダウンロードします。
私はユーザディレクトリにソース置き場を作ってあるので、そこにダウンロードします。/usr/local/srcでも良いと思います。
$ cd /home/ユーザディレクトリ/src
wgetでダウンロード 。
$ wget http://jp.php.net/get/php-5.2.5.tar.gz/from/this/mirror
ソースを展開します。
$ tar xzf php-5.2.5.tar.gz
ソースディレクトリに移動しコンパイルします。
  ./configureのオプションは
  --prefix=/usr/local/php5:デフォルト以外のディレクトリにインストールする時に指定します。デフォルトは/usr/localです。
  --with-apxs2=/usr/local/apache2/bin/apxs :Apache2のDSOモジュールとます。
  --enable-versioning :PHP4とPHP5を併用出来るように指定しておきます。
  コマンドライン版CLI SAPIを使いますがデフォルトでオンになるそうなので--enable-cliは指定しません。
  --disable-cgi :CGI版を使用しない時に指定します。
  --enable-mbstring :マルチバイト文字列関数を有効にします。
  注)--enable-mbstr-enc-transは無くなったそうです。php.iniのmbstring.encoding_translationを使います。
  --enable-memory-limit :スクリプトが確保できる最大メモリを制限できるようにします。
                                                PHP5.2.1よりデフォルトでOnになるため無くなりました。
  --enable-zend-multibyte
--with-openssl=/usr :SSLを使うなら、OpenSSLのインストールディレクトリを指定します。
  --with-mysql=/usr/local/mysql50 : MySQL4.1以前を使う時に、MySQL関数を利用できるようにします。
  --without-mysql : MySQL4.1以降を使う時に、mysqli拡張サポートだけを利用したい時はMySQL関数を無効にします。
  --with-mysqli=/usr/local/mysql50/bin : MySQL4.1以降を使う時に、mysqli拡張サポートを利用できるようにします。
  --with-pdo-mysql=/usr/local/mysql50 : MySQL用PDOドライバ(PDO=PHP Data Objects 拡張モジュール)を利用できるようにします。
  --with-pgsql=/usr/local/pgsql : postgreSQLを使う時に、postgreSQL関数を利用できるようにします。
  --with-pdo-pgsql=/usr/local/pgsql : postgreSQL用PDOドライバ(PDO=PHP Data Objects 拡張モジュール)を利用できるようにします。
  --with-mcrypt :Mcrypt暗号化関数を使えるようにする
  --enable-sqlite-utf8 :SQLiteでUTF-8を使えるようにする
  --without-iconv :iconvサポートを外す
  --with-zlib:PHPインストール時にzlibのインストールも含める場合に指定します。通常はインストール済みなので指定の必要ありません。
               zlibはgzip 等に使われている圧縮アルゴリズムのライブラリ。pecl インストーラやPDOでも必要です。

 今回はPDOを試してみたいので、そのオプションも含めました。DBを使う為には、必須ではありませんから含めなくてもOKです。
 なお、私は./configureでこんなエラーが出ました。
$ cd php-5.2.5
$ ./configure --prefix=/usr/local/php5 \
              --with-apxs2=/usr/local/apache2/bin/apxs \
              --disable-cgi \
              --enable-mbstring \
              --enable-zend-multibyte \
              --with-openssl=/usr \
              --with-mysql=/usr/local/mysql50 \
              --with-mysqli=/usr/local/mysql50/bin/mysql_config \
              --with-pdo-mysql=/usr/local/mysql50 \
              --with-pgsql=/usr/local/pgsql \
              --with-pdo-pgsql=/usr/local/pgsql \
              --with-mcrypt \
              --with-gd \
              --with-jpeg-dir=/usr \
              --with-png-dir=/usr \
              --with-zlib-dir=/usr \
              2>&1 |tee configure.log
$ make 2>&1 |tee make.log
$ make test 2>&1 |tee make_test.log
make test でこんなエラーが出ました。
取り合えずインストールします。
$ su
# make install 2>&1 |tee install.log
Installing PHP SAPI module:       apache2handler
/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apache2/build/libtool' libphp5.la
  /usr/local/apache2/modules
/usr/local/apache2/build/libtool --mode=install cp libphp5.la /usr/local/apache2/modules/
cp .libs/libphp5.so /usr/local/apache2/modules/libphp5.so
cp .libs/libphp5.lai /usr/local/apache2/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /home/ユーザ名/src/php-5.2.5/libs'
chmod 755 /usr/local/apache2/modules/libphp5.so
[activating module `php5' in /usr/local/apache2/conf/httpd.conf]
Installing PHP CLI binary:        /usr/local/php5/bin/
Installing PHP CLI man page:      /usr/local/php5/man/man1/
Installing build environment:     /usr/local/php5/lib/php/build/
Installing header files:          /usr/local/php5/include/php/
Installing helper programs:       /usr/local/php5/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php5/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php5/lib/php/
[PEAR] Console_Getopt - installed: 1.2.3
[PEAR] Archive_Tar    - installed: 1.3.2
[PEAR] Structures_Graph- installed: 1.0.2
pear/PEAR can optionally use package "pear/XML_RPC" (version >= 1.4.0)
[PEAR] PEAR           - installed: 1.6.1
Wrote PEAR system config file at: /usr/local/php5/etc/pear.conf
You may want to add: /usr/local/php5/lib/php to your php.ini include_path
Installing PDO headers:          /usr/local/php5/include/php/ext/pdo/
warningが出ていました。メッセージにしたがって以下のようにコマンドを実行しましたが、再インストールでも結果は変わりません。
調べたところ、同様の問題が PHP 5.1.4でも起きていたようです。 PHP 5.1.4 では、気にしなくてOKのようです。取りあえず私も気にしないことにしました。ですから、以下のコマンドを実行する必要は無いと思います。
# libtool --finish /home/ユーザ名/src/php-5.2.5/libs
PATH="$PATH:/sbin" ldconfig -n /home/ユーザ名/src/php-5.2.5/libs
----------------------------------------------------------------------
Libraries have been installed in:
   /home/ユーザ名/src/php-5.2.5/libs

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

PHPの設定

PHPの設定ファイルをコピーします。
# cp php.ini-recommended /usr/local/php5/lib/php.ini
PHPの設定ファイルを編集します。
# vi /usr/local/php5/lib/php.ini

以下のディレクティブを変更します。
マルチバイト文字列関数関係の各ディレクティブは、コメントアウトされている設定例と実際のデフォルト値が違うので注意が必要です。
  私の環境での文字コードは、OS:UTF-8、ソースコード:色々、http出力:色々、DB文字コード:UTF-8、となるので、以下のようにしました。
  mbstring.language = Japanese : デフォルトはneutral(UTF-8)だとドキュメントにあるので変更します。
  mbstring.internal_encoding = UFT-8 :内部文字コードをOSとDBに合わせてUFT-8にします。デフォルトはNULLです。
  mbstring.http_input : デフォルトはpass(無変換)だとドキュメントにあります。http入力文字エンコーディングを変更しないのでそのままにします。
  mbstring.http_output :デフォルトはpass(無変換)だとドキュメントにあります。http出力文字エンコーディングを変更しないのでそのままにします。
  mbstring.encoding_translation = On :HTTP入力文字エンコーディング検出および内部文字エンコーディングへの変換を行うのでOnにします。デフォルトはOffです。
  mbstring.detect_order = auto : 文字コード検出順をautoにします。デフォルト値はNULLで、検出文字コードがありません。
  mbstring.substitute_character : 無効な文字を代替する文字は特に指定しません。デフォルト値はNULLなのでそのままにします。
  mbstring.func_overload :シングルバイト対応の関数をmbstring関数でオーバーロード(置換)出来るそうですが、 これらの関数は使い分けているので 0: オーバーロード無し(デフォルト)のままにします。
  mbstring.script_encoding :ソースコードがSJISなどPHPで動作しない文字エンコーディングの時は指定が必要らしいのですが、取り合えず指定しません。
[mbstring]
; language for internal character representation.
;mbstring.language = Japanese
mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
;mbstring.internal_encoding = EUC-JP
mbstring.internal_encoding = UTF-8

; http input encoding.
;mbstring.http_input = auto

; http output encoding. mb_output_handler must be
; registered as output buffer to function
;mbstring.http_output = SJIS

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
;mbstring.encoding_translation = Off
mbstring.encoding_translation = On

; automatic encoding detection order.
; auto means
;mbstring.detect_order = auto
mbstring.detect_order = auto

; substitute_character used when character cannot be converted
; one from another
;mbstring.substitute_character = none;

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
;mbstring.func_overload = 0

; enable strict encoding detection.
;mbstring.strict_encoding = Off
PHPの設定ファイルを保存します。

Apacheの設定

次にApacheの設定ファイルを編集します。
PHP5のDSOモジュールが読み込まれる設定が追加されていることを確認します。
PHPのMIMEタイプを追加します。
# vi /usr/local/apache2/conf/httpd.conf
     ...省略...
LoadModule php5_module        modules/libphp5.so

<IfModule mod_php5.c>
    AddType application/x-httpd-php .php .php5 .phtml
    AddType application/x-httpd-php-source .phps
</IfModule>

     ...省略...
設定ファイルの文法チェックをするとエラーが出ました。
# /usr/local/apache2/bin/apachectl configtest
httpd: Syntax error on line 77 of /usr/local/apache2/conf/httpd.conf:
  Cannot load /usr/local/apache2/modules/libphp5.so into server:
  /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc:
  Permission denied
調べてみると、どうやら「SELinuxポリシーに適応されてエラーの出たライブラリの セキュリティコンテキストが適切に設定されていない」 為に出るメッセージだそうです。
ファイルコンテキストを見てみます。
# ls -lZ /usr/local/apache2/modules/libphp5.so
-rwxr-xr-x  root root user_u:object_r:usr_t            /usr/local/apache2/modules/libphp5.so
以下のコマンドで変更します。
# chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache2/modules/libphp5.so
context of /usr/local/apache2/modules/libphp5.so changed to system_u:object_r:textrel_shlib_t
もう一度ファイルコンテキストを見てみます。
# ls -lZ /usr/local/apache2/modules/libphp5.so
-rwxr-xr-x  root root system_u:object_r:textrel_shlib_t /usr/local/apache2/modules/libphp5.so
再度設定ファイルの文法チェックをするとOKでした。
# /usr/local/apache2/bin/apachectl configtest
Syntax OK
しかし、textrel_shlib_tの意味が分かっていません。課題です。何故なら、他のライブラリのコンテキストは変更前のlibphp5.soと同じだからです。
Apacheを再起動します。
# /usr/local/apache2/bin/apachectl stop
# /usr/local/apache2/bin/apachectl start

PHPの動作テスト

まず、phpinfo()を表示するスクリプトinfo.phpを用意します。info.phpのソースは以下の通りです。
<?phpinfo();?>
info.phpをApacheのドキュメントルートに置いて、LAN内のWindowsマシンのブラウザで表示してみます。phpinfo()の画面が表示できれば成功です。
次に、コマンドライン版が動作することを確認します 。
バージョン情報等を表示してみます。
$ /usr/local/php5/bin/php -v
PHP 5.2.5 (cli) (built: Feb 29 2008 17:47:02)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
無事に動作しました。しかし/usr/local/php5/binにパスが通っていないので「php -v」とは出来ません。
info.phpも呼んでみました 。 phpinfo()の内容が表示できれば成功です。
$ /usr/local/php5/bin/php /ドキュメントルートのディレクトリパス/info.php

PEARライブラリのインストール

使用したいPEARライブラリがあればインストールします。以下はPEAR::Mailをインストールした例です。
--alldepsで、依存関係にあるパッケージもインストールしてくれます。
# /usr/local/php5/bin/pear install --alldeps Mail
downloading Mail-1.1.14.tgz ...
Starting to download Mail-1.1.14.tgz (17,537 bytes)
......done: 17,537 bytes
downloading Net_SMTP-1.2.11.tgz ...
Starting to download Net_SMTP-1.2.11.tgz (10,385 bytes)
...done: 10,385 bytes
downloading Net_Socket-1.0.8.tgz ...
Starting to download Net_Socket-1.0.8.tgz (5,441 bytes)
...done: 5,441 bytes
downloading Auth_SASL-1.0.2.tgz ...
Starting to download Auth_SASL-1.0.2.tgz (5,645 bytes)
...done: 5,645 bytes
install ok: channel://pear.php.net/Mail-1.1.14
install ok: channel://pear.php.net/Net_Socket-1.0.8
install ok: channel://pear.php.net/Auth_SASL-1.0.2
install ok: channel://pear.php.net/Net_SMTP-1.2.11

各DBの接続チェック

MySQLのチェックはこちらを参照して下さい。
PostgreSQLのチェックはこちらを参照して下さい。

紆余曲折Tips

./configureのエラー

その1
configure: error: libjpeg.(a|so) not found.
調べてみるとlibjpeg-develが必要なようでした。
この辺はrpmパッケージで入っているようなのでチェックしてみると、確かにありません。
$ rpm -qa|grep libjpeg
libjpeg-6b-38.fc7
yumでインストールしました。
# yum install libjpeg-devel
Loading "installonlyn" plugin
Setting up Install Process
Parsing package install arguments
fedora                    100% |=========================| 2.1 kB    00:00
updates                   100% |=========================| 2.3 kB    00:00
primary.sqlite.bz2        100% |=========================| 2.4 MB    00:00
Resolving Dependencies
--> Running transaction check
---> Package libjpeg-devel.i386 0:6b-38.fc7 set to be updated

Dependencies Resolved

=============================================================================
Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
libjpeg-devel           i386       6b-38.fc7        updates           106 k

Transaction Summary
=============================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 106 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): libjpeg-devel-6b-3 100% |=========================| 106 kB    00:00
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: libjpeg-devel                ######################### [1/1]

Installed: libjpeg-devel.i386 0:6b-38.fc7
Complete!
その2    
configure: error: libpng.(a|so) not found.
調べてみるとlibpng-develが必要なようでした。
この辺はrpmパッケージで入っているようなのでチェックしてみると、確かにありません。
$ rpm -qa | grep libpng
libpng-1.2.16-1.fc7
yumでインストールしました。
# yum install libpng-devel
Loading "installonlyn" plugin
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package libpng-devel.i386 2:1.2.22-1.fc7 set to be updated
--> Processing Dependency: libpng = 2:1.2.22-1.fc7 for package: libpng-devel
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package libpng-devel.i386 2:1.2.22-1.fc7 set to be updated
---> Package libpng.i386 2:1.2.22-1.fc7 set to be updated

Dependencies Resolved

=============================================================================
Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
libpng-devel            i386       2:1.2.22-1.fc7   updates           105 k
Updating for dependencies:
libpng                  i386       2:1.2.22-1.fc7   updates           244 k

Transaction Summary
=============================================================================
Install      1 Package(s)
Update       1 Package(s)
Remove       0 Package(s)

Total download size: 349 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): libpng-1.2.22-1.fc 100% |=========================| 244 kB    00:00
(2/2): libpng-devel-1.2.2 100% |=========================| 105 kB    00:00
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating  : libpng                       ######################### [1/3]
  Installing: libpng-devel                 ######################### [2/3]
  Cleanup   : libpng                       ######################### [3/3]

Installed: libpng-devel.i386 2:1.2.22-1.fc7
Dependency Updated: libpng.i386 2:1.2.22-1.fc7
Complete!
その3
なお、with-zlib-dirでzlibのパスを指定しておかないと以下のエラーになります。
configure: error: PNG support requires ZLIB. Use --with-zlib-dir=<DIR>

make testのエラー

make test でエラーが出ました。 libcが脳死?・・・。
=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
Bug #35634 (Erroneous "Class declarations may not be nested" error raised) [Zend/tests/bug35634.phpt]
date OO cloning [ext/date/tests/oo_002.phpt]
Bug #16069 (ICONV transliteration failure) [ext/iconv/tests/bug16069.phpt]
iconv stream filter [ext/iconv/tests/iconv_stream_filter.phpt]
htmlentities() test 2 (setlocale / fr_FR.ISO-8859-15) [ext/standard/tests/strings/htmlentities02.phpt]
  (warn: possibly braindead libc)
htmlentities() test 4 (setlocale / ja_JP.EUC-JP) [ext/standard/tests/strings/htmlentities04.phpt]
  (warn: possibly braindead libc)
htmlentities() test 15 (setlocale / KOI8-R) [ext/standard/tests/strings/htmlentities15.phpt]
  (warn: possibly braindead libc)
Test setlocale() function : usage variations - Setting all available locales in the platform
  [ext/standard/tests/strings/setlocale_variation2.phpt]
=====================================================================