トップ 新規 編集 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

PHP

PHP5

ThinkIT PHP5のオブジェクト指向
http://www.thinkit.co.jp/free/marugoto/1/1/1/1.html

ソースからのインストール

php-4.4.2をソースからインストールする。

#./configure \
--prefix=/usr/local/php4 \
--without-pgsql \
--with-mysql=/usr/local/mysql \
--with-apxs=/usr/local/apache/bin/apxs \
--enable-track-vars \
--enable-mbstring \
--enable-mbregex \
--enable-zend-multibyte \
--with-gd \
--with-freetype-dir=/usr/lib \
--with-jpeg-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-zlib-dir=/usr/lib \
--enable-gd-native-ttf \
--enable-gd-jis-conv

足らなかったパッケージはyumでインストール。

# yum install libjpeg-devel \
 libpng-devel \
 freetype-devel \
 flex \
 bison

configure中にbisonは1.28推奨と警告がでるが、パッケージには2.0-6しかなかったのでそのまま続行(汗)。

makeとmake install。時間は30分ぐらい。

# make
# make install

phpの解凍フォルダよりiniのコピー。

# cp /usr/local/src/php-4.4.2/php.ini-dist /usr/local/php4/lib/php.ini
# vi /usr/local/php4/lib/php.ini

mbstring関係のセミコロンを外す。
つづいてhttpd.confの設定。

LoadModule php4_module libexec/libphp4.so
AddType application/x-httpd-php .php

DirectoryIndex index.html index.cgi index.php

apacheの再起動。

#/usr/local/apache/bin/apachectl restart

phpの情報をphpinfo()で確認し、mbstring、mysql、gdのあたりが設定されているか確認する。

php-4.4.4

うちの環境では

/usr/local/apache2
/usr/local/mysql5

php-4.4.2をソースからインストールする。

# cd /usr/local/src
# wget http://www.php.net/get/php-4.4.4.tar.gz/from/jp2.php.net/mirror
# chmod 755 php-4.4.4.tar.gz
# tar -zxvf php-4.4.4.tar.gz
# ./configure \
 --prefix=/usr/local/php444 \
 --without-pgsql \
 --with-mysql=/usr/local/mysql5 \
 --with-apxs2=/usr/local/apache2/bin/apxs \
 --enable-track-vars \
 --enable-mbstring \
 --enable-mbregex \
 --enable-zend-multibyte \
 --with-gd \
 --with-freetype-dir=/usr/lib \
 --with-jpeg-dir=/usr/lib \
 --with-png-dir=/usr/lib \
 --with-zlib-dir=/usr/lib \
 --enable-gd-native-ttf \
 --enable-gd-jis-conv
 make
 make install

したあとに、httpd.confに勝手に

LoadModule php4_module libexec/libphp4.so

が記述される。
すでにこの記述がある場合は二重になってしまうので、チェックして削除する。

php-5.2.0

libxml2が必要。FedoraCore4ではlibxml2自体はrpmパッケージでインストールされているが、libxml2-develが入っていないためconfigureで失敗する。
さきにyumでlibxml2-develをインストールしておくこと。

# ./configure \
 --prefix=/usr/local/php520 \ 
 --without-pgsql \ 
 --with-mysql=/usr/local/mysql \
 --with-apxs2=/usr/local/apache/bin/apxs \
 --enable-mbstring \
 --enable-zend-multibyte \
 --with-gd \
 --with-freetype-dir=/usr/lib \
 --with-jpeg-dir=/usr/lib \
 --with-png-dir=/usr/lib \
 --with-zlib-dir=/usr/lib \
 --enable-gd-native-ttf \
 --enable-gd-jis-conv \
 --enable-sqlite-utf8 \
 --enable-memory-limit \
 --with-mysqli=/usr/local/mysql/bin/mysql_config

configureの最後にワーニング表示がでている場合は、どこかタイプミスしている。

--enable-track-varsは無い?みたい。sqliteはデフォで組み込まれる模様。ただしutf-8に対応させるのは別途指定。

PHP-5.2.1

PHP-5.2.0には結構バグがあるみたいでupdateする。memory-limitは早い段階で呼び出さないといけないみたいなので記述の順番を変更。

# ./configure \
 --prefix=/usr/local/php521 \
 --enable-memory-limit \
 --with-apxs2=/usr/local/apache/bin/apxs \
 --without-pgsql \
 --with-mysql=/usr/local/mysql \
 --with-mysqli=/usr/local/mysql/bin/mysql_config \
 --enable-mbstring \
 --enable-zend-multibyte \
 --with-gd \
 --with-freetype-dir=/usr/lib \
 --with-jpeg-dir=/usr/lib \
 --with-png-dir=/usr/lib \
 --with-zlib-dir=/ur/lib \
 --enable-gd-native-ttf \
 --enable-gd-jis-conv \
 --enable-sqlite-utf8

configureで↓のようなワーニングが出た。

configure: warning: You will need re2c 0.9.11 or later if you want to regenerate PHP parsers.
configure: warning: lemon versions supported for regeneration of libsqlite parsers: 1.0 (found: none).

5.2.0のときも出ていたのかは不明。
とりあえずそのままmakeもできて、make installしようとすると

# make install
  Installing PHP SAPI module:       apache2handler
  /usr/local/apache224/build/instdso.sh SH_LIBTOOL='/usr/local/apr224/build-1/libtool' libphp5.la /usr/local/apache224/modules
  /usr/local/apr224/build-1/libtool --mode=install cp libphp5.la /usr/local/apache224/modules/
  cp .libs/libphp5.a /usr/local/apache224/modules/libphp5.a
  cp .libs/libphp5.lai /usr/local/apache224/modules/libphp5.la
  libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-5.2.1/libs'
  chmod 755 /usr/local/apache224/modules/libphp5.so

まできてlibphp5.soがnot foundで止まってしまう。
たにかに/usr/local/apache/modulesにはlibphp5.aとlibphp5.laがあるのみ。

ググったところ、re2cは実際にはgccに含まれるため特に必要はないことと、lemonはlibsqliteに関するものなのでlibphp5.soには関係なさそうだった。
そもそもワーニングだし。

↓を参考にre2cとlemonを入れることにした。

Web、只今勉強中
http://d.hatena.ne.jp/kazukichop/comment?date=20061120
wget http://nchc.dl.sourceforge.net/sourceforge/re2c/re2c-0.11.0.tar.gz
tar xfz re2c-0.11.0.tar.gz 
cd re2c-0.11.0
./configure
make
make install
wget http://www.sqlite.org/cvstrac/getfile/sqlite/tool/lemon.c
gcc -o lemon lemon.c 
mv lemon /usr/local/bin

configureではワーニングがでなくなり、makeも完了。
そしてmake installでは... やっぱりlibphp5.soが作れてない(汗)。

で結局のところ、make cleanしてmakeしてmake installしていたんですが、make distcleanしてmakeしてmake installしたらよくなった模様?
make cleanではなにか残骸?が消えていなかったのか...

PHP-5.2.6

5.2.1と同じだが、--enable-memory-limitがデフォルトで有効になっているため必要なくなった。
というか、指定するとconfigureでエラーになる。

# ./configure \
--prefix=/usr/local/php526 \
--with-apxs2=/usr/local/apache/bin/apxs \
--without-pgsql \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql \
--enable-mbstring \
--enable-zend-multibyte \
--with-gd \
--with-freetype-dir=/usr/lib \
--with-jpeg-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-zlib-dir=/ur/lib \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-sqlite-utf8 \
--with-pdo-sqlite

--enable-zend-multibyteでコンパイルしたPHPならスクリプトがUTF-8のBOMありでもOK

php.iniの設定

[mbstring]
mbstring.language = Japanese
mbstring.internal_encoding = EUC-JP
mbstring.http_input = auto
mbstring.http_output = SJIS
mbstring.encoding_translation = Off
mbstring.detect_order = auto
mbstring.substitute_character = none;
mbstring.func_overload = 0

スクリプト内部で下記のように設定することも可能。
ただし、ini_setでは設定できないものもある。

mb_language('Japanese');
ini_set('mbstring.detect_order', 'auto');
ini_set('mbstring.http_input'  , 'auto');
ini_set('mbstring.http_output' , 'pass');
ini_set('mbstring.internal_encoding', 'UTF-8');
ini_set('mbstring.script_encoding'  , 'UTF-8');
ini_set('mbstring.substitute_character', 'none');
mb_regex_encoding('UTF-8');

.htaccessで指定する場合は

php_value mbstring.language Japanese
php_value mbstring.http_input EUC-JP
php_value mbstring.http_output EUC-JP
php_flag auto_detect_line_endings on
php_value output_handler mb_output_handler
php_value default_charset EUC-JP
php_value mbstring.internal_encoding EUC-JP
php_flag mbstring.encoding_translation off
php_value mbstring.detect_order auto
php_value mbstring.substitute_character none
php_flag magic_quotes_gpc off

値に対してはphp_valueで、On、Offに対してはphp_flagを使用するらしい。

文字コードの指定は

UTF-8
EUC-JP
SJIS
ISO-2022-JP(日本語メール)

 mbstring.internal_encoding

内部エンコーディングの設定。PHPの内部で文字データを扱う際の文字コードを設定しておく。
データベースと連携する場合、データベースと同じ文字コードにしておくと、いちいち変換処理をする必要がなくなる。
Shift-JISには問題点があるので、内部エンコーディングにはEUC-JPかUTF-8を使用するようにする。

mbstring.internal_encoding = EUC-JP

 mbstring.script_encoding

PHPスクリプトの文字コードを設定する。
この設定がない場合はmbstring.internal_encodingの設定が利用される。

mbstring.script_encoding = EUC-JP

 mbstring.encoding_translation

古いバージョンのブラウザでは常にShift-JISでデータを送信してくるブラウザもある。
mbstring.encoding_translationがonならば、Webサーバが受け取るデータ(入力データ)を自動判別し、内部エンコーディングに変換する。

mbstring.encoding_translation = On

参考

マルチバイト文字列関数
http://www.php.net/manual/ja/ref.mbstring.php

PHPを実行するディレクトリを制限する

phpの初期設定のままでは、どのディレクトリにphpを置いても実行してしまうので、
制限する方法。

Apacheモジュールとしてインストール
http://php.planetmirror.com/manual/ja/security.apache.php
open_basedir
http://php.planetmirror.com/manual/ja/features.safe-mode.php#ini.open-basedir

phpの設定

  1. php.ini
  2. httpd.conf
  3. .htaccess
  4. Windowsレジストリ
  5. PHPのini_set()
定数設定可能
PHP_INI_ALL1〜5
PHP_INI_PERDIR1〜3
PHP_INI_SYSTEM1〜2
php.ini ディレクティブ - Manual
http://jp.php.net/manual/ja/ini.php

サーバモジュール版ではWebサーバの起動時に読み込まれる。
CGI版ではスクリプトが実行される度に読み込まれる。

実行時設定
http://jp.php.net/manual/ja/configuration.php

 php.iniの記述

ディレクティブ名=設定値

 httpd.confまたは.htaccessの記述

php_value ディレクティブ名 値
php_flag ディレクティブ名 on/off

PHP資料

PHP:Hypertext Preprocessro
http://jp.php.net/
PHP マニュアル
http://jp.php.net/manual/ja/