温馨提示×

如何测试LNMP是否成功

小樊
58
2026-07-23 05:27:27
栏目: 编程语言

要确认 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)是否安装成功,可以按下面步骤逐项测试,从系统服务到实际页面访问,逐步验证。


一、检查各服务是否正常运行

1️⃣ 检查 Nginx

systemctl status nginx

✅ 正常状态应为:

Active: active (running)

测试 Nginx 是否能响应:

curl http://127.0.0.1

或浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。


2️⃣ 检查 MySQL / MariaDB

systemctl status mysqld
# 或
systemctl status mariadb

登录数据库测试:

mysql -u root -p

✅ 能成功进入 MySQL 命令行说明数据库正常。


3️⃣ 检查 PHP

查看 PHP 版本:

php -v

检查 PHP-FPM 是否运行:

systemctl status php-fpm
# 或
systemctl status php7.4-fpm

二、测试 Nginx 是否能解析 PHP(关键)

1️⃣ 创建 PHP 测试文件

vim /usr/share/nginx/html/info.php

内容:

<?php
phpinfo();

2️⃣ 配置 Nginx 支持 PHP(如未配置)

编辑 Nginx 站点配置(示例):

vim /etc/nginx/conf.d/default.conf

确保包含以下内容:

server {
    listen 80;
    server_name localhost;

    root /usr/share/nginx/html;
    index index.php index.html;

    location ~ \.php$ {
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        # 或 127.0.0.1:9000
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

重载 Nginx:

nginx -t
systemctl reload nginx

3️⃣ 访问 PHP 测试页面

浏览器访问:

http://服务器IP/info.php

✅ 看到 PHP Info 页面说明 LNMP 成功。


三、测试 PHP 是否能连接 MySQL

创建测试文件:

vim /usr/share/nginx/html/dbtest.php

内容(MySQL 示例):

<?php
$conn = new mysqli("localhost", "root", "你的密码");
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}
echo "✅ MySQL 连接成功";
?>

访问:

http://服务器IP/dbtest.php

✅ 显示“MySQL 连接成功”说明 PHP + MySQL 正常。


四、常见问题排查

❌ Nginx 能访问但 PHP 不解析

  • PHP-FPM 未启动
  • fastcgi_pass 路径错误
  • 文件权限问题

❌ 403 Forbidden

  • root 路径错误
  • 目录没有权限

❌ 502 Bad Gateway

  • PHP-FPM 未运行
  • sock 文件路径不匹配

五、快速验证清单 ✅

项目 验证方式
Linux uname -a
Nginx curl localhost
MySQL mysql -u root -p
PHP php -v
PHP + Nginx info.php
PHP + MySQL dbtest.php

如果你愿意,可以告诉我:

  • ✅ 系统版本(CentOS / Ubuntu)
  • ✅ 手动安装还是一键脚本(如 LNMP.org)
  • ✅ 报错信息

我可以帮你 精确定位问题。

0 踩