如何在腾讯云轻量应用服务器上搭建LNMP环境?

在腾讯云轻量应用服务器(Lighthouse)上搭建 LNMP(Linux + Nginx + MySQL + PHP)环境,推荐使用 Ubuntu 22.04 LTSCentOS Stream 9(但注意:CentOS 7/8 已停更,建议优先选 Ubuntu)。以下以 Ubuntu 22.04 为例,提供安全、稳定、生产就绪的详细步骤(含必要优化和验证):


✅ 前提准备

  1. 购买并登录轻量服务器

    • 选择地域、配置(建议 ≥2GB 内存),镜像选 Ubuntu 22.04 LTS
    • 使用 SSH 登录(如 ssh root@你的公网IP),首次登录后建议创建普通用户并禁用 root 密码登录(可选但推荐)。
  2. 更新系统 & 安装基础工具

    sudo apt update && sudo apt upgrade -y
    sudo apt install -y curl wget vim git gnupg2 software-properties-common lsb-release ca-certificates

🌐 步骤一:安装 Nginx(Web 服务器)

sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

✅ 验证:浏览器访问 http://你的公网IP,看到 "Welcome to nginx!" 即成功。

💡 优化建议(可选)
编辑 /etc/nginx/nginx.conf,调整 worker_processes auto;keepalive_timeout 65;;启用 Gzip:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

🐘 步骤二:安装 MySQL(推荐 MySQL 8.0)

# 添加官方 MySQL APT 仓库(确保最新稳定版)
curl -fsSL https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb | sudo dpkg -i -
# 运行配置向导(按 Enter 选默认 → 选择 MySQL Server & Cluster → 选择 8.0 → OK)
sudo dpkg-reconfigure mysql-apt-config
sudo apt update
sudo apt install -y mysql-server

✅ 初始化安全设置(必须执行!):

sudo mysql_secure_installation
# 按提示:设 root 密码(推荐强密码)、禁用匿名用户、禁止远程 root 登录、删除 test 库、重载权限

✅ 验证连接:

sudo mysql -u root -p  # 输入刚设的密码
mysql> SHOW DATABASES;  -- 应看到 database 列表
mysql> EXIT;

⚠️ 注意:Ubuntu 22.04 默认 MySQL 8.0 使用 caching_sha2_password 认证插件,PHP 连接时需兼容(后续 PHP 配置会处理)。


☕ 步骤三:安装 PHP(推荐 PHP 8.1 或 8.2)

# 添加 Ondřej Surý 的 PPA(Ubuntu 官方源 PHP 版本较旧)
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-xmlrpc php8.2-zip php8.2-opcache php8.2-cli

✅ 启动并启用 PHP-FPM:

sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm

✅ 验证 PHP:

php -v  # 应显示 PHP 8.2.x

🔧 步骤四:配置 Nginx + PHP-FPM(关键!)

  1. 备份默认配置

    sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
  2. 编辑默认站点配置(支持 PHP 解析):

    sudo vim /etc/nginx/sites-available/default

    替换全部内容为以下(已优化,支持 .php 解析、隐藏敏感文件、启用 OPcache):

    server {
       listen 80 default_server;
       listen [::]:80 default_server;
       root /var/www/html;
       index index.php index.html index.htm;
       server_name _;
    
       location / {
           try_files $uri $uri/ =404;
       }
    
       # 处理 PHP 文件(关键!)
       location ~ .php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php8.2-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }
    
       # 禁止访问敏感文件
       location ~ /.ht {
           deny all;
       }
       location ~ /.env {
           deny all;
       }
    }
  3. 测试配置 & 重启服务

    sudo nginx -t  # 应输出 "syntax is ok"
    sudo systemctl reload nginx

🧪 步骤五:创建 PHP 测试页

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

✅ 浏览器访问 http://你的公网IP/info.php,看到 PHP 信息页即成功!
🔍 检查页面中:

  • Server API: FPM/FastCGI
  • mysqlnd 扩展已启用 ✅
  • OPcache 状态为 enabled

🔐 安全加固(生产必备)

项目 命令/操作
防火墙(UFW) sudo ufw allow OpenSSH && sudo ufw allow 'Nginx Full' && sudo ufw enable
禁用 root SSH 登录 sudo sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config && sudo systemctl restart sshd
创建普通用户(如 deploy sudo adduser deploy && sudo usermod -aG sudo deploy
MySQL 创建应用用户(非 root) mysql -u root -p -e "CREATE USER 'webapp'@'localhost' IDENTIFIED BY 'StrongPass123!'; GRANT ALL PRIVILEGES ON *.* TO 'webapp'@'localhost'; FLUSH PRIVILEGES;"

📦 可选:一键部署脚本(复制粘贴运行)

# 保存为 setup-lnmp.sh,然后 bash setup-lnmp.sh(需 root 权限)
#!/bin/bash
set -e
echo "🚀 开始安装 LNMP..."
apt update && apt upgrade -y
apt install -y nginx mysql-server php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-opcache

# 配置 Nginx
cat > /etc/nginx/sites-available/default << 'EOF'
server { listen 80; root /var/www/html; index index.php; location / { try_files $uri $uri/ =404; } location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; } }
EOF

systemctl enable nginx php8.2-fpm
systemctl restart nginx php8.2-fpm
echo "<?php echo 'LNMP OK!'; ?>" > /var/www/html/index.php
echo "✅ LNMP 部署完成!访问 http://$(curl -s ifconfig.me)"

❓常见问题排查

现象 解决方案
502 Bad Gateway 检查 sudo systemctl status php8.2-fpm,确认 sock 文件路径 /run/php/php8.2-fpm.sock 存在且权限正确(www-data 用户组)
Connection refused(MySQL) sudo systemctl status mysql,检查是否启动;确认 bind-address = 127.0.0.1(默认安全)
PHP 不解析,显示源码 检查 Nginx 配置中 location ~ .php$ 是否匹配,fastcgi_pass 路径是否与 php8.2-fpm.sock 一致
mysqli_connect(): (HY000/1045) MySQL 8.0 密码认证插件问题 → 在 MySQL 中执行:
ALTER USER 'your_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

至此,LNMP 环境已在腾讯云轻量服务器上稳定运行!
下一步可部署 WordPress、Laravel、Typecho 等应用,或配置 HTTPS(使用腾讯云免费 SSL 证书 + Nginx 配置)。

需要我为你:

  • 提供 HTTPS(Let’s Encrypt)自动配置脚本
  • 输出 WordPress 一键部署命令
  • 生成 MySQL 数据库 + 用户 + 导入 SQL 的完整流程
    欢迎随时告诉我 👇
未经允许不得转载:云知道CLOUD » 如何在腾讯云轻量应用服务器上搭建LNMP环境?