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)寻求进一步帮助。