LoginSignup
2
3

More than 5 years have passed since last update.

PHP-FPMでドメインごとに違うユーザでプロセスを動かす

Posted at

前提条件など

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_pass127.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です。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3