php.ini

最後更新: 2017-06-06

介紹

 


最後更新: 2024-07-02

介紹

 


; ';' 是注解來

 

; 不用以全寫的 <?php 作為 script 的開始

short_open_tag = On

;

zlib.output_compression = On

 

include_path='.:/usr/share/psa-horde:/usr/share/psa-horde/lib:/usr/share/psa-pear'

 

; 為安全, 停用部份 function

disable_functions =show_source,system,shell_exec,passthru,exec,phpinfo,proc_open ;

register_globals = Off

default_mimetype = "text/html"

 

; 不設定他時常會有 warning

date.timezone = Asia/Hong_Kong

 

; 有關 upload file 的設定

file_uploads = On

upload_max_filesize = 50M

; post size 一定要 > upload size

post_max_size = 60M

; uploaded via a single request

max_file_uploads = 10

 


max_execution_time

 

; 單位: 秒, default: 30

; hardcoded to 0 for the CLI SAPI

max_execution_time = 30

Remark

If this is reached, the script returns a fatal error.

---

To get the currently used time, use "getrusage()"

---

On non Windows systems, the maximum execution time is

not affected by system calls, stream operations and database queries etc.

ie.

sleep, file_get_contents, shell_exec, mysql_query

---

When called, set_time_limit() restarts the timeout counter from zero. zero => no time limit is imposed

i.e.

After 25 sec the script execution a call such as set_time_limit(20),

the script will run for a total of 45 sec)

 


memory_limit

 

php.ini

memory_limit = 512M

code

ini_set('memory_limit','512M');

checking

echo ini_get('memory_limit');

echo memory_get_usage();

Returns the amount of memory, in bytes, that's currently being allocated to your PHP script.

 


# each script may spend parsing request data(GET, POST).

max_input_time = 60

 


Debug 相關

 

; 每句 print(), echo() 都有 flush(), 用於 Debug

implicit_flush = Off

error_reporting  =  E_ALL & ~E_NOTICE

display_errors = Off

display_startup_errors = Off

log_errors = Off

ignore_repeated_errors = Off

log_errors_max_len = 1024

error_log = /var/log/php.log

 


url_fopen

 

# treatment of URLs (like http:// or ftp://) as files

allow_url_fopen = On

# "include/require" to open URLs

allow_url_include = Off

# seconds

default_socket_timeout = 30

 

 


Module Settings

 

[mail function]

sendmail_path =

 

[MySQL]

mysql.allow_persistent = On

mysql.max_links = -1

mysql.connect_timeout = 60

[Session]

session.save_handler = files

session.save_path = /var/lib/php5

session.use_cookies = 1

; seconds, Default: 24min

session.gc_maxlifetime= 1440

session.name = PHPSESSID

session.auto_start = 0

; until browser is restarted

session.cookie_lifetime = 0

 


PHP5 TimeZone Error

 

php5.3.x 以上才會有此 Error

php.ini 找到

[Date]
;date.timezone = "Asia/Taipei"

或在 php code 內只要輸入

date_default_timezone_set("Asia/Taipei");

 


session.gc_maxlifetime

 

php_value session.gc_maxlifetime "3600"

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime

session.cookie_lifetime = 0

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
; http://php.net/session.gc-maxlifetime
; 24 min

session.gc_maxlifetime = 1440

 


user_ini

 

These files are processed only by the CGI/FastCGI SAPI

; sets the name of the file PHP looks for in each directory
; To disable this feature set this option to empty value
user_ini.filename = .user.ini

; controls how often user INI files are re-read (Unit: Second)
user_ini.cache_ttl = 300

 


php-cgi 的 php.ini

 

建立一個 php 測試

<?php phpinfo(); ?>

OR

php-cgi -i | grep ini

Loaded Configuration File: /etc/php5/cgi/php.ini

Scan this dir for additional .ini files: /etc/php5/cgi/conf.d

Additional .ini files parsed

/etc/php5/cgi/conf.d/*.ini

mod_php 修改 ini 的位置

cp /etc/php5/cgi/php.ini /home/vhosts/xxx/ini

<VirtualHost 1.2.3.4:80>
[...]
# mod_php
PHPINIDir /home/vhosts/xxx/ini
[...]
</VirtualHost>

 

php-cgi 的 php.ini

php-cgi -v

PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch (cgi-fcgi) (built: Oct 29 2014 12:12:21)

# PHP information and configuration

php-cgi -i | grep ini

<tr><td class="e">Configuration File (php.ini) Path </td><td class="v">/etc/php5/cgi </td></tr>
<tr><td class="e">Loaded Configuration File </td><td class="v">/etc/php5/cgi/php.ini </td></tr>
<tr><td class="e">Scan this dir for additional .ini files </td><td class="v">/etc/php5/cgi/conf.d </td></tr>
.................

php-cgi

-f             # parse and execute files
-c path|file   # Look for php.ini file in the directory path

# fcgid
<IfModule mod_fcgid.c>
    AddHandler fcgid-script .php
    FcgidWrapper "/usr/bin/php-cgi -c /home/vhosts/xxx/ini" .php
    RewriteEngine On
    RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
    <Directory /usr/share/phpMyAdmin/>
            Options +ExecCGI
            Deny from All
            Allow from 127.0.0.1
            Allow from 115.160.172.18
    </Directory>
</IfModule>

ps aux | grep php-cgi

/usr/bin/php-cgi -c /home/vhosts/bnw.hk/ini

 


Apache 爆 error log

 

log

[Fri Nov 04 09:40:03 2016] [error] [client ::1] PHP Notice:  Undefined variable: ...

修改 php.ini 設定檔

error_reporting integer

# E_ALL: All errors and warnings (doesn't include E_STRICT), not included in E_ALL until PHP 5.4.0

# E_STRICT: PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT

OR

error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

 


Execution Time

 

Troubleshoot

Fatal error: Maximum execution time of 120 seconds exceeded in /home/vhosts/...

方法1

<?php
set_time_limit(120);
...
?>

方法2

<?php
ini_set('max_execution_time', 2*60);
...
?>

方法3

.htaccess

php_value max_execution_time 120

 


error_reporting

 

 * 如果 program 寫不好, E_WARNING 很易食盡系統空間

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_WARNING & ~E_NOTICE

 


expose_php

 

在沒有 phpinfo() 的情況下也知 php 存在及是什麼 Version

Default: On

Exposes to the world that PHP is installed on the server, which includes the PHP version within the HTTP header (Response Headers)

# Server 載入了 mod_php 就有, 即使是透問 txt 檔
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.0.33
# 頁面有執行過 php 就有它
X-Powered-By: PHP/7.3.4

 


session cookies

 

session.use_only_cookies (Defaults to 1)

# Enabling this setting prevents attacks involved passing session ids in URLs

session.cookie_secure

# cookies should only be sent over secure connections (https)

session.cookie_httponly

# Marks the cookie as accessible only through the HTTP protocol.

session.cookie_lifetime

# The value 0 means "until the browser is closed." Defaults to 0.

session.cookie_path

session.cookie_domain

 

 

Creative Commons license icon Creative Commons license icon