昨天接到一個網友的問題,說yum安裝nginx+php-fpm+mysql+phpMyAdmin後,發現phpMyAdmin沒法打開,一直報502錯誤已經抓狂半天了,本着幫助別人快樂本身的原則,遠程幫他看了一下, 現記錄和總結以下,問題解決思路的總結放在文章最後,問題解決思路總結也是本文的重點。php
問題環境:CentOS6經過yum安裝的nginx+php-fpm+mysql+phpMyAdminhtml
問題描述:安裝完成後發現nginx沒有問題,而phpMyAdmin沒法打開,提示502錯誤mysql
問題解決過程nginx
查看問題環境的安裝包:sql
nginx-filesystem-1.0.15-12.el6.noarch |
nginx-1.0.15-12.el6.x86_64 |
rrdtool-php-1.3.8-7.el6.x86_64 |
php-pear-1.9.4-4.el6.noarch |
php-devel-5.3.3-46.el6_6.x86_64 |
php-mbstring-5.3.3-46.el6_6.x86_64 |
php-mcrypt-5.3.3-3.el6.x86_64 |
php-5.3.3-46.el6_6.x86_64 |
php-tidy-5.3.3-46.el6_6.x86_64 |
php-pecl-memcache-3.0.5-4.el6.x86_64 |
php-xmlrpc-5.3.3-46.el6_6.x86_64 |
php-xmlseclibs-1.3.1-3.el6.noarch |
php-common-5.3.3-46.el6_6.x86_64 |
php-pdo-5.3.3-46.el6_6.x86_64 |
php-xml-5.3.3-46.el6_6.x86_64 |
php-fpm-5.3.3-46.el6_6.x86_64 |
php-cli-5.3.3-46.el6_6.x86_64 |
php-mysql-5.3.3-46.el6_6.x86_64 |
php-eaccelerator-0.9.6.1-1.el6.x86_64 |
php-gd-5.3.3-46.el6_6.x86_64 |
根據nginx報的502錯誤,能夠初步判斷是upstream出現了問題,再提到upstream以前,先列一下nginx的配置文件(去掉註釋,我已經將nginx記錄錯誤日誌的級別從默認級別提高到info)。緩存
user nginx; worker_processes 1; error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; client_max_body_size 10M; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; }
因爲此配置文件中沒有顯式寫明任何server,所以須要查看一下include /etc/nginx/conf.d/*.conf; 所包含的默認server文件,即/etc/nginx/conf.d/default.conf,去掉註釋bash
cat /etc/nginx/conf.d/default.conf server { listen 80 default_server; server_name _; include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } }
初步判斷,此nginx的配置確實沒有問題,應該是php-fpm或者php自己的問題(縮小問題範圍)。服務器
查閱nginx日誌文件(/var/log/nginx/error.log),發現以下提示,肯定是php-fpm的問題,fastcgi也算是對upstream的一種代理app
2015/08/14 17:05:32 [notice] 9645#0: using the "epoll" event method 2015/08/14 17:05:32 [notice] 9645#0: nginx/1.0.15 2015/08/14 17:05:32 [notice] 9645#0: built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 2015/08/14 17:05:32 [notice] 9645#0: OS: Linux 2.6.32-504.el6.x86_64 2015/08/14 17:05:32 [notice] 9645#0: getrlimit(RLIMIT_NOFILE): 65535:65535 2015/08/14 17:05:32 [notice] 9646#0: start worker processes 2015/08/14 17:05:32 [notice] 9646#0: start worker process 9648 2015/08/14 17:05:36 [error] 9648#0: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.228, server: 192.168.1.101, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.101" 2015/08/14 17:09:22 [error] 9648#0: *4 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.228, server: 192.168.1.101, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.101" 2015/08/14 17:11:23 [error] 9648#0: *7 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.228, server: 192.168.1.101, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.101" 2015/08/14 17:11:33 [info] 9648#0: *9 client closed prematurely connection while reading client request line, client: 192.168.1.228, server: 192.168.1.101
建立一個能打開phpinfo的文件,查看php文件可否正確解析(進一步縮小問題範圍)ide
發現php-fpm能正常解析php文件,裏面的各個php組件都顯示正常
查看phpMyAdmin的版本,查閱官方網站的文檔看看是否支持php5.3.3,發現當前的phpMyAdmin支持,所以應該不是phpMyAdmin的問題
開始檢查php-fpm的日誌(/var/log/php-fpm/error.log),發現以下所示:
[14-Aug-2015 16:34:53] NOTICE: fpm is running, pid 9522 [14-Aug-2015 16:34:53] NOTICE: ready to handle connections [14-Aug-2015 16:43:54] WARNING: [pool www] child 9527 exited on signal 11 (SIGSEGV) after 541.401349 seconds from start [14-Aug-2015 16:43:55] NOTICE: [pool www] child 9614 started [14-Aug-2015 16:44:00] WARNING: [pool www] child 9526 exited on signal 11 (SIGSEGV) after 547.107407 seconds from start [14-Aug-2015 16:44:00] NOTICE: [pool www] child 9615 started [14-Aug-2015 17:05:36] WARNING: [pool www] child 9523 exited on signal 11 (SIGSEGV) after 1843.098829 seconds from start [14-Aug-2015 17:05:36] NOTICE: [pool www] child 9649 started
這個日誌顯然不足以提供足夠的信息來解決問題,所以修改php-fpm和php.ini對日誌級別的一些參數配置,以提高日誌級別,獲取詳細的錯誤信息。
搜索配置文件的中log關鍵字,或者根據文檔或資料修改,一些方法或步驟以下:
/etc/php-fpm.conf文件,將日誌級別從notice改動到debug
log_level = debug
/etc/php-fpm.d/www.conf文件,將php worker的標準輸出和錯誤輸出從/dev/null 重定向到主要的錯誤日誌中,即/var/log/php-fpm/error.log
catch_workers_output = yes
/etc/php.ini文件
error_reporting = E_ALL & ~E_DEPRECATED display_errors = On display_startup_errors = On log_errors = On track_errors = On html_errors = On
再次從新啓動php-fpm,發現worker中的詳細錯誤:
[14-Aug-2015 17:09:18] NOTICE: fpm is running, pid 9672 [14-Aug-2015 17:09:18] NOTICE: ready to handle connections [14-Aug-2015 17:09:22] WARNING: [pool www] child 9673 said into stderr: "[Fri Aug 14 17:09:22 2015" [14-Aug-2015 17:09:22] WARNING: [pool www] child 9673 said into stderr: "] [notice] EACCELERATOR(9673): PHP crashed on opline 30 of PMA_URL_getCommon() at /usr/share/nginx/html/libraries/url_generating.lib.php:188" [14-Aug-2015 17:09:22] WARNING: [pool www] child 9673 said into stderr: "" [14-Aug-2015 17:09:22] WARNING: [pool www] child 9673 exited on signal 11 (SIGSEGV) after 4.286828 seconds from start [14-Aug-2015 17:09:22] NOTICE: [pool www] child 9679 started [14-Aug-2015 17:11:23] WARNING: [pool www] child 9675 said into stderr: "[Fri Aug 14 17:11:23 2015" [14-Aug-2015 17:11:23] WARNING: [pool www] child 9675 said into stderr: "] [notice] EACCELERATOR(9675): PHP crashed on opline 30 of PMA_URL_getCommon() at /usr/share/nginx/html/libraries/url_generating.lib.php:188"
錯誤信息中提到EACCELERATOR這個php模塊,所以先肯定一下是否是因爲這個模塊有問題,所以,先將此模塊禁用,方法是將/etc/php.d/eaccelerator.ini文件更改個後綴名稱,例如mv /etc/php.d/eaccelerator.ini /etc/php.d/eaccelerator.ini~,而後重啓php-fpm,再校驗一下結果,發現問題已經解決。
多是eaccelerator與phpMyAdmin衝突的緣由,所以要想使用phpMyAdmin能夠將此模塊禁用,或者安裝時跳過這個包。
註釋:eAccelerator是一個自由開放源碼php加速器,優化和動態內容緩存,提升了php腳本的緩存性能,使得PHP腳本在編譯的狀態下,對服務器的開銷幾乎徹底消除。它還有對腳本起優化做用,以加快其執行效率。使PHP程序代碼執效率能提升1-10倍。(來自bdbk)
問題解決思路總結
第0條,溝通是診斷故障的關鍵,詳細瞭解問題始末,例如部署方案,步驟,作了哪些操做等
第一,根據經驗判斷,nginx+php-fpm+phpMyAdmin是很牢靠的組合,所以判斷這是個例問題,而不是批量問題,所以直接開始動手,登陸到系統中查看安裝的軟件包,nginx、php和phpMyAdmin版本都是要查看的,此步驟有助於根據掌握的知識和經驗,初步判斷是否相互兼容,是否有未修復bug等。
第二,執行nginx -t檢查nginx的配置文件有無顯式錯誤,檢查nginx運行狀態
第三,執行php-fpm -t檢查php-fpm的配置文件有無顯式錯誤,檢查php-fpm的運行狀態
第四,檢查錯誤日誌,先檢查nginx的錯誤日誌,由於它是「第一現場」,再檢查php-fpm日誌,由於它是「第二現場」
第五,若是日誌提示明顯,則按照日誌提示,修改相應的配置文件,再次驗證問題
第六,若是依然有問題,則本步驟就是解決問題的最關鍵的步驟,須要提高記錄日誌的級別,這也就是爲何有debug爲何叫作調試,將nginx的日誌級別提高到info(爲何不能提高到debug,nginx編譯時有個--debug選項,不肯定時能夠不用),將php的日誌級別提高到debug,打開全部的php調試開關
第七,從新啓動nginx和php-fpm後,配置文件生效,從新打開網頁重現問題,再次打開日誌,根據日誌提示內容再次,修改相應的配置文件,再次驗證問題
第八,若是反覆修改無果後,該查閱官方手冊就查閱官方手冊,該Google 搜索就Google搜索,該反饋bug就反饋bug,若是持續無果,則換種解決問題的方式,尋找正確的解決方案,參照以下:
參考已有的成功的版本組合,更換版本組合或者修改配置文件,消除環境差別性,適用於快速解決問題
將yum安裝改成編譯安裝,或者yum安裝更少的包,以最小化的安裝方式將問題範圍縮減到最小,從而肯定問題,提高解決問題的能力,適用於研究和學習
最後補充一句:只要出現的問題可以重現,而不是隨機出現,則就必定能很好的解決,所以不要慌,也不要浮躁,更不要放棄,甚至能夠緩一緩後再冷靜處理。
--end--