Troubleshooting of PHP errors: Difference between revisions

Jump to navigation Jump to search
no edit summary
Tags: Mobile edit Mobile web edit
No edit summary
Line 6: Line 6:
# 檢查網站伺服器 error log e.g. 如果使用 [https://www.nginx.com/ NGINX] Web Server 可輸入指令 {{kbd | key=<nowiki>sudo tail /var/log/nginx/error.log</nowiki>}}
# 檢查網站伺服器 error log e.g. 如果使用 [https://www.nginx.com/ NGINX] Web Server 可輸入指令 {{kbd | key=<nowiki>sudo tail /var/log/nginx/error.log</nowiki>}}


 
== Configuration and Environment Issues ==
== PHP 技術問題處理 ==
 
=== 檔案操作有關 ===
可否讀取或寫入:例如 imagejpeg($canvas, $filename, 100); 沒有顯示錯誤訊息,則需要
* 檢查檔案或目錄是否存在 [http://tw2.php.net/file_exists file_exists]
 
* 檢查檔案是否可以讀取 [http://php.net/manual/en/function.is-readable.php is_readable]
 
* 如果是上傳檔案,需要檢查暫存目錄是否可以寫入 [http://stackoverflow.com/questions/1520956/php-way-to-find-the-web-servers-temp-path upload - PHP way to find the web server's temp path? - Stack Overflow]
 
* 檢查該檔案所在目錄是否已經建立,若未建立則需要建立 [http://php.net/manual/en/function.mkdir.php mkdir],並設定可以寫入。如果產生位置是在多層子目錄下 (巢狀的多層目錄),則需要啟用 recursive 選項<ref>[http://stackoverflow.com/questions/2303372/create-a-folder-if-it-doesnt-already-exist php - Create a folder if it doesn't already exist - Stack Overflow]  code snippet: <nowiki>mkdir('path/to/directory', 0755, true);</nowiki> </ref>
 
* 設定該檔案/目錄是否可以寫入: 如果要刪除或寫入檔案,需要檢查是否具備寫入權限
*# SELinux (Security-Enhanced Linux) policy: {{kbd | key = httpd_sys_rw_content_t}}<ref>[https://blog.lysender.com/2015/07/centos-7-selinux-php-apache-cannot-writeaccess-file-no-matter-what/ CentOS 7 + SELinux + PHP + Apache – cannot write/access file no matter what | Lysender's Daily Log Book]</ref>
*# (optional) 該檔案/目錄的 (1) 擁有者是網站使用者 (e.g. 網站伺服器設定檔預設值 CentOS 的 Apache 伺服器是 apache、Ubuntu 的 Apache 伺服器則是 www-data<ref>[https://ubuntuforums.org/showthread.php?t=1293508 [SOLVED] How to find out Apache Username?]</ref>) 、或者是 (2) 網站使用者的群組 (e.g. CentOS 的 Apache 伺服器是 apache、Nginx 伺服器是 nginx) 可以寫入
*# {{Win}} [http://www.webdeveloper.com/forum/showthread.php?165933-File-Permissions-in-Windows-(WAMP) File Permissions in Windows (WAMP)] <ref>[https://technet.microsoft.com/en-us/library/Cc754344.aspx Set, View, Change, or Remove Permissions on Files and Folders]</ref>
 
* 驗證是否具備寫入權限:
*# 輸入 {{Linux}} 指令 (1) 檢查檔案權限 {{kbd | key = ls -Z /path/to/file}}、(2) 檢查目錄權限 {{kbd | key = ls -Zd /path/to/directory/}}
*# PHP [https://www.php.net/manual/en/function.get-current-user.php get_current_user] function
*# PHP [http://php.net/manual/en/function.is-writable.php is_writable function]
 
=== 不知道網站伺服器載入哪一個 php.ini 設定檔 ===
=== 不知道網站伺服器載入哪一個 php.ini 設定檔 ===
使用 console command<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref>
使用 console command<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref>
Line 60: Line 38:
** 法1: 啟用 short_open_tag (1) php.ini 檔案 需要開啟 short_open_tag = Off -> On (2) 重新啟動網頁伺服器服務,讓 php.ini 修改生效 (3) 驗證 short_open_tag = On
** 法1: 啟用 short_open_tag (1) php.ini 檔案 需要開啟 short_open_tag = Off -> On (2) 重新啟動網頁伺服器服務,讓 php.ini 修改生效 (3) 驗證 short_open_tag = On
** 法2: 不啟用 short_open_tag,將多個 PHP 檔案的 {{kbd | key=<nowiki><? </nowiki>}} 改成 {{kbd | key=<nowiki><?php </nowiki>}}。 (1) 使用支援 [[Regular expression]] 的文字編輯軟體、(2) 搜尋 {{kbd | key=<nowiki><\?\s</nowiki>}} 取代為 {{kbd | key=<nowiki><\?php </nowiki>}} 。請注意取代部分結尾有一個空白。詳見 [[Batch remove PHP short tags]]
** 法2: 不啟用 short_open_tag,將多個 PHP 檔案的 {{kbd | key=<nowiki><? </nowiki>}} 改成 {{kbd | key=<nowiki><?php </nowiki>}}。 (1) 使用支援 [[Regular expression]] 的文字編輯軟體、(2) 搜尋 {{kbd | key=<nowiki><\?\s</nowiki>}} 取代為 {{kbd | key=<nowiki><\?php </nowiki>}} 。請注意取代部分結尾有一個空白。詳見 [[Batch remove PHP short tags]]
=== curl error 7 while downloading https://packagist.tw/ ... ===
Error condition
<pre>
composer.phar update                             
Loading composer repositories with package information                                                      https://packagist.tw could not be fully loaded (curl error 7 while downloading https://packagist.tw/packages.json: Failed to connect to packagist.tw port 443: Connection refused), package information was loaded from the local cache and may be out of date
In CurlDownloader.php line 372:
                                                                                                                                                               
  curl error 7 while downloading https://packagist.tw/p/zircote/xxx.json: Failed to 
  connect to packagist.tw port 443: Connection refused         
</pre>
Solution
* The mirror I was using encountered a network error.
* Switch to a different mirror by entering the following command: {{kdb | key=<nowiki>composer config repos.packagist composer https://packagist.org</nowiki>}}
== Unclassified PHP 技術問題處理 ==
=== 檔案操作有關 ===
可否讀取或寫入:例如 imagejpeg($canvas, $filename, 100); 沒有顯示錯誤訊息,則需要
* 檢查檔案或目錄是否存在 [http://tw2.php.net/file_exists file_exists]
* 檢查檔案是否可以讀取 [http://php.net/manual/en/function.is-readable.php is_readable]
* 如果是上傳檔案,需要檢查暫存目錄是否可以寫入 [http://stackoverflow.com/questions/1520956/php-way-to-find-the-web-servers-temp-path upload - PHP way to find the web server's temp path? - Stack Overflow]
* 檢查該檔案所在目錄是否已經建立,若未建立則需要建立 [http://php.net/manual/en/function.mkdir.php mkdir],並設定可以寫入。如果產生位置是在多層子目錄下 (巢狀的多層目錄),則需要啟用 recursive 選項<ref>[http://stackoverflow.com/questions/2303372/create-a-folder-if-it-doesnt-already-exist php - Create a folder if it doesn't already exist - Stack Overflow]  code snippet: <nowiki>mkdir('path/to/directory', 0755, true);</nowiki> </ref>
* 設定該檔案/目錄是否可以寫入: 如果要刪除或寫入檔案,需要檢查是否具備寫入權限
*# SELinux (Security-Enhanced Linux) policy: {{kbd | key = httpd_sys_rw_content_t}}<ref>[https://blog.lysender.com/2015/07/centos-7-selinux-php-apache-cannot-writeaccess-file-no-matter-what/ CentOS 7 + SELinux + PHP + Apache – cannot write/access file no matter what | Lysender's Daily Log Book]</ref>
*# (optional) 該檔案/目錄的 (1) 擁有者是網站使用者 (e.g. 網站伺服器設定檔預設值 CentOS 的 Apache 伺服器是 apache、Ubuntu 的 Apache 伺服器則是 www-data<ref>[https://ubuntuforums.org/showthread.php?t=1293508 [SOLVED] How to find out Apache Username?]</ref>) 、或者是 (2) 網站使用者的群組 (e.g. CentOS 的 Apache 伺服器是 apache、Nginx 伺服器是 nginx) 可以寫入
*# {{Win}} [http://www.webdeveloper.com/forum/showthread.php?165933-File-Permissions-in-Windows-(WAMP) File Permissions in Windows (WAMP)] <ref>[https://technet.microsoft.com/en-us/library/Cc754344.aspx Set, View, Change, or Remove Permissions on Files and Folders]</ref>
* 驗證是否具備寫入權限:
*# 輸入 {{Linux}} 指令 (1) 檢查檔案權限 {{kbd | key = ls -Z /path/to/file}}、(2) 檢查目錄權限 {{kbd | key = ls -Zd /path/to/directory/}}
*# PHP [https://www.php.net/manual/en/function.get-current-user.php get_current_user] function
*# PHP [http://php.net/manual/en/function.is-writable.php is_writable function]


=== 錯誤訊息: ERROR: 00000:: ===
=== 錯誤訊息: ERROR: 00000:: ===

Navigation menu