75
PHP勉強会SIDE-B (1) WindowsでのPHPとのつきあい方 (前編) id:msakamoto-sf 坂本 昌彦

PHP With Windows binary

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: PHP With Windows binary

PHP勉強会SIDE-B (第1回)

WindowsでのPHPとのつきあい方(前編)

id:msakamoto-sf

坂本 昌彦

Page 2: PHP With Windows binary

アジェンダ

● 1.インストーラを用いずにzipファイルからインストールする方法

● 2.Windows用PHPバイナリの構成● 3.CUIで動かす時の最小構成

Page 3: PHP With Windows binary

1.インストーラを用いずにzipファイルからインストールする方法

WindowsでのPHPのインストール

Page 4: PHP With Windows binary

ところで

Windowsって使ってますよね?

Page 5: PHP With Windows binary

MacOSXとか*nix使いは

寝ちゃうかも・・・(;^^)ヘ..

Page 6: PHP With Windows binary

インストール

どうやってインストールしているか?

Page 7: PHP With Windows binary

インストール方法

XAMP?

Page 8: PHP With Windows binary

インストール方法

インストーラパッケージ?

Page 9: PHP With Windows binary

インストール方法

本で/WEBで見たとおり?

Page 10: PHP With Windows binary

インストールして・・・

インストールした後

Page 11: PHP With Windows binary

なんとなく

放置してませんか?

Page 12: PHP With Windows binary

なんとなく

放置すると・・・

Page 13: PHP With Windows binary

半年おきとか。

いつのまにやらバージョンが上がってたり。

Page 14: PHP With Windows binary

泣く泣く

入れ直し。(つд・)エーン

Page 15: PHP With Windows binary

厭な所

・XAMP・インストーラ

↓レジストリとか弄られるのが

厭。

Page 16: PHP With Windows binary

プロっぽく

zipパッケージで全てのファイル・ディレクトリ構成を掌

握し、管理する。

Page 17: PHP With Windows binary

こだわり。

レジストリを弄る必要も無い!!

(゚∀゚)

Page 18: PHP With Windows binary

PHP本体の

バージョンアップも、ファイルの入れ替えだけでほぼOK

Page 19: PHP With Windows binary

工夫すれば・・・

PHP4とPHP5の共存も!

Page 20: PHP With Windows binary

試したことは無いけど・・・

(多分マイナーバージョン間の共存もOK)

Page 21: PHP With Windows binary

前提

WinXP SP2Apache 2.0.5x系列

PHP 5.2.6

Page 22: PHP With Windows binary

主要な二種類のダウンロード

Page 23: PHP With Windows binary

今回は上を使います。

Page 24: PHP With Windows binary

ダウンロードしたら

解凍して、好きな場所にうつします。

Page 25: PHP With Windows binary

例(in_vitroというのは気にしなくてOK)

Page 26: PHP With Windows binary

php.iniは

php.ini-recommendedを

php.iniにコピーします。

Page 27: PHP With Windows binary

Apache

httpd.confの編集

Page 28: PHP With Windows binary

以下を追記。(パターン)

LoadModule php5_module "C:/in_vitro/apps/php5/php5apache2.dll"# ↑は実際は一行で。# (Load Module php5_module "C:/.../php5apache2.dll")

PHPIniDir "C:/in_vitro/apps/php5"

AddType application/x-httpd-php .php .htmlAddType application/x-httpd-php-source .phps

Page 29: PHP With Windows binary

"PHPIniDir"

PHPIniDir: php.iniのあるフォルダを指定。

Page 30: PHP With Windows binary

PHP4と違って

C:\WINDOWS とかにphp.iniを置かなくても

平気!

Page 31: PHP With Windows binary

Apacheを再起動・・・の前に。

Apache.exe -tで確認。

Page 32: PHP With Windows binary

(何かオプションがアレですが気にしないで!)

↑OK。Apache起動できる。

> cd "C:\Program Files\Apache Group\Apache2\bin"> apache -t -f conf\httpd_php5.confSyntax OK

Page 33: PHP With Windows binary

ディレクトリ指定とかが間違ってた時

・・・何か出ます。

> cd "C:\Program Files\Apache Group\Apache2\bin"> apache -t -f conf\httpd_php5.confSyntax error on line 15 of C:/Program Files/Apache Group/Apache2/conf/httpd_php5.conf:Cannot load C:/in_vitro/apps/php5/php5apache2.dll into server: \x8ew\x92\xe8\x82\xb3\x82\xea\x82\xbd\x83\x82\x83W\x83\x85\x81[\x83\x8b\x82\xaa\x8c\xa9\x82\xc2\x82\xa9\x82\xe8\x82\xdc\x82\xb9\x82\xf1\x81B

Page 34: PHP With Windows binary

Apacheを起動したら

適当な公開ディレクトリで"<?php phpinfo(); ?>"を叩いて動けばOK.

Page 35: PHP With Windows binary

php.iniの設定内容は

大垣先生のWikiを見るとよい。http://wiki.ohgaki.net/

「PHP/tips/推奨php.ini設定」など。

Page 36: PHP With Windows binary

PHPのバージョンが変わった時は・・・

1.PHPをDLする。2.httpd.confの書き換え

3.php.iniの移行(4.extensionのコピー)

Page 37: PHP With Windows binary

Apacheを入れ直した時は・・・

1.httpd.confの追記

Page 38: PHP With Windows binary

2.Windows用PHPバイナリの構成(直下のファイル群)

● php5ts.dll - PHP言語の実行コアライブラリ● php.ini-{dist|recommended} - php.iniの雛形● php({-cgi|-win}).exe - CLIやCGIでのPHP実行ファイル

● php5apache*.dll, php5{is|ns}api.dll - 各種Webサーバー用モジュール

● go-pear.bat - PEARライブラリインストールバッチ● 他のDLL - MySQLやSSLなどのネイティブライブラリ

Page 39: PHP With Windows binary

ディレクトリなど

● dev/ - 静的ライブラリ(php5ts.lib)置き場● ext/ - PECLモジュール(dll)置き場● extras/ - MIB, SSL, PDF関係のファイル置き場● PEAR/ - PEARインストールパッケージ(*.phar)●

Page 40: PHP With Windows binary

PHPの動作とファイルの関係(1)Apacheモジュールの場合

Apache.exe↓

php5apache*.dll↓

php5ts.dll

Page 41: PHP With Windows binary

PHPの動作とファイルの関係(2)CGIの場合

Apache.exe/IIS↓

php-cgi.exe↓

php5ts.dll

Page 42: PHP With Windows binary

PHPの動作とファイルの関係(3)CLIからの場合

(コマンドプロンプト)↓

php.exe↓

php5ts.dll

Page 43: PHP With Windows binary

php-win.exeが謎だけど・・・

結局php5ts.dll

に行き着きます。

Page 44: PHP With Windows binary

php.ini

重要

Page 45: PHP With Windows binary

デフォルトでは何処にphp.iniがあると(php5ts.dllは)思っているか。

(PHPIniDirをコメントアウトしてApacheを起動)

Page 46: PHP With Windows binary

php.iniの場所の教え方(1)Apacheモジュール

PHPIniDir ディレクティブ

Page 47: PHP With Windows binary

PHPIniDir ディレクティブの効果

PHPIniDir "C:/in_vitro/apps/php5"

Page 48: PHP With Windows binary

php.iniの場所の教え方(2)exe系から叩く時

"-c "オプション

php(-cgi).exe -c ...\php.ini

Page 49: PHP With Windows binary

php.iniは重要

・・・ですが、セキュリティ関係の設定は大垣先生の資料を参

考にするとして。

Page 50: PHP With Windows binary

プログラミングする上で

普通に重要な設定が二つあります。

Page 51: PHP With Windows binary

二つ。

"include_path"

"extension_dir"

Page 52: PHP With Windows binary

"include_path"

{require|include}(_once)する時に指定されたファイルを探

すディレクトリの一覧。

Page 53: PHP With Windows binary

"include_path"の設定をミスると・・・

PEARのライブラリファイルが見つからなくなったりします。

アプリのrequire/includeで失敗したりします。

Page 54: PHP With Windows binary

"extension_dir"

"extension"で読み込むDLLの入っているディレクトリ。

MySQL接続とか、mbstringとか・・・。

Page 55: PHP With Windows binary

"extension_dir"の設定をミスると・・・

DBに接続できなくなったり、いろいろと出来なくなります。

Page 56: PHP With Windows binary

これほど重要な設定なのに・・・

php.ini-recommendedのデフォルト設定は役立たず。

(´・ω・`)

Page 57: PHP With Windows binary

"include_path"のデフォルト

;include_path = ".;c:\php\ includes"

コメントアウトされてるし。

Page 58: PHP With Windows binary

"extension_dir"のデフォルト

extension_dir = "./"

イヤ、それ、違うし。

Page 59: PHP With Windows binary

なので

この二項目はすぐに正しいディレクトリに書き換えて下さい。

include_pathについてはPEARを入れてから調整しても良いで

すが・・・

Page 60: PHP With Windows binary

extension_dirについては直ぐに。

でないと、php_zipやmbstringなどが読み込めない

=PEARのインストールを始めとして

色々支障が出ます。

Page 61: PHP With Windows binary

3.CUIで動かす時の最小構成

突然ですが、Windows用のPHPって"可愛い"と思うのです。

Page 62: PHP With Windows binary

理由

・好きな場所における。・ファイルの組み合わせが

楽。・*nixと違ってディレクトリ構成の制約が少ない。

Page 63: PHP With Windows binary

極端な話

php5ts.dllphp.exephp.ini

だけでCLIで実験できる!

Page 64: PHP With Windows binary

CLIの構成例

● php5ts.dll● php.ini● php(-win).exe : php-winとの使い分けは後述。● ext/php_***.dll : お好みのDLL● boot.bat : プログラム実行用BATファイル●

● 他、PHPスクリプトファイル。

Page 65: PHP With Windows binary

秘密1

%~dp0

Page 66: PHP With Windows binary

%~dp0とは?

%0がバッチファイル名。(boot.bat)

~dでドライブ名展開。~pでファイル名を除くパス

名に展開。

Page 67: PHP With Windows binary

秘密2

php(-win).exeの"-d"オプション。

Page 68: PHP With Windows binary

"-d"オプション

php.iniの設定項目を上書き。

Page 69: PHP With Windows binary

秘密3

php-win.exe+

"start"コマンド in boot.php

Page 70: PHP With Windows binary

php-win.exe

コマンドプロンプトウインドウを表示せずにPHPスクリプ

トを実行可能。

Page 71: PHP With Windows binary

"start"コマンド

kickしたプロセスの終了を待たずに、処理続行

=boot.bat自体は終了。

Page 72: PHP With Windows binary

組み合わせれば・・・

タスクスケジューラからkickされる定時実行バッチもPHPスクリプトで書ける!

Page 73: PHP With Windows binary

配布ファイルも柔軟に構成可能だから

PHP実行環境と一緒に配布することも可能。

(=配布先でわざわざPHPを"インストール"する必要

無し。)

Page 74: PHP With Windows binary

まとめ

● インストーラやXAMPを使わず、zipから展開すれば、レジストリも弄らないし、PHP本体のバージョンアップに対する追従も楽。

● php5ts.dll, php.ini重要。● php.iniではextension_dirとinclude_pathに注

意。● "%~dp0", php-win.exe, "start"コマンドでPHPスクリプトを柔軟に配布・実行可能。

Page 75: PHP With Windows binary

Windows用のPHP5バイナリって

実は結構使い勝手が良い言語環境だと思う。