PHPをダウンロード
https://windows.php.net/download こちらのサイトからZipファイルをダウンロード

nginxは基本「FastCGI」なので上の「VS17 x64 Non Thread Safe」をダウンロードします
ダウンロードしたファイルを展開
ダウンロードしたファイルを右クリックし「すべて展開」をクリック
展開する場所を聞かれるので任意の場所を指定
c:/php
c:/php8.4
c:/srv/nginx/php8.4
phpfフォルダは自分で作成
PHPの設定
設定ファイルのリネーム
PHPフォルダにある「php.ini-development」というファイルを一旦別のフォルダにコピーし「php.ini」にリネーム
php.iniをPHPフォルダに移動
設定ファイルを編集
「php.ini」をメモ帳などのテキストエディタで開く
776行目付近「extension_dir = “ext”」の部分。コメントアウトを削除
「php/ext」までの絶対パスに変更
;extension_dir = "exe"
↓
extension_dir = "c:/php/ext"
extension_dir = "c:/php8.4/ext"
extension_dir = "c:/srv/nginx/php8.4"
上記は例です
Nginxと連携する
nginx.confを開く
45行目付近「index.php」を追加
66行目以降のPHPに関するロケーション部分のコメントアウトを削除
一部修正
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
↓
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root c:/srv/html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root c:/srv/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
44行目、66行目付近の2か所にある「root~~」はWebファイルがある場所までのパス 2か所とも同じパスにする
45行目 index.phpを追加(index.phpから先に参照する)
69行目付近 「/scripts」から「$doment_root」に変更
動作確認
nginxが起動している場合は、再起動
phpを起動
Apacheと違ってnginxでphpを使う場合は個別でphpを起動する必要があります
コマンドプロンプトにてphpを起動する
cd c:\srv\nginx\php
start php-cgi.exe -b 127.0.0.1:9000
cd~~ phpフォルダがある階層まで移動
start~~ phpを起動
127.0.0.1:9000 は fastcgi_pass で指定したアドレス(デフォルトのままでOK)
ブラウザでhttp://localhost/info.php などにアクセス

上のように表示されれば成功です(バージョンはダウンロードしたファイルによって変わります)
NginxとPHPをコマンド操作で起動 停止させる
Nginxの起動 停止 再起動
コマンドプロンプトにて
cd c:\nginx
cd c:\srv\nginx
nginxまで移動
start eginx で起動
nginx -s stop で停止
nginx -reload で再起動
PHPの起動 停止
コマンドプロンプトにて
cd c:\php
cd c:\php8.4
cd c:srv\nginx\php8.4
PHPフォルダまで移動
start php-cgi.exe -b 127.0.0.1:9000 で起動
taskkill /f /im php-cgi.exe で停止
正確にはPHPの停止コマンドはないので強制終了です
Nginx+PHP 同時に起動 停止
NginxはApacheみたいにPHPも同時に起動、停止することができませんが、batファイル作って疑似的に同時に起動、停止することができます。
起動用batファイル
メモ帳に以下を記入
@echo off
cd C:\srv\nginx
start nginx
cd C:\srv\nginx\php
start php-cgi -b 127.0.0.1:9000
cd C:\server\nginx はnginxまでのパス
cd C:\server\nginx\php はphpフォルダまでのパス
start_nginx_php.bat など分かりやすい名前で保存
停止用batファイル
メモ帳に以下を記入
@echo off
cd C:\srv\nginx
nginx -s stop
taskkill /f /im php-cgi.exe
cd C:\server\nginx nginxまでのパス
phpは停止コマンドがないので強制終了させます
stop_nginx_php.bat など分かりやすい名前で保存
Nginxの停止コマンドが効いてない
nginx.confを編集したりして起動、停止しても設定が反映されていないときがあります。
そういう時は大体nginxが完全に停止していない、若しくはできて可能性があります。
Windowsを再起動する手もありますが、コマンドで強制的に終了させることもできます
コマンドプロンプトにて
taskkill /f /im nginx.exe
を実行するだけです。PHPの強制終了と同じコマンドです
PHPと同時に強制終了するbatファイルも作れます
taskkill /f /im nginx.exe
taskkill /f /im php-cgi.exe
ng_php_kill.bat などで保存
コメント