LoginSignup
15
16

More than 5 years have passed since last update.

【PHP】php.ini ディレクティブまとめ

Last updated at Posted at 2017-10-26

■ はじめに

● この記事で挙げられている引用ついて

特に記述がなければ PHP: PHP マニュアル - Manual (以下PHPマニュアル) から引用されたものです。

● この記事で挙げられているデフォルト値について

この記事におけるデフォルト値とはPHPマニュアルに記述された値であり、下記の説明に沿います。

これらのデフォルト値は、php.ini が読み込まれなかったときに使われるものです。

ですので未編集のphp.iniに記述されている値という意味ではないので注意してください。

● この記事で挙げられているphp.iniについて

この記事におけるphp.iniの例は、php7.0.24にバンドルされているもので、推奨値ではありません

● この記事で扱わないディレクティブについて

この記事ではModule Settingsディレクティブを扱いません。(Date, mbstring, Session など)
すなわち PHP: コア php.ini ディレクティブに関する説明 - Manual と、
PHP: 実行時設定 - Manual で挙げられているディレクティブしか扱いません。

またphp7.0.24にバンドルされているphp.iniからディレクティブを引っ張ってきているため、古かったり新しかったりするものは扱いません。

■ php.iniのQiitaにおける頻出ディレクティブ

20171023時点で、Qiitaで検索して10件以上見つかったディレクティブを、一部飛ばしつつ件数の降順で挙げていきます。

error_log (string)

スクリプトエラーが記録されるファイル名です。

・ デフォルト:null
・ 設定例:"/var/log/php_error.log" など環境に応じて

php.ini
;error_log = php_errors.log
;error_log = syslog

internal_encoding (string)

PHP 5.6.0 以降で利用可能です。 この設定は、mbstring や iconv などのマルチバイトモジュールが使うものです。

・ デフォルト:""
・ 設定例: (設定しない)

php.ini
;internal_encoding =

空文字を指定した場合default_charsetが参照されるので、重複した内容を記述するのであれば、設定しなくても良いでしょう。input_encoding, output_encoding についても同様です。
なお mbstring.internal_encoding, mbstring.http_input, mbstring.http_output は PHP 5.6.0 から非推奨になりました。

display_errors (string)

エラーをHTML出力の一部として画面に出力するかどうかを定義します。

・ デフォルト:"1"
・ 設定例:開発環境では"1"(On), 本番環境では"0"(Off)

php.ini
display_errors = Off

error_reporting (integer)

エラー出力レベルを設定します。

・ デフォルト:null
・ 設定例:-1 (全てのエラーを出力する)

php.ini
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

memory_limit (integer, または省略形)

スクリプトが確保できる最大メモリをバイト数で指定します。

・ デフォルト:"128M" (PHP >= 5.2)
・ 設定例:環境に応じて (-1で制限なし)

php.ini
memory_limit = 128M

最初から不必要に大きな値にせず、Fatal error: Out of memoryちゃんに怒られたりしながら値を精査した方が良いでしょう。ですがphp.iniを変更するより先に、当該部分のコードを直して解消できないか検討した方がきっと幸せになれます。

default_charset (string)

PHP から送信する Content-Type ヘッダのデフォルト値としてこれを使います。
ただし、header() で上書きされている場合は別です。

・ デフォルト:"UTF-8"
・ 設定例:"UTF-8" (変更しない)

php.ini
default_charset = "UTF-8"

不用意にUTF-8以外の文字コードを扱うことを許容してしまうと、悲しみを背負ってしまうかもしれません。

upload_max_filesize (integer, または省略形)

アップロードされるファイルの最大サイズ。

・ デフォルト:"2M"
・ 設定例:環境に応じて

php.ini
upload_max_filesize = 2M

PHPでファイルをアップロードするときは、たとえばredhat系ならば chcon -R -t httpd_sys_rw_content_t でアップロード先のフォルダを指定しなければならない場合があります。

post_max_size (integer, または省略形)

POSTデータに許可される最大サイズを設定します。
大きなファイルをアップロードするには、この値を upload_max_filesize より大きく設定する必要があります。
一般的に memory_limit は、post_max_sizeよりも大きくする必要があります。

・ デフォルト:"8M"
・ 設定例:環境に応じて

php.ini
post_max_size = 8M

expose_php (boolean)

PHP がサーバーにインストールされていることを全世界に晒し、PHP のバージョンも HTTP ヘッダに含めます (X-Powered-By: PHP/5.3.7 など)。

・ デフォルト:"1"(On)
・ 設定例:"0"(Off)

php.ini
expose_php = On

Onにして悪いことはあっても良いことはないため、必ず"0"(Off)にしておく

include_path (string)

require、include、fopen()、file()、readfile() および file_get_contents() 関数がファイルを探すディレクトリのリストを指定します。

・ デフォルト:".;/path/to/php/pear"
・ 設定例:".:/usr/local/lib/php" など環境に応じて

php.ini
;include_path = ".:/php/includes"
;include_path = ".;c:\php\includes"

log_errors (string)

エラーメッセージを、サーバーのエラーログまたはerror_logに記録するかどうかを指定します。
このオプションはサーバーに依存します。

・ デフォルト:"0"(Off)
・ 設定例:"1"(On)

php.ini
log_errors = On

max_execution_time (integer)

スクリプトがパーサにより強制終了されるまでに許容される最大の時間を秒単位で指定します。

・ デフォルト:30
・ 設定例:環境に応じて

php.ini
max_execution_time = 30

最初から不必要に大きな値にせず、Fatal error: Maximum execution time of n seconds exceededちゃんに怒られたりしながら値を精査した方が良いでしょう。ですがphp.iniを変更するより先に、当該部分のコードを直して解消できないか検討した方がきっと幸せになれます。

short_open_tag (boolean)

PHP タグの短縮型 (<? ?>) を使用可能にするかどうかを設定します。

・ デフォルト:"1"(On)
・ 設定例:"0"(Off)

php.ini
short_open_tag = Off

output_handler (string)

スクリプトの全ての出力を関数にリダイレクトすることができます。
このディレクティブには、組み込み関数のみが使用可能です。

・ デフォルト:null
・ 設定例:ob_gzhandler, mb_output_handler など必要に応じて

php.ini
;output_handler =

allow_url_fopen (boolean)

スクリプトの全ての出力を関数にリダイレクトすることができます。

・ デフォルト:"1"(On)
・ 設定例:必要に応じて、不要ならば必ず"0"(Off)にしておく

php.ini
allow_url_fopen = On

PHPでhttpからファイルを扱うには、たとえばredhat系ならば setsebool httpd_can_network_connect 1 を実行しなければならない場合があります。
またここでは取り上げて扱うことをしないので併記しますが、不要ならば allow_url_include も必ず"0"(Off)になっていることを確認した方が良いでしょう。

output_buffering (boolean/integer)

このディレクティブを 'On' と設定することにより、全てのファイルに関して出力バッファリングを有効にすることができます。
特定の大きさにバッファの大きさを制限したい場合、このディレクティブの値として 'On' の代わりに最大バイト数(例:output_buffering=4096) を使用することができます。

・ デフォルト:"0"(Off)
・ 設定例:必要に応じて

php.ini
output_buffering = 4096
  • PHPのheader関数の前で出力しちゃダメ、本当?
    • https://qiita.com/chinka/items/d63a4d0f2c4e5e794284

上掲の記事がわかりやすいです。

file_uploads (boolean)

HTTP ファイルアップロード を有効とするかどうか。

・ デフォルト:"0"(Off)
・ 設定例:必要に応じて、不要ならば必ず"0"(Off)にしておく

php.ini
file_uploads = On

auto_prepend_file (string)

メインファイルの前に自動的にパースされるファイルの名前を指定します。

・ デフォルト:""
・ 設定例:必要に応じて

php.ini
auto_prepend_file =

対になるディレクティブは auto_append_file
太字の部分は、PHPマニュアルの日本語版『付加される』部分を英語版『parsed』に読み替えています。

display_startup_errors (boolean)

display_errorsをonにした場合でも、PHPの起動シーケンスにおいて発生したエラーは表示されません。デバッグ時を除き、 display_startup_errorsをoffにしておくことが強く推奨されます。

・ デフォルト:"1"
・ 設定例:開発環境では"1"(On), 本番環境では"0"(Off)

php.ini
display_startup_errors = Off

つまりPHPの起動シーケンスにおいて発生したエラーを表示するかどうかです。

open_basedir (string)

PHP からアクセスできるファイルを、指定したディレクトリツリーに限定します。
ファイル自身も含みます。

・ デフォルト:null
・ 設定例:"/var/www/http" など環境に応じて、可能であるならば必ず指定しておく

php.ini
;open_basedir =

Directory Traversalを防ぐことができる場合があります。

■ Qiitaにおける各ディレクティブ別の件数 (20171023時点)

項番 分類 ディレクティブ名 件数
86 Fopen wrappers from 231
3 Language Options engine 129
51 Error handling and logging error_log 82
65 Data Handling internal_encoding 79
35 Error handling and logging display_errors 77
34 Error handling and logging error_reporting 72
33 Resource Limits memory_limit 70
64 Data Handling default_charset 54
82 File Uploads upload_max_filesize 40
60 Data Handling post_max_size 39
29 Miscellaneous expose_php 36
69 Paths and Directories include_path 36
37 Error handling and logging log_errors 26
30 Resource Limits max_execution_time 25
4 Language Options short_open_tag 23
87 Fopen wrappers user_agent 20
8 Language Options output_handler 17
84 Fopen wrappers allow_url_fopen 15
7 Language Options output_buffering 14
80 File Uploads file_uploads 14
61 Data Handling auto_prepend_file 12
83 File Uploads max_file_uploads 12
36 Error handling and logging display_startup_errors 11
15 Language Options open_basedir 10
66 Data Handling input_encoding 10
67 Data Handling output_encoding 10
76 Paths and Directories cgi.fix_pathinfo 10
6 Language Options precision 9
27 Language Options zend.multibyte 9
46 Error handling and logging html_errors 9
9 Language Options zlib.output_compression 8
24 Language Options realpath_cache_size 8
31 Resource Limits max_input_time 8
38 Error handling and logging log_errors_max_len 8
55 Data Handling variables_order 8
62 Data Handling auto_append_file 8
70 Paths and Directories doc_root 8
85 Fopen wrappers allow_url_include 8
14 Language Options serialize_precision 7
68 Data Handling always_populate_raw_post_data 7
81 File Uploads upload_tmp_dir 7
28 Language Options zend.script_encoding 6
43 Error handling and logging track_errors 6
47 Error handling and logging docref_root 6
48 Error handling and logging docref_ext 6
71 Paths and Directories user_dir 6
72 Paths and Directories enable_dl 6
88 Fopen wrappers default_socket_timeout 6
89 Fopen wrappers auto_detect_line_endings 6
1 php.ini Options user_ini.filename 5
10 Language Options zlib.output_compression_level 5
11 Language Options zlib.output_handler 5
16 Language Options disable_functions 5
18 Language Options highlight.string 5
19 Language Options highlight.comment 5
20 Language Options highlight.keyword 5
21 Language Options highlight.default 5
22 Language Options highlight.html 5
23 Language Options ignore_user_abort 5
25 Language Options realpath_cache_ttl 5
26 Language Options zend.enable_gc 5
32 Resource Limits max_input_nesting_level 5
39 Error handling and logging ignore_repeated_errors 5
41 Error handling and logging report_memleaks 5
42 Error handling and logging report_zend_debug 5
49 Error handling and logging error_prepend_string 5
50 Error handling and logging error_append_string 5
57 Data Handling register_argc_argv 5
58 Data Handling auto_globals_jit 5
59 Data Handling enable_post_data_reading 5
63 Data Handling default_mimetype 5
2 php.ini Options user_ini.cache_ttl 4
5 Language Options asp_tags 4
12 Language Options implicit_flush 4
13 Language Options unserialize_callback_func 4
17 Language Options disable_classes 4
40 Error handling and logging ignore_repeated_source 4
44 Error handling and logging xmlrpc_errors 4
45 Error handling and logging xmlrpc_error_number 4
53 Data Handling arg_separator.output 4
54 Data Handling arg_separator.input 4
56 Data Handling request_order 4
78 Paths and Directories fastcgi.logging 4
79 Paths and Directories cgi.rfc2616_headers 4
52 Error handling and logging windows.show_crt_warning 3
73 Paths and Directories cgi.force_redirect 3
74 Paths and Directories cgi.nph 3
75 Paths and Directories cgi.redirect_status_env 3
77 Paths and Directories fastcgi.impersonate 3

おわり!

15
16
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
15
16