温馨提示×

怎么调试linux lnmp环境

小樊
43
2026-09-09 17:04:01
栏目: 智能运维

调试 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常要分层次进行:网络 → Nginx → PHP → 数据库 → 日志 → 权限。下面给你一套系统的排查思路和常用命令。


一、先确认基础服务是否在运行

1. 查看服务状态

systemctl status nginx
systemctl status php-fpm
systemctl status mysql
# 或 mariadb
systemctl status mariadb

如果没启动:

systemctl start nginx
systemctl start php-fpm
systemctl start mysql

设置开机启动:

systemctl enable nginx php-fpm mysql

二、Nginx 层调试

1. 检查 Nginx 配置是否正确

nginx -t

有错误会直接提示哪一行。

2. 查看 Nginx 错误日志

tail -f /var/log/nginx/error.log

常见错误:

  • permission denied → 权限问题
  • File not found → root 路径或 PHP 配置错误
  • connect() failed (111: Connection refused) → PHP-FPM 没起

3. 测试 Nginx 是否能访问

curl http://localhost

三、PHP 层调试

1. 确认 PHP-FPM 在监听

netstat -lntp | grep php-fpm
# 或
ss -lntp | grep php-fpm

默认监听:

127.0.0.1:9000

2. 检查 Nginx 是否转发 PHP

Nginx 配置示例(关键):

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

3. 单独测试 PHP

php -v
php -m

写个测试文件:

<?php
phpinfo();

4. 查看 PHP 错误日志

tail -f /var/log/php-fpm/error.log
# PHP 自身错误
tail -f /var/log/php_errors.log

(php.ini 中开启)

display_errors = On
error_log = /var/log/php_errors.log

四、MySQL / MariaDB 调试

1. 是否能登录

mysql -u root -p

2. 查看错误日志

tail -f /var/log/mysqld.log
# 或
tail -f /var/log/mysql/error.log

3. PHP 连接数据库测试

<?php
$conn = new mysqli("127.0.0.1", "user", "pass", "db");
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "OK";

五、文件权限问题(非常常见)

1. 网站目录权限

chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

(用户可能是 nginxwww-data

2. SELinux(CentOS / RHEL)

getenforce

临时关闭:

setenforce 0

六、防火墙 & 端口

iptables -L
# 或
firewall-cmd --list-all

开放端口:

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

七、排错思路总结(推荐顺序)

  1. nginx -t
  2. systemctl status
  3. curl localhost
  4. 看 Nginx error.log
  5. 看 PHP-FPM log
  6. 测试 PHP 文件
  7. 测试数据库连接
  8. 检查权限 / SELinux / 防火墙

八、如果你愿意,我可以更具体地帮你

你可以告诉我:

  • 使用的系统(CentOS / Ubuntu / Debian)
  • 报的是什么错(截图或日志)
  • 访问 502 / 404 / 空白页 / 数据库连不上

我可以直接帮你定位到具体配置文件和修复命令。

0