Debian系统上如何一步步详细安装LNMP环境?

更新于
2026-11-24 17:02:13
27阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

一、程序准备与更新

⚠️ 痛点:很多教程默认使用旧版 Debian,导致软件源不可达或依赖冲突。

sudo apt update && sudo apt upgrade -y

如果程序较旧。建议先执行以下操作以确保源可用:

Debian系统上如何一步步详细安装LNMP环境?
sudo apt install -y curl bash

二、安装 Nginx

⚠️ 痛点:直接使用默认源可能安装老旧版本,影响性能。

# 推荐使用官方 stable 版本
sudo apt install -y nginx

启动并设置开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx

三、安装 PHP 与相关模块

⚠️ 痛点:缺少 php-fpm 导致 Nginx 无法通过 FastCGI 与 PHP 沟通。

Debian系统上如何一步步详细安装LNMP环境?
# 安装 PHP 7.4
sudo apt install -y php-fpm php-mysql php-gd php-zip php-curl php-xml php-json

A. 调整 PHP‑FPM 配置

listen = /run/php/php7.4-fpm.sock # 建议使用 socket 提高性能
pm = dynamic # 自适应模式更稳健
pm.max_children = 50 # 根据服务器内存调节
pm.start_servers = 10 # 初始进程数
pm.min_spare_servers = 5 # 最小空闲进程数
pm.max_spare_servers = 35 # 最大空闲进程数

注意:

  • .service file 错误: 忘记在 /etc/systemd/system/php7.4-fpm.service.d/override.conf# 指定 socket 方法
  • .conf 参数拼写错误: 如 fastcgi_param 拼写错误会导致页面空白。

四、安装 MySQL / MariaDB

A. 安装 MySQL& 基础安全配置)

$ sudo apt install -y mysql-server
$ sudo mysql_secure_installation # 按提示设置 root 密码、删除匿名使用者等}
$ sudo systemctl start mysql
$ sudo systemctl enable mysql

$ sudo apt install -y mariadb-server && sudo systemctl start mariadb && sudo systemctl enable mariadb 

五、Nginx 配置虚拟主机并连接 PHP‑FPM&

  1. /etc/nginx/sites-available/yoursite
  2. location ~ \.php$ { include snippets/fastcgi-php.conf;fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;按理说,}

  • /etc/nginx/sites-enabled/yoursite –> 软链接到 sites‑available。
  • sudo nginx -t 检查语法后 sudo systemctl reload nginx 生效。
  • 六、测试 LNMP 是否正常工作&

    1. `cd /var/www/html`
    2. `echo ""> info.php`
    3. `curl http://localhost/info.php` 或在浏览器访问 `http://your_domain/info.php`。
    4. .

    七、常见痛点及方法&

    • ⚡   Nginx 502 Bad Gateway⚡    ✅ 解决办法确认 /run/php/*.sock 已存在且对应的 service 已启动.

  • ⚡  PHP 版本不匹配MySQL驱动
    ✅ *解决办法*:选择与数据库兼容的 PDO 驱动,并在 php.ini 中打开对应
  • ⚡  权限问题导致无法创建数据库或写入日志
    ✅ *解决办法*:为 www-data 使用者赋予正确权限。
  •  .

    标签:Debian

    一、程序准备与更新

    ⚠️ 痛点:很多教程默认使用旧版 Debian,导致软件源不可达或依赖冲突。

    sudo apt update && sudo apt upgrade -y

    如果程序较旧。建议先执行以下操作以确保源可用:

    Debian系统上如何一步步详细安装LNMP环境?
    sudo apt install -y curl bash

    二、安装 Nginx

    ⚠️ 痛点:直接使用默认源可能安装老旧版本,影响性能。

    # 推荐使用官方 stable 版本
    sudo apt install -y nginx

    启动并设置开机自启:

    sudo systemctl start nginx
    sudo systemctl enable nginx

    三、安装 PHP 与相关模块

    ⚠️ 痛点:缺少 php-fpm 导致 Nginx 无法通过 FastCGI 与 PHP 沟通。

    Debian系统上如何一步步详细安装LNMP环境?
    # 安装 PHP 7.4
    sudo apt install -y php-fpm php-mysql php-gd php-zip php-curl php-xml php-json

    A. 调整 PHP‑FPM 配置

    listen = /run/php/php7.4-fpm.sock # 建议使用 socket 提高性能
    pm = dynamic # 自适应模式更稳健
    pm.max_children = 50 # 根据服务器内存调节
    pm.start_servers = 10 # 初始进程数
    pm.min_spare_servers = 5 # 最小空闲进程数
    pm.max_spare_servers = 35 # 最大空闲进程数

    注意:

    • .service file 错误: 忘记在 /etc/systemd/system/php7.4-fpm.service.d/override.conf# 指定 socket 方法
    • .conf 参数拼写错误: 如 fastcgi_param 拼写错误会导致页面空白。

    四、安装 MySQL / MariaDB

    A. 安装 MySQL& 基础安全配置)

    $ sudo apt install -y mysql-server
    $ sudo mysql_secure_installation # 按提示设置 root 密码、删除匿名使用者等}
    $ sudo systemctl start mysql
    $ sudo systemctl enable mysql
    

    $ sudo apt install -y mariadb-server && sudo systemctl start mariadb && sudo systemctl enable mariadb 

    五、Nginx 配置虚拟主机并连接 PHP‑FPM&

    1. /etc/nginx/sites-available/yoursite
    2. location ~ \.php$ { include snippets/fastcgi-php.conf;fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;按理说,}

  • /etc/nginx/sites-enabled/yoursite –> 软链接到 sites‑available。
  • sudo nginx -t 检查语法后 sudo systemctl reload nginx 生效。
  • 六、测试 LNMP 是否正常工作&

    1. `cd /var/www/html`
    2. `echo ""> info.php`
    3. `curl http://localhost/info.php` 或在浏览器访问 `http://your_domain/info.php`。
    4. .

    七、常见痛点及方法&

    • ⚡   Nginx 502 Bad Gateway⚡    ✅ 解决办法确认 /run/php/*.sock 已存在且对应的 service 已启动.

  • ⚡  PHP 版本不匹配MySQL驱动
    ✅ *解决办法*:选择与数据库兼容的 PDO 驱动,并在 php.ini 中打开对应
  • ⚡  权限问题导致无法创建数据库或写入日志
    ✅ *解决办法*:为 www-data 使用者赋予正确权限。
  •  .

    标签:Debian