LNMP在Ubuntu上的錯誤日志解決流程
LNMP環境的錯誤日志分散在不同服務中,需先明確各服務的日志路徑(Ubuntu系統默認路徑):
/var/log/nginx/error.log
(記錄Nginx配置、端口占用、請求處理等問題);/var/log/php7.x-fpm.log
或/var/log/php-fpm/error.log
(7.x
為實際PHP版本,記錄PHP代碼執行、進程管理錯誤);/var/log/mysql/error.log
(記錄數據庫連接、查詢、權限等問題)。使用tail -f
命令實時監控日志輸出,快速定位最新錯誤:
sudo tail -f /var/log/nginx/error.log # 監控Nginx錯誤
sudo tail -f /var/log/php7.x-fpm.log # 監控PHP-FPM錯誤(替換版本號)
sudo tail -f /var/log/mysql/error.log # 監控MySQL錯誤
通過實時日志,可直接看到錯誤發生的時間、類型及詳細描述(如“Permission denied”“Connection refused”)。
nginx: [emerg] invalid number of arguments in "listen" directive
”。sudo nginx -t
命令測試配置文件語法,根據提示修復錯誤(如listen
指令缺少端口),修復后重啟Nginx:sudo systemctl restart nginx
。bind() to 0.0.0.0:80 failed (98: Address already in use)
”。sudo netstat -tulnp | grep 80
命令查找占用80端口的進程,用kill -9 <進程ID>
終止該進程,再重啟Nginx。upstream prematurely closed connection
”。sudo systemctl status php7.x-fpm
),確認Nginx配置中的fastcgi_pass
指令指向正確的PHP-FPM監聽地址(如unix:/run/php/php7.x-fpm.sock
或127.0.0.1:9000
),并確保兩者用戶/組權限一致(如均為www-data
)。PHP Parse error: syntax error, unexpected ';' in /var/www/html/index.php on line 10
”。php.ini
中設置error_reporting=E_ALL
和display_errors=1
)。WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
”。/etc/php/7.x/fpm/pool.d/www.conf
),調整pm.max_children
值(根據服務器內存計算,如1GB內存可設置為20-30),重啟PHP-FPM使配置生效。PDOException: SQLSTATE[HY000] [2002] No such file or directory
”。localhost
或127.0.0.1
)、端口(默認3306
)、用戶名/密碼是否正確;用sudo systemctl status mysql
確認MySQL服務是否啟動,用mysql -u root -p
登錄數據庫驗證用戶權限(SHOW GRANTS FOR 'username'@'localhost';
)。Access denied for user 'www-data'@'localhost'
”。GRANT ALL PRIVILEGES ON database_name.* TO 'www-data'@'localhost'; FLUSH PRIVILEGES;
),并確保MySQL用戶的主機設置正確(localhost
或%
)。grep
命令過濾關鍵錯誤(如sudo grep -i "error" /var/log/nginx/error.log
),快速定位問題類型;www-data
)和PHP-FPM(www-data
)用戶對網站目錄(如/var/www/html
)有讀寫權限(sudo chown -R www-data:www-data /var/www/html
,sudo chmod -R 755 /var/www/html
)。修改配置文件或修復問題后,需重啟對應服務使更改生效:
sudo systemctl restart nginx # 重啟Nginx
sudo systemctl restart mysql # 重啟MySQL
sudo systemctl restart php7.x-fpm # 重啟PHP-FPM(替換版本號)
通過以上流程,可系統性地解決LNMP在Ubuntu上的大部分錯誤。若問題仍未解決,建議結合具體錯誤日志信息,查閱官方文檔或社區論壇(如Stack Overflow)尋求進一步幫助。