温馨提示×

LNMP如何搭建WordPress

小樊
46
2026-07-11 00:17:21
栏目: 编程语言

下面给你一套从零搭建 LNMP + WordPress 的完整流程(适合 CentOS 7/8、Rocky、AlmaLinux,Ubuntu 略有差异我会标注)。
LNMP = Linux + Nginx + MySQL/MariaDB + PHP


一、环境准备

# 关闭 SELinux(避免权限问题)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 防火墙放行 80/443
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

二、安装 Nginx

CentOS / Rocky

yum install -y epel-release
yum install -y nginx
systemctl enable --now nginx

Ubuntu

apt update
apt install -y nginx
systemctl enable --now nginx

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


三、安装 MySQL / MariaDB

安装 MariaDB(推荐)

yum install -y mariadb mariadb-server
systemctl enable --now mariadb

安全初始化

mysql_secure_installation

设置 root 密码,其余选 Y

创建 WordPress 数据库

mysql -uroot -p
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wp123456';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

四、安装 PHP(WordPress 推荐 PHP 8.0+)

CentOS 7

yum install -y yum-utils
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php81
yum install -y php php-fpm php-mysql php-gd php-mbstring php-xml php-json

CentOS 8 / Rocky

dnf install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml php-json

启动 PHP-FPM

systemctl enable --now php-fpm

五、下载并配置 WordPress

cd /usr/share/nginx/html
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar -zxvf latest-zh_CN.tar.gz
mv wordpress/* .
rm -rf wordpress latest-zh_CN.tar.gz

设置权限

chown -R nginx:nginx /usr/share/nginx/html
chmod -R 755 /usr/share/nginx/html

六、配置 Nginx 支持 PHP

编辑配置文件:

vi /etc/nginx/conf.d/wordpress.conf

示例配置(✅ 推荐):

server {
    listen 80;
    server_name yourdomain.com;

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

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

测试并重载:

nginx -t
systemctl reload nginx

七、安装 WordPress

浏览器访问:

http://服务器IP

选择语言 → 填写数据库信息:

数据库名:wordpress
用户名:wpuser
密码:wp123456
数据库主机:localhost
表前缀:wp_

✅ 安装完成即可进入后台


八、常见问题排查

1️⃣ 页面空白

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

2️⃣ 无法上传文件

chown -R nginx:nginx /usr/share/nginx/html

3️⃣ 伪静态失效

确认 Nginx 中已配置:

try_files $uri $uri/ /index.php?$args;

九、可选优化(强烈推荐)

  • ✅ 安装 SSL(Let’s Encrypt)
  • ✅ PHP 使用 Unix Socket
  • ✅ 开启 Nginx 缓存
  • ✅ 使用 Redis / Memcached

如果你愿意,我也可以帮你:

  • Ubuntu 专用版
  • 一键脚本
  • Docker 方式
  • 生产环境安全加固

你现在用的是 CentOS 还是 Ubuntu?

0