不想用宝塔面板、想完全掌控环境、或者只是想知道"网站环境到底怎么搭的"——纯命令行搭建 LNMP(Linux + Nginx + MySQL + PHP)是最值得掌握的技能。它是国内最主流的生产环境组合,这篇文章以 CentOS 7 为例,一步步从零部署一套能跑 WordPress 的 LNMP 环境。
一、架构说明
LNMP 的请求链路:浏览器 → Nginx(静态资源 / 反向代理)→ PHP-FPM(处理 PHP 动态请求)→ MySQL(存取数据)
用户请求 → Nginx(80/443) → 静态文件直接返回
→ .php 转发给 PHP-FPM(9000) → 连接 MySQL(3306)
Nginx 负责静态资源和转发,PHP 负责动态解析,MySQL 负责数据存储,各司其职。
二、安装 Nginx
使用官方源安装,能拿到较新版本:
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
systemctl start nginx
systemctl enable nginx
systemctl status nginx # 确认 active (running)
验证:浏览器访问 http://服务器IP,看到 Welcome to nginx 页面即成功。访问不了先检查安全组/防火墙是否放行 80 端口。
三、安装 MySQL(MariaDB)
CentOS 7 默认仓库是 MariaDB,生产环境也够用:
yum install -y mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation # 交互式安全设置:设 root 密码、删匿名用户、禁远程 root
登录验证:
mysql -uroot -p
# 创建网站数据库
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '强密码';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT
用独立用户而非 root 连库,是安全底线。
四、安装 PHP 与 PHP-FPM
CentOS 7 自带 PHP 5.4 太老,用 Remi 源装 PHP 8.x:
rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php82
yum install -y php82-php-fpm php82-php-mysqlnd php82-php-gd php82-php-mbstring php82-php-xml php82-php-curl
Ubuntu/Debian 用
apt install -y php php-fpm php-mysql php-gd php-mbstring php-xml php-curl即可(自带较新版本)。
启动并配置:
systemctl start php82-php-fpm
systemctl enable php82-php-fpm
确认 PHP-FPM 监听:ss -tlnp | grep 9000。PHP-FPM 默认以 apache 用户运行,改成 nginx:
sed -i 's/user = apache/user = nginx/; s/group = apache/group = nginx/' /etc/opt/remi/php82/php-fpm.d/www.conf
systemctl restart php82-php-fpm
五、配置 Nginx 站点
创建站点配置 /etc/nginx/conf.d/example.com.conf:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
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;
}
location ~ /\.ht {
deny all;
}
}
核心点:try_files 实现伪静态;.php 请求转发给 fastcgi_pass(PHP-FPM);SCRIPT_FILENAME 拼出脚本完整路径(写错会 502)。
创建目录并测试:
mkdir -p /var/www/example.com
echo "<?php phpinfo(); ?>" > /var/www/example.com/index.php
nginx -t # 检查配置语法
systemctl reload nginx
访问 http://服务器IP/index.php 能看到 PHP 信息页即整套环境打通。
六、部署 WordPress 收尾
cd /var/www/example.com
wget https://cn.wordpress.org/latest-zh_CN.zip
unzip latest-zh_CN.zip && mv wordpress/* . && rm -rf wordpress
chown -R nginx:nginx /var/www/example.com # 属主改成 Nginx 用户
chmod -R 755 /var/www/example.com
浏览器访问域名 → 填数据库信息(主机 localhost、库名 wordpress、用户 wpuser)→ 完成安装。
七、常见问题排查
- 502 Bad Gateway:PHP-FPM 没启动或配置错了(改 www.conf 后没重启、9000 端口被占),
systemctl status php82-php-fpm、ss -tlnp | grep 9000排查; - 404 Not Found:
try_files没写、nginx 用户对目录无权限(chown nginx:nginx)、index.php没配在 index 里; - PHP 代码不执行(直接下载):.php location 块缺失或
fastcgi_pass没配对; - 连不上数据库:MySQL 用户权限没给对(按
localhost授权)、密码特殊字符在 wp-config.php 里转义问题; - 安全组:80/443 端口在云控制台安全组放行,数据库 3306 只允许本机连接(
GRANT ... TO 'wpuser'@'localhost'已保证)。
八、总结
LNMP 搭建流程一句话:装 Nginx → 装 MySQL → 装 PHP-FPM → 配置站点转发 → 部署程序。三个关键点:PHP-FPM 的 fastcgi_pass 地址要一致、目录属主给 nginx 用户、try_files 配伪静态。照着这篇文章走一遍,你就理解了一个网站环境最底层的运转逻辑,比盲目点面板心里有底得多。




