CakeFest 2024: The Official CakePHP Conference

設定

FPM では、php.ini 形式の構文の設定ファイル php-fpm.conf と、プール設定ファイルを使います。

php-fpm.conf のグローバル設定項目

pid string

PID ファイルへのパス。デフォルト値: なし

error_log string

エラーログファイルへのパス。デフォルト値: #INSTALL_PREFIX#/log/php-fpm.log "syslog" と設定すると、ログをローカルファイルに書き出すのではなく、syslogd に送信します。

log_level string

エラーログのレベル。使用可能な値: alert, error, warning, notice, debug、デフォルト値: notice

log_limit int

1024文字以上のログメッセージを、 改行せずにロギングできる文字数の上限。 デフォルト値: 1024 PHP 7.3.0 以降で利用可能

log_buffering bool

追加のバッファリングを行わずに実験的なロギングを行う デフォルト値: yes PHP 7.3.0 以降で利用可能

syslog.facility string

メッセージを記録するプログラムの種別。 デフォルト値: daemon

syslog.ident string

すべてのメッセージの先頭に付加する内容。 複数の FPM インスタンスを同一サーバー上で実行する場合には、 デフォルト値を変更すれば区別できるようになります。 デフォルト値: php-fpm

emergency_restart_threshold int

emergency_restart_interval で設定された間隔で この数以上の子プロセスが SIGSEGV あるいは SIGBUS で終了した場合に FPM は再起動します。0 は 'オフ' を意味します。デフォルト値: 0 (オフ)

emergency_restart_interval mixed

emergency_restart_interval が緩やかな再起動をいつ実行するかを決めるときに使う間隔。 これは、アクセラレータの共有メモリが壊れてしまったときの回避策として有用です。 使用可能な単位: s(秒), m(分), h(時間) あるいは d(日)、 デフォルトの単位: 秒、デフォルト値: 0 (オフ)

process_control_timeout mixed

子プロセスが、マスタからのシグナルの反応を待つ最大時間。 使用可能な単位: s(秒), m(分), h(時間) あるいは d(日)、 デフォルトの単位: 秒、デフォルト値: 0

process.max int

FPM がフォークするプロセスの最大数。 多数のプール内で動的な PM を使う場合に、全体のプロセス数を制御するために用意されたものです。 気をつけて使うようにしましょう。 デフォルト値: 0

process.priority int

マスタープロセスに適用する nice(2) 値。 -19 (優先度最高) から 20 (優先度最低) までの値を指定します。 デフォルト値: 未設定

daemonize bool

FPM をバックグラウンドに送る。'no' にすると デバッグ用に FPM をフォアグラウンドに置き続けます。 デフォルト値: yes

rlimit_files int

マスタープロセス用の、オープン時のファイル記述子の rlimit。 デフォルト値: システムで定義されている値

rlimit_core int

マスタープロセス用の、最大コアサイズの rlimit。 デフォルト値: 0

events.mechanism string

FPM が利用するイベントメカニズム。 select、pool、epoll、kqueue (*BSD)、port (Solaris) が利用可能。 デフォルト値: 未設定 (自動検出)

systemd_interval int

FPM を systemd 統合込みでビルドした場合に、 ヘルスチェックのレポートを systemd に送る間隔 (秒単位)。 0 にすると、無効化されます。 デフォルト値: 10

プール一覧の項目

FPM を使うと、複数のプロセスプールをそれぞれ別の設定で実行することができます。 プール単位での設定を行う項目を以下に示します。

listen string

FastCGI リクエストを受け入れるアドレス。 'ip.add.re.ss:port', 'port', '/path/to/unix/socket' 形式の構文が使えます。 このオプションは、各プール単位で必須となります。

listen.backlog int

listen(2) のバックログを設定します。 -1 はBSD 系のシステムでは最大値を意味します。 デフォルト値: -1 (FreeBSD や OpenBSD) または 511 (Linux やその他のプラットフォーム)

listen.allowed_clients string

接続を許可されている FastCGI クライアントの IPv4 または IPv6 アドレス一覧。オリジナル版 PHP FastCGI (5.2.2+) における環境変数 FCGI_WEB_SERVER_ADDRS と同じです。 tcp でリスンするソケットに対してのみ意味をなします。 書くアドレスはカンマ区切りで指定します。この値を空にしておくと、任意の ip アドレスからの接続を許可します。 デフォルト値: 任意の ip アドレスを許可。

listen.owner string

unix ソケットを使う場合に、そのパーミッションを設定します。Linux では、 読み書きアクセス権限を設定しないとウェブサーバーからの接続を受け付けることができません。 多くの BSD 由来のシステムでは、パーミッションにかかわらず接続を受け付けることができます。 デフォルト値: ユーザーとグループは実行しているユーザーと同じ、モードは 0660

listen.group string

listen.owner を参照ください。

listen.mode string

listen.owner を参照ください。

listen.acl_users string

POSIX の Access Control List をサポートしている場合は、このオプションでそれを指定できます。 これを設定した場合は、listen.owner および listen.group は無視されます。 値には、ユーザー名をカンマ区切りのリスト形式で指定します。

listen.acl_groups string

listen.acl_users を参照。 グループ名を、カンマ区切りのリスト形式で指定します。

user string

FPM プロセスの unix ユーザー。このオプションは必須です。

group string

FPM プロセスの unix グループ。未設定の場合は、デフォルトのユーザーのグループを使います。

pm string

プロセスマネージャが子プロセスの数を制御する方法を選択します。 使用可能な値: static, ondemand, dynamic このオプションは必須です。

static - 子プロセスの数は固定 (pm.max_children) です。

ondemand - プロセスを必要に応じて立ち上げます。 dynamic とは対照的に、リクエストされると pm.start_servers で指定しただけサービスを開始します。

dynamic - 子プロセスの数は、 pm.max_childrenpm.start_serverspm.min_spare_serverspm.max_spare_servers の内容に基づいて動的に設定されます。

pm.max_children int

pmstatic の場合は作成される子プロセスの数、 pmdynamic の場合は作成される子プロセスの最大数。 このオプションは必須です。

このオプションは、同時に処理できるリクエストの最大数を設定します。 mpm_prefork での ApacheMaxClients ディレクティブや、 オリジナル版の PHP FastCGI における環境変数 PHP_FCGI_CHILDREN と同じです。

pm.start_servers int

起動時に作成される子プロセスの数。pmdynamic の場合にのみ使います。デフォルト値: min_spare_servers + (max_spare_servers - min_spare_servers) / 2

pm.min_spare_servers int

アイドル状態のサーバープロセス数の最小値。 pmdynamic の場合にのみ使います。 また、この場合には必須となります。

pm.max_spare_servers int

アイドル状態のサーバープロセス数の最大値。 pmdynamic の場合にのみ使います。 また、この場合には必須となります。

pm.max_spawn_rate int

一度に子プロセスを生成する数。 pm の値が dynamic である場合にのみ使えます。 デフォルト値: 32

pm.process_idle_timeout mixed

アイドルなプロセスがkillされた後の秒数。 pm の値が 使用可能な単位: s(秒)(デフォルト)、m(分)、h(時間)、または d(日)。 デフォルト値: 10 秒

pm.max_requests int

各子プロセスが、再起動するまでに実行するリクエスト数。 サードパーティのライブラリにおけるメモリリークの回避策として便利です。 再起動せずにずっとリクエストを処理させる場合は '0' を指定します。 PHP_FCGI_MAX_REQUESTS と同じです。デフォルト値: 0

pm.status_listen string

FastCGI の statusリクエスト を受け付けるアドレス。 これを設定すると、独自にリクエストを処理する新しい、 外からは見えないプールが作られます。 これは、長時間実行されるリクエストによって メインプールがビジーである場合に役立ちます。 なぜなら、そうした処理に長時間かかるリクエストが終わる前であっても、 FPM の情報ページ を見ることが出来るからです。 listen ディレクティブと設定できる内容は同じです。 デフォルト値: 未設定。

pm.status_path string

FPM の情報ページ を見るための URI。この値は、スラッシュ (/) で始めなければいけません。 この値を省略した場合は、どの URI も情報ページとは見なされません。 デフォルト値: なし

ping.path string

FPM のモニタリングページをコールするための ping URI。この値を省略した場合は、どの URI も ping ページとは見なされません。これを使うと、 FPM が生きていて応答するかどうかを外部から確かめることができます。 この値の最初はスラッシュ (/) で始めなければならないことに注意しましょう。

ping.response string

このディレクティブを使うと、ping リクエストに対するレスポンスをカスタマイズすることができます。 このレスポンスは text/plain 形式となり、レスポンスコード 200 で返されます。 デフォルト値: pong

process.priority int

ワーカープロセスに適用される、 nice(2) の値の優先度を指定します。 (指定された場合のみ有効) この値は、-19(優先度最高) から、20(優先度最低) まで変わります。 デフォルト値: 未設定

process.dumpable bool

プロセスのユーザーやグループが、 masterプロセスのユーザーと異なっていた場合でも、 プロセスのダンプ可能フラグ(PR_SET_DUMPABLE prctl)を設定します。 これにより、プロセスのコアダンプを生成できるようになり、 pool のユーザーがプロセスを ptrace 出来るようになります。 デフォルト値: no PHP 7.0.29, 7.1.17 および 7.2.5 から利用可能

prefix string

path を評価する際に、 path の前につける prefix を指定します。

request_terminate_timeout mixed

単一のリクエストを処理する際のタイムアウト。この時間を過ぎるとワーカープロセスが kill されます。 このオプションは、'max_execution_time' ini オプションが何らかの理由でスクリプトの実行を止められなかった場合に使われます。 値 '0' は 'Off' を意味します。 使用可能な単位: s(秒)(デフォルト), m(分), h(時間) あるいは d(日)、 デフォルト値: 0

request_terminate_timeout_track_finished bool

fastcgi_finish_request がコールされた後や、 アプリケーションが終了し、 内部的なシャットダウン関数がコールされているときには適用されない request_terminate_timeout を有効にするかどうか。 このディレクティブを有効にすると、 そのような適用されないケースでもタイムアウトを無条件に有効にします。 デフォルト値: no。PHP 7.3.0 以降で利用可能。

request_slowlog_timeout mixed

単一のリクエストを処理する際のタイムアウト。この時間を過ぎると PHP のバックトレースが 'slowlog' ファイルに出力されます。 値 '0' は 'Off' を意味します。 使用可能な単位: s(秒)(デフォルト), m(分), h(時間) あるいは d(日)、 デフォルト値: 0

request_slowlog_trace_depth int

slowlog に吐き出されるログのスタックトレースの深さ。 デフォルト値: 20。PHP 7.2.0 以降で利用可能。

slowlog string

遅いリクエストを記録するログファイル。デフォルト値: #INSTALL_PREFIX#/log/php-fpm.log.slow

rlimit_files int

このプール内の子プロセス用の、オープン時のファイル記述子の rlimit。デフォルト値: システムで定義されている値

rlimit_core int

このプール内の子プロセス用の、最大コアサイズの rlimit。 使用可能な値: 'unlimited' あるいは 0 以上の整数値、 デフォルト値: システムで定義されている値

chroot string

このディレクトリに chroot して開始位置とします。この値は絶対パスで指定しなければなりません。 この値を省略した場合は、chroot を使いません。

chdir string

このディレクトリに chdir して開始位置とします。この値は絶対パスで指定しなければなりません。 デフォルト値: カレントディレクトリ、あるいは chroot した場合は /

catch_workers_output bool

ワーカーの標準出力および標準エラー出力を本体のエラーログにリダイレクトします。 省略した場合は、FastCGI の仕様にしたがって標準出力および標準エラー出力を /dev/null にリダイレクトします。 デフォルト値: no

decorate_workers_output bool

catch_workers_output が有効になった場合に、 ワーカーの出力のデコレートを有効にします。 デフォルト値: yes PHP 7.3.0 以降で利用可能。

clear_env bool

FPM ワーカー内の環境をクリアする。 任意の環境変数が FPM ワーカープロセスに到達してしまうことを防ぐために、 ワーカー内の環境をいったんクリアしてから、このプールの設定で指定された環境変数を追加します。 デフォルト値: Yes

security.limit_extensions string

FPM がパース可能なメインスクリプトを拡張子で制限する。 サーバー側の設定ミスによる被害を抑えることができます。 FPM には拡張子 .php だけをパースさせるようにしておけば、 悪意のあるユーザーがその他の拡張子で php のコードを実行させようとする試みを防ぐことができます。 デフォルト値: .php .phar

apparmor_hat string

AppArmor が有効になっている場合に、 ハット変更を許可します。 デフォルト値: 未設定。

access.log string

アクセス・ログ・ファイル。 デフォルト値: 未設定

access.format string

アクセス・ログ形式。 デフォルト値: "%R - %u %t \"%m %r\" %s":

有効なオプション
プレースホルダ 説明
%C %CPU
%d duration µs
%e fastcgi env
%f script
%l content length
%m method
%M memory
%n pool name
%o header output
%p PID
%q query string
%Q the glue between %q and %r
%r request URI
%R remote IP address
%s status
%T time
%t time
%u remote user

追加の環境変数を渡して、特定のプールだけで PHP の設定を更新することができます。 そのためには、次のオプションをプール設定ファイルに追加しなければなりません。

例1 環境変数や PHP の設定をプールに渡す

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 32M
php_valuephp_flag で渡した PHP の設定は、その前に設定されていた内容を上書きします。 ただし disable_functionsdisable_classes は別で、 php.ini で定義された値を上書きするのではなく、 新たに指定した値を追記することになります。

php_admin_valuephp_admin_flag で設定した値を ini_set() で上書きすることはできません。

PHP の設定は、ウェブサーバーでも設定できます。

例2 PHP の設定を nginx.conf で行う例

set $php_value "pcre.backtrack_limit=424242";
set $php_value "$php_value \n pcre.recursion_limit=99999";
fastcgi_param  PHP_VALUE $php_value;

fastcgi_param  PHP_ADMIN_VALUE "open_basedir=/var/www/htdocs";
警告

これらの設定は fastcgi ヘッダーとして php-fpm に渡されるので、 一般公開されているアドレスに php-fpm をバインドしてはいけません。 そんなことをすると、誰でもそのサーバーの PHP の設定を変更できることになります。 listen.allowed_clients も参照ください。

注意: プールはセキュリティを考慮した仕組みではありません。 なぜなら、完全な分離を提供しないからです。 たとえば、FPM のプールは、単一の OPcache のインスタンスを使います。

add a note

User Contributed Notes 13 notes

up
5
jon dot phpnetdonotspam at langevin dot me
1 year ago
PHP-FPM configuration page apparently doesn't see the need to specify what options are available with each version of PHP.

It claims that pm.status_listen is a valid directive, but that directive only exists as of php 8.0.0, which is a bummer for those of us still using PHP 7.4.

Noting this for anyone else fighting with this.
up
22
ikrabbe
6 years ago
It seems there is no way to get informed about the access log format codes that are used or can be used. All I found is the source code.

It would really help, not to have open questions when deploying php-fpm. I constantly struggle with file paths for example, but that is another topic.

case '%': /* '%' */
case 'C': /* %CPU */
case 'd': /* duration µs */
case 'e': /* fastcgi env */
case 'f': /* script */
case 'l': /* content length */
case 'm': /* method */
case 'M': /* memory */
case 'n': /* pool name */
case 'o': /* header output */
case 'p': /* PID */
case 'P': /* PID */
case 'q': /* query_string */
case 'Q': /* '?' */
case 'r': /* request URI */
case 'R': /* remote IP address */
case 's': /* status */
case 'T':
case 't': /* time */
case 'u': /* remote user */
up
12
rob at librobert dot net
2 years ago
The 'index' directive that is used in php-fpm.conf is not documented here. However, this directive can also be used in the pool configurations. In the included file, the $pool variable is substituted correctly.

This means that, if you have multiple pools with similar configurations, you can create a file 'default-values.inc' like so:

-----
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = X
pm.min_spare_servers = X
pm.max_spare_servers = X

access.log = /var/log/php-fpm/$pool.access
access.format = "%R %u [%t] \"%m %r\" %s %d %l"
slowlog = /var/log/php-fpm/$pool.slow

php_flag[short_open_tag] = off
-----

And then include that file in each pool configuration like so:

-----
[vhost1.example.com]
user = www-vhost1
group = www-vhost1

listen = 127.0.0.1:9001

include = /usr/local/etc/php-fpm.d/default-values.inc
-----

This makes things a bit more transparent, and it could potentially save some time if you decide to change settings.

Make sure the name of the included file does not end in '.conf', because all files with that extension are loaded from php-fpm.conf.
up
17
gadnet at aqueos dot com
9 years ago
the doc is lacking a lot of things it seems.

The php fpm exemple config file indicate different thing, more option etc... I wonder why the main documentation is less verbose that the configuration file that user can have .. or not have ?
up
4
rob at librobert dot net
2 years ago
Correction for my previous note...

I wrote "The 'index' directive that is used in php-fpm.conf".

But obviously I meant "The 'include' directive"...
up
10
Frank DENIS
12 years ago
The default value for listen.backlog isn't exactly "unlimited".

It's 128 on some operating systems, and -1 (which doesn't mean "unlimited" as well, but is an alias to a hard limit) on other systems.

Check for a sysctl value like kern.somaxconn (OpenBSD) or net.core.somaxconn (Linux).

Crank it up if you need more PHP workers than the default value. Then adjust listen.backlog in your php-fpm configuration file to the same value.

-Frank.
up
1
antonfedonyuk at gmail dot com
2 years ago
NOTE: "access.format" containing "%o" generate error in PHP 7.4 (don't tested in other versions)
up
6
frederic at juliana-multimedia dot com
5 years ago
With Apache, mod_proxy_fcgi and php-fpm, if you want to have a generic pool and several vhost with different php configuration, you can use the ProxyFCGISetEnvIf directive and the PHP_ADMIN_VALUE environment variable. It does not work with PHP_ADMIN_FLAG even for boolean directives.

PHP directives must be separated by spaces and a \n.

ProxyFCGISetEnvIf "true" PHP_ADMIN_VALUE "open_basedir=/var/www/toto/:/tmp/ \n session.save_path=/var/www/toto/session \n display_errors=On \n error_reporting=-1"
up
1
david dot cancalon at proxeem dot fr
3 years ago
Be very carrefull when using ProxyFCGISetEnvIf within a Apache virtual host configuration using a shared PHP-FPM pool. Values defined like this are shared across all the Apache virtual hosts within a pool worker, may resulting in strange behaviours depending on the requests chronology.

See full explanation here:
https://serverfault.com/questions/817020/stop-reusing-php-value-for-different-sites-with-php-fpm/817905#817905
up
0
antonfedonyuk at gmail dot com
2 years ago
; The access log format.
; The following syntax is allowed
; %%: the '%' character
; %C: %CPU used by the request
; it can accept the following format:
; - %{user}C for user CPU only
; - %{system}C for system CPU only
; - %{total}C for user + system CPU (default)
; %d: time taken to serve the request
; it can accept the following format:
; - %{seconds}d (default)
; - %{milliseconds}d
; - %{milli}d
; - %{microseconds}d
; - %{micro}d
; %e: an environment variable (same as $_ENV or $_SERVER)
; it must be associated with embraces to specify the name of the env
; variable. Some examples:
; - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
; - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
; %f: script filename
; %l: content-length of the request (for POST request only)
; %m: request method
; %M: peak of memory allocated by PHP
; it can accept the following format:
; - %{bytes}M (default)
; - %{kilobytes}M
; - %{kilo}M
; - %{megabytes}M
; - %{mega}M
; %n: pool name
; %o: output header
; it must be associated with embraces to specify the name of the header:
; - %{Content-Type}o
; - %{X-Powered-By}o
; - %{Transfert-Encoding}o
; - ....
; %p: PID of the child that serviced the request
; %P: PID of the parent of the child that serviced the request
; %q: the query string
; %Q: the '?' character if query string exists
; %r: the request URI (without the query string, see %q and %Q)
; %R: remote IP address
; %s: status (response code)
; %t: server time the request was received
; it can accept a strftime(3) format:
; %d/%b/%Y:%H:%M:%S %z (default)
; The strftime(3) format must be encapsulated in a %{<strftime_format>}t tag
; e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
; %T: time the log has been written (the request has finished)
; it can accept a strftime(3) format:
; %d/%b/%Y:%H:%M:%S %z (default)
; The strftime(3) format must be encapsulated in a %{<strftime_format>}t tag
; e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
; %u: remote user
;
; Default: "%R - %u %t \"%m %r\" %s"
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{milli}d %{kilo}M %C%%"

https://github.com/php/php-src/blob/master/sapi/fpm/www.conf.in#L257-L318
up
-4
Yousef Ismaeil Cliprz
10 years ago
Check if fastCGI enabled

<?php
// You can use isset or is_null for $_SERVER['FCGI_SERVER_VERSION']
function isFastCGI () {
return !
is_null($_SERVER['FCGI_SERVER_VERSION']);
}

?>
up
-8
sroussey at gmail dot com
11 years ago
Starting in 5.3.9 the security.limit_extensions configuration item has been added and it defaults to .php. If you need to execute other extensions, you have to change this setting.
up
-18
mb
8 years ago
If you need to disable security.limit_extensions variable, simply set the variable to FALSE like so:

security.limit_extensions = FALSE
To Top