HOME
  Security
   Software
    Hardware
  
FPGA
  CPU
   Android
    Raspberry Pi
  
nLite
  Xcode
   etc.
    ALL
  
LINK
BACK
 

2018/02/17

Windowsパソコンに nginx + PHP 5.2.17の高速 Webサーバ環境を構築する手順 Windowsパソコンに nginx + PHP 5.2.17の高速 Webサーバ環境を構築する手順

(nginxを使って Apacheよりも高速な Webサーバ環境を Windowsで構築する PHP 5.2系)

Tags: [Windows開発]




● Windowsパソコンに nginx + PHP 5.2.17の高速 Webサーバ環境を構築する手順

 nginxを使って Apacheよりも高速な Webサーバ環境を Windowsで構築する PHP 5.2系

 PHPスクリプトを動かした場合、Apache 2.0よりも実測値で 6倍は速かった。(あまりにも速かったので大笑いした)
 同一実行環境で Apache 2.4と比べた場合でも 1.25倍速かった。

 2018/07追記:nginx 1.14.0 + PHP 5.6.36を Windows 10 + Pentium N4200で動かしたら更に 2倍速くて驚いた。

 Apache系と nginxの PHPスクリプトの実行速度比較
 Webサーバ + PHPスクリプトの構成で動くアプリを実行した例です。
実行環境処理時間nginx速度比(遅さ)実行環境
Apache 2.0.63 + PHP 4.4.9590秒6倍Windows XP + Atom D525
Apache 2.2.25 + PHP 4.4.9110秒1.15倍Windows 10 + Atom X5-Z8350
Apache 2.4.29 + PHP 5.2.17120秒1.25倍Windows 10 + Atom X5-Z8350
nginx 1.12.2 + PHP 5.2.1795秒-Windows 10 + Atom X5-Z8350
nginx 1.14.0 + PHP 5.6.3643秒-Windows 10 + Pentium N4200
※ PHP 5.2.17は PHP 4互換モードを有効(zend.ze1_compatibility_mode = On)
※ PHP 5.6.36は 通常の PHP 5モードを使用(PHPプログラム側を PHP5対応に修正した)
※ Windows 10 + Atom X5-Z8350実行環境は Morefine M1sを使用
※ Windows 10 + Pentium N4200実行環境は VOYO V1を使用

● 2022年 11月現在の速度比較
実行環境処理時間nginx速度比(遅さ)実行環境PHPの環境サイズ
Apache 2.0.63 + PHP 4.4.91572秒10~11倍Windows XP + Atom D52519.2MB
nginx 1.22.1 + PHP 5.2.17153秒-Windows 10 + Core i5-8259U26.0MB
nginx 1.22.1 + PHP 5.6.39142秒-Windows 10 + Core i5-8259U51.3MB
nginx 1.22.1 + PHP 5.6.40142秒-Windows 10 + Core i5-8259U51.3MB
nginx 1.22.1 + PHP 7.2.34172秒-Windows 10 + Core i5-8259U59.1MB
nginx 1.22.1 + PHP 7.4.33142秒-Windows 10 + Core i5-8259U61.7MB
※ PHP 7.2.34がなぜか遅いです


2017/06/08
MoreFine M1s Intel Atom X5 Z8350搭載 64GB 4GB 中華スティック PCのレビュー
MoreFine M1s Intel Atom X5 Z8350搭載 64GB 4GB 中華スティック PCのレビュー

  Windows 10搭載のスティック PC MoreFine Z8350 M1s 64GB 4GBを BangGoodで買ってみた


●用意する物

 ・nginx本体 nginx 1.12.2
Stable version nginx/Windows-1.12.2
Stable version nginx/Windows-1.12.2
nginx: download
※ 2018/12追記 最新の nginx-1.14.2.zipでも下記の手順で同様に設定可能。

 ・PHP 5.2.17本体 Non Thread Safe(NTS)
php-5.2.17-nts-Win32.zip 10455037 2011-Jan-06 17:44
php-5.2.17-nts-Win32.zip
php museum - Index of /php5/

 ※ nginxで使用する場合は Non Thread Safe版(NTS)を使用します。
 ※ Apacheで使用する場合は Thread Safe版(TS)を使用します。



● Webサーバのディレクトリ構成

C:\00_Web\
C:\00_Web\bin\
C:\00_Web\bin\ngnix\
C:\00_Web\bin\PHP52NTS\

Webサーバの Document Root
C:\00_Web\bin\ngnix\html\


● nginx本体をインストールする。

nginx-1.12.2.zip
 を解凍する。

 好みで下記のディレクトリにインストールする。
C:\00_Web\bin\ngnix\

● PHP本体をインストールする。

php-5.2.17-nts-Win32.zip
 を解凍する。
 ※ nginxで使用する場合は Non Thread Safe版(NTS)を使用します。

 好みで下記のディレクトリにインストール(解凍)する。
C:\00_Web\bin\PHP52NTS\

● nginxの設定ファイル nginx.confファイルを編集する

C:\00_Web\bin\nginx\conf\nginx.conf
 をテキストエディタで開いて下記の行を追加する。
 63行目あたりから。
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 を下記に書き換える。
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

 worker_connectionsは 1024個も要らないので 16個に減らす。
events {
#    worker_connections  1024;
    worker_connections  16;
}

● PHPの設定ファイル php.iniファイルを編集する

C:\00_Web\bin\PHP52NTS\php.ini-dist
 または、
C:\00_Web\bin\PHP52NTS\php.ini-recommended
 のファイルを php.iniの名前にリネームする(コピーでも良い)

C:\00_Web\bin\PHP52NTS\php.ini
 をテキストエディタで開いて好みで編集する。
[PHP]
; Enable compatibility mode with Zend Engine 1 (PHP 4.x)
; zend.ze1_compatibility_mode = Off
zend.ze1_compatibility_mode = On

; short_open_tag = Off
short_open_tag = On

; extension_dir = "./"
extension_dir = "./ext/"

extension=php_mbstring.dll

[mbstring]
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_output = UTF-8

 ※ PHP 5.2を使う理由は PHP 4系の互換オプションが使えるから。
コア php.ini ディレクティブに関する説明
zend.ze1_compatibility_mode = On
 Zend Engine 1 (PHP 4) との互換モードを有効にします。 この設定は、オブジェクトのコピー、キャスト (プロパティを 保持しないオブジェクトが FALSE あるいは 0 のいずれになるか)、 そして 比較 に影響を与えます。このモードの場合、オブジェクトを渡す際の デフォルトの方法は、参照渡しではなく値渡しとなります。
 警告 この機能は 非推奨 で、 PHP 5.3.0 以降では 削除 されました。
PHP 4 から PHP 5.0.x への移行 - 新しいオブジェクトモデル

● nginx Webサーバの実行ファイルを手動で実行する。

nginx.exe
 を必要な時に実行する。
 PHPをソケット待ち受けで動かすので
php-cgi.exe -b 127.0.0.1:9000
 で PHPを実行する。

 毎回入力するのは面倒なので起動用のバッチファイルと停止用のバッチファイルを作成します。

 nginx + PHPの起動コマンド一式(バッチファイルを作成する)
cd /d C:\00_Web\bin\nginx\
if exist .\logs\nginx.pid goto SKIP
start nginx.exe
cd /d C:\00_Web\bin\PHP52NTS\
start /MIN php-cgi.exe -b 127.0.0.1:9000
:SKIP
TIMEOUT /T 3 /NOBREAK

 nginx + PHPの停止コマンド一式(バッチファイルを作成する)
cd /d C:\00_Web\bin\nginx\
rem tasklist /FI "IMAGENAME eq nginx.exe"
if not exist .\logs\nginx.pid taskkill /f /im nginx.exe
if exist .\logs\nginx.pid nginx.exe -s quit
rem tasklist /FI "IMAGENAME eq php-cgi.exe"
taskkill /im php-cgi.exe
TIMEOUT /T 3 /NOBREAK

● nginx.exeを実行すると could not open error log file: CreateFile() "logs/error.log" failedで動かない

 nginx.exeを実行する時に nginx.exeのディレクトリ以外で実行すると下記のエラーが出て動きません。
C:\> C:\00_Web\bin\nginx\nginx.exe
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2018/03/01 23:07:00 [emerg] 1192#4908: CreateFile() "C:\00_Web\bin/conf/nginx.conf" failed (3: The system cannot find the path specified)

●おなじみの phpinfo.phpを動かして、PHPの動作を確認する。

http://localhost/
Welcome to nginx!

C:\00_Web\bin\nginx\html\phpinfo.php
phpinfo.php
<?php
  phpinfo();
?>

http://localhost/phpinfo.php
PHP Version 5.2.17
Build Date Jan 6 2011 17:34:09
Server API CGI/FastCGI
Loaded Configuration File C:\00_Web\bin\PHP52NTS\php.ini

mbstring
Multibyte Support enabled
Multibyte string engine libmbfl
Multibyte (japanese) regex support enabled
Multibyte regex (oniguruma) version 4.4.4
Multibyte regex (oniguruma) backtrack check On

mbstring extensionの有効化をチェック
C:\00_Web\bin\nginx\html\phpmbstring.php
phpmbstring.php
<?php
  print_r(mb_get_info());
?>

http://localhost/phpmbstring.php
Array (
 [internal_encoding] => UTF-8
 [http_input] =>
 [http_output] => UTF-8
 [func_overload] => 0
 [func_overload_list] => no overload
 [mail_charset] => ISO-2022-JP
 [mail_header_encoding] => BASE64
 [mail_body_encoding] => 7bit
 [illegal_chars] => 0
 [encoding_translation] => Off
 [language] => Japanese
 [detect_order] => Array (
  [0] => ASCII
  [1] => JIS
  [2] => UTF-8
  [3] => EUC-JP
  [4] => SJIS )
 [substitute_character] => 63
 [strict_detection] => Off
)


● Webサーバの Document Rootのディレクトリを他の場所にする場合。

Webサーバの Document Root
C:\00_Web\html\

 location /の root指定を絶対パスで指定する。
 (root ../../html;の相対パス指定でも大丈夫だった)
        location / {
#            root   html;       # OK C:\00_Web\bin\ngnix\html\
#            root   ../../html; # OK C:\00_Web\html\
            root   C:\00_Web\html;

            index  index.html index.htm;
        }

 .phpの root指定を絶対パスで指定する。
 (root ../../html;の相対パス指定は PHPが No input file specified.エラーになる)
 (phpファイルが無い場合にも No input file specified.エラーになる)
        location ~ \.php$ {
#            root           html;
            root   C:\00_Web\html;

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

 下記の設定は phpファイルが無い場合には 404エラーにしてくれる。
        location ~ [^/]\.php(/|$) {
#            root   html;       # OK C:\00_Web\bin\ngnix\html\
#            root   ../../html; # NG No input file specified.
            root   C:\00_Web\html;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php;

            include fastcgi_params;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        }


● nginxで Apacheの Options Indexesと同様の指定をする方法

 index.htmlや phpのファイルが無いディクレトリを指定した時に一覧表示する設定方法。
 Apacheの Options Indexesに相当する指定方法。(ちなみに拒否する場合は Options -Indexes)

 autoindex on;を serverブロックに記述します。
server {
    autoindex   on;


●コピペ実行用お気楽バッチファイル

echo zipファイルを解凍する
mkdir C:\00_Web\bin\
..\UnZip.exe nginx-1.12.2.zip C:\00_Web\bin\
ren C:\00_Web\bin\nginx-1.12.2 nginx
mkdir C:\00_Web\bin\PHP52NTS\
..\UnZip.exe php-5.2.17-nts-Win32.zip C:\00_Web\bin\PHP52NTS\

copy ..\mbsed.exe C:\00_Web\bin\

echo Webサーバの Document Root C:\00_Web\html\
mkdir C:\00_Web\html\

echo nginx.confを sedコマンドの置換処理で書き換える
echo Webサーバの Document Root = C:\00_Web\html\
cd /d C:\00_Web\bin\nginx\conf\
copy nginx.conf nginx_org.conf

..\..\mbsed.exe -e "s/worker_connections  1024;/worker_connections  16;/" nginx.conf --in-place=.bak
del nginx.conf.bak

rem "1,/html;/"指定で最初の 1個目だけ(location / {の所)を置換対象にしている。
..\..\mbsed.exe -e "1,/html;/s/html;/C:\\00_Web\\html;/" nginx.conf --in-place=.bak
del nginx.conf.bak

rem 最初の 1個目の "    }"を置換対象として、.phpの定義を追加している。(既存の .phpのブロックをコメントを取りながら書き換える方法よりも簡単だから)
..\..\mbsed.exe -e "1,/^    }$/s/^    }$/\n        location ~ \\.php$ {\n            root           C:\\00_Web\\html;\n            fastcgi_pass   127.0.0.1:9000;\n            fastcgi_index  index.php;\n            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;\n            include        fastcgi_params;\n        }\n    }/" nginx.conf --in-place=.bak
del nginx.conf.bak


echo php.iniを sedコマンドの置換処理で書き換える
cd /d C:\00_Web\bin\PHP52NTS\
copy php.ini-recommended php.ini

..\mbsed.exe -e "s/^zend.ze1_compatibility_mode = Off/zend.ze1_compatibility_mode = On/" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^short_open_tag = Off/short_open_tag = On/" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^extension_dir = \".\/\"/extension_dir = \".\/ext\/\"/" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^;extension=php_mbstring.dll/extension=php_mbstring.dll/" php.ini --in-place=.bak
del php.ini.bak

echo PHPで httpsで始まるページが取得できない場合
echo PHP Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP ?
echo allow_url_fopen = Offの場合は {2}echo allow_url_fopen = Onも必要
..\mbsed.exe -e "s/^;extension=php_openssl.dll/extension=php_openssl.dll/g" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^;mbstring.language = Japanese/mbstring.language = Japanese/" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^;mbstring.internal_encoding = EUC-JP/mbstring.internal_encoding = UTF-8/" php.ini --in-place=.bak
del php.ini.bak

..\mbsed.exe -e "s/^;mbstring.http_output = SJIS/mbstring.http_output = UTF-8/" php.ini --in-place=.bak
del php.ini.bak


● mbsed.exe Windowsのコマンドラインで sedコマンド

GNU sed with Oniguruma (Onigsed)とは?

鬼車を使っていないバージョン(2009/10/5)
md5 3fd753202cb5dbc565eeba29abd049d *sed-mbcs-win32-20091004.zip
sha1 9f4f44f67cb1fa6ad9c95fa3ac232be625b36f6 sed-mbcs-win32-20091004.zip


● UnZip.exe

 Visual Studio 2013の練習で作成した ZIPファイル解凍専用アプリ。
 UnZip.exe
using System.IO.Compression;

namespace UnZip
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2) return;

            string zipPath = args[0];
            string extractPath = args[1];

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}
 ZipFile クラス > ZipFile メソッド > ExtractToDirectory メソッド
ZipFile.ExtractToDirectory メソッド (String, String)
 指定した zip アーカイブのすべてのファイルをファイル システムのディレクトリに抽出します。
 名前空間: System.IO.Compression

 ※ 解凍時に既にファイルが存在していると例外でアプリが落ちます。


● PHPの設定ファイル php.iniファイルの読み込み先の設定

PHP - 設定ファイル
 設定ファイル (php.ini) は PHP の起動時に読み込まれます。 PHP のサーバーモジュール版では、Web サーバーの起動時に 一度だけ読み込まれます。CGI 版と CLI 版では、スクリプトが呼び出される度に読み込まれます。

 PHPRC 環境変数。
 PHP.exe単体で動かす場合で php.iniファイルの場所を指定する場合。
set PHPRC=C:\00_Web\bin\PHP52NTS
 ※ 上記の環境変数の設定をしなくても php.exeと同じディレクトリの php.iniを自動で読み込みました。(nginx Webサーバで PHPを動かす場合にも同様)


● Windowsパソコンに nginx + PHP 5.6.36の高速 Webサーバ環境を構築する手順

PHP For Windows
 から、PHP 5.6 (5.6.36)の VC11 x86 Non Thread Safe (2018-Apr-26 07:43:29)をダウンロードし、上記と同様に解凍してセットアップします。

 nginx側の設定の変更は有りません。
 起動バッチの php-cgi.exeの実行バイナリへの PATHを変えるだけでその他の設定は大丈夫です。
 php.iniファイルは php.ini-development、または、php.ini-productionを元に作成します。(php.ini-recommendedファイルは無くなった)
copy php.ini-development php.ini

php-5.6.36-nts-Win32-VC11-x86.zip
 x86版を使用します。

 ※ nginxサーバとの組み合わせの時の PHPは Non Thread Safe版を使用します。


● Windowsパソコンに nginx + PHP 7.2.7の高速 Webサーバ環境を構築する手順

PHP For Windows
 から、PHP 7.2 (7.2.7)の VC15 x86 Non Thread Safe (2018-Jun-20 02:53:39)をダウンロードし、上記と同様に解凍してセットアップします。

 nginx側の設定の変更は有りません。
 起動バッチの php-cgi.exeの実行バイナリへの PATHを変えるだけでその他の設定は大丈夫です。
 php.iniファイルは php.ini-development、または、php.ini-productionを元に作成します。(php.ini-recommendedファイルは無くなった)
copy php.ini-development php.ini

php-7.2.7-nts-Win32-VC15-x86.zip
 x86版を使用します。

 ※ nginxサーバとの組み合わせの時の PHPは Non Thread Safe版を使用します。



Tags: [Windows開発]

●関連するコンテンツ(この記事を読んだ人は、次の記事も読んでいます)

Apache Webサーバを過剰なアクセスや DoS攻撃から守る modモジュールの紹介
Apache Webサーバを過剰なアクセスや DoS攻撃から守る modモジュールの紹介

  対 DoS攻撃用の Apache2モジュール mod_evasive、mod_dosdetector、mod_limitipconn、fail2ban等の紹介

Windowsパソコンに Apache 2.2 + PHP 4.4.9の Webサーバ環境を構築する手順(最後の砦)
Windowsパソコンに Apache 2.2 + PHP 4.4.9の Webサーバ環境を構築する手順(最後の砦)

  過去の遺産等のしがらみでどうしても PHP 4.4系を Windowsで動かしたいと言う場合に

Windowsパソコンに Apache 2.4 + PHP 5.2.17の Webサーバ環境を構築する手順(最後の砦)
Windowsパソコンに Apache 2.4 + PHP 5.2.17の Webサーバ環境を構築する手順(最後の砦)

  過去のプログラム等でどうしても PHP 5.2系を Windowsで動かしたいと言う場合に

C# .NETで ZIPファイル解凍ツール UnZipをソースリスト 1行で自作する方法、Windows .NET専用
C# .NETで ZIPファイル解凍ツール UnZipをソースリスト 1行で自作する方法、Windows .NET専用

  Visual Studio 2013の C# .NET 4.5で ZipFile.ExtractToDirectoryを使い、UnZip解凍ツールを作成




[HOME] | [BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c) 2018 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator

http://www.neko.ne.jp/~freewing/software/windows_nginx_php_5217/