編碼的世界 / 優質文選 / 文明

CentOS 7中查看PHP運行時的Log文件日志信息


2022年7月13日
-   

對於服務器上面運行的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.再去添加讀寫權限:
cd /var/log

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-rr 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-rr 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 /econf: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

熱門文章