前提条件など
NGINX+PHP-FPMでバーチャルホストを構成していて、それぞれのホストごとに別ユーザとしてプロセスを動かします。
ユーザの作成をしておきます。example.comを動かすユーザ、exampleを追加しておきます。
$ useradd example
PHP-FPMでユーザごとにつながるようにしておきます。
ポイントはlisten
の箇所です。待受を行うTCP/IPのポート番号(若しくはユニックスソケット)を他のものと違う番号にしておきます。
今回はuseraddした時に出来たユーザIDが1005だったので9005としました。
/etc/php7-fpm.d/example.conf
; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[example]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = example
group = example
; Note: This value is mandatory.
listen = 127.0.0.1:9005
NGINXの設定です。
/etc/nginx/sites-available/example.com.conf
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9005;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffers 256 128k;
fastcgi_buffer_size 128k;
fastcgi_intercept_errors on;
fastcgi_read_timeout 120s;
こちらもfastcgi_pass
を127.0.0.1:9005;
にしておきます。
テスト
適当に負荷を掛けるコードを書いて、動かします。
test.php
<?php
function hoge() {
for($i=0; $i<1000000000; $i++) {
if((rand()%100) === 0 ) {
echo 'hello'.PHP_EOL;
}
}
}
hoge();
?>
top
などでプロセスの所有者が確認できればOKです。