> CentOS > CentOS运维 >

CentOS 7中查看PHP运行时的Log文件日志信息

【背景】

折腾:

【已解决】PHP代码尝试使用vsprintf期间出错无任何输出

期间,需要搞懂,对于服务器上面运行的PHP代码,期间的log输出到哪里。

想要去查看对应的log,找到代码无法运行的原因。

【折腾过程】

1.搜:

check php log

centos check php log

参考:

Where does PHP store the error log? – Stack Overflow

apache2 – Where are the Apache and PHP log files? – Ask Ubuntu

去看看:

自己此处的/var/log/下面没有apache2或apache

 

2.通过:

phpinfo()

去找error_log

结果得到:

error_log

no value

no value

 

3.所以去设置php.ini的log日志:

【已解决】CentOS 7中PHP配置文件php.ini的放在哪个位置

4.然后去编辑php.ini,添加对应的error_log

vi /etc/php.ini

把:

 
1
2
3
4
5
6
7
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog

改为:

 
1
2
3
4
5
6
7
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
error_log = /var/log/php_errors.log
; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog

5.同时把:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
; error_reporting
;   Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
;   Development Value: E_ALL
;   Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
......
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

改为:

 
1
2
#error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL | E_STRICT

6.再去添加读写权限:

 
1
2
3
4
5
6
7
8
9
root@chantyou:log# pwd
/var/log
root@chantyou:log# touch /var/log/php_errors.log
root@chantyou:log# ls /var/log/php_errors.log -l
-rw-r--r-- 1 root root 0 Jul 28 16:03 /var/log/php_errors.log
root@chantyou:log# chmod +rw /var/log/php_errors.log
root@chantyou:log# ls /var/log/php_errors.log -l    
-rw-r--r-- 1 root root 0 Jul 28 16:03 /var/log/php_errors.log
root@chantyou:log#

此处由于都是root用户,所以和没添加一样。。。

7.然后重启apache2:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@chantyou:log# service httpd restart
Redirecting to /bin/systemctl restart  httpd.service
root@chantyou:log# service httpd status
Redirecting to /bin/systemctl status  httpd.service
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: active (running) since Tue 2015-07-28 16:05:13 CST; 7s ago
  Process: 6858 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 2224 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 6864 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─6864 /usr/sbin/httpd -DFOREGROUND
           ├─6866 /usr/sbin/httpd -DFOREGROUND
           ├─6867 /usr/sbin/httpd -DFOREGROUND
           ├─6868 /usr/sbin/httpd -DFOREGROUND
           ├─6869 /usr/sbin/httpd -DFOREGROUND
           └─6870 /usr/sbin/httpd -DFOREGROUND
 
Jul 28 16:05:13 chantyou.com httpd[6864]: AH00548: NameVirtualHost has no effect and will be removed in the next release /e...conf:1
Jul 28 16:05:13 chantyou.com httpd[6864]: AH00558: httpd: Could not reliably determine the server's fully qualified domain ...essage
Jul 28 16:05:13 chantyou.com systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
root@chantyou:log#

8.看看有无log输出了:

 
1
2
root@chantyou:log# tail /var/log/php_errors.log
root@chantyou:log#

结果空的。

那就继续去测试其他php,如果出错了,希望此处可以看到错误的log日志。

9.然后,也确定了phpinfo()中是可以看到有错误日志的配置了:

error_log

/var/log/php_errors.log

/var/log/php_errors.log

 

【总结】

暂时反正是设置了PHP的错误日志,但是实际上后续的一些错误,比如代码的语法错误,结果却没有任何输出。

感觉可能还是某些地方禁止了错误输出。

估计是那个:

 

display_errors

暂不确定。

暂不去深究了。


 

(责任编辑:IT)