LNMP架构搭建wordpress博客

一、环境概述

  • 架构组成:Linux(CentOS7)+ Nginx(Web 服务器)+ MariaDB(MySQL 兼容数据库)+ PHP(PHP-FPM 进程管理)

  • 核心优势:轻量高效、并发处理能力强,适合中小型网站、博客、企业官网等场景

  • 适配场景:生产环境、测试环境、高并发静态资源服务(动态内容需配合 PHP-FPM)

  • 组件版本参考(默认 / 推荐):

组件 系统默认版本 推荐版本 安装方式
Nginx 1.12.x 1.20.x+ YUM(EPEL 源)/ 源码编译
MariaDB 5.5.x 10.11.x YUM / 官方镜像源
PHP 5.4.x 7.4.x/8.3.x YUM(Remi 源)
PHP-FPM 随 PHP 配套 与 PHP 版本一致 随 PHP 安装

二、前置准备

1. 系统初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 1. 备份并替换阿里云镜像源(加速安装,避免官方源卡顿)

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 2. 安装基础工具

sudo yum install -y vim wget net-tools yum-utils

# 3. 防火墙配置(开放80/443/22端口)

sudo firewall-cmd --permanent --add-service=http

sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --permanent --add-port=22/tcp

sudo firewall-cmd --reload

#或者停止防火墙服务

systemctl stop firewalld
systemctl disable firewalld

# 4. SELinux配置(避免权限拦截,核心步骤)

sudo setenforce 0 # 临时关闭(测试环境)

sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config # 永久关闭(需重启)

2. 依赖环境检查

1
2
3
# 检查是否已安装冲突服务(如Apache httpd),如有则卸载

rpm -qa | grep httpd && sudo yum remove -y httpd

三、核心组件安装与配置

(一)Nginx 安装与基础配置

1. 安装 Nginx(通过 EPEL 源,避免报错)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 安装阿里云EPEL源(解决默认EPEL源无法访问问题)

sudo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

sudo yum repolist | grep epel # 验证仓库是否生效

# 安装Nginx

sudo yum install -y nginx

# 启动并设置开机自启

sudo systemctl start nginx

sudo systemctl enable nginx

# 验证Nginx状态(确保显示 active (running))

sudo systemctl status nginx

2. 基础配置验证

  • 浏览器访问 http://服务器IP,出现 “Welcome to nginx!” 页面即安装成功

  • 核心配置文件路径:

    • 主配置文件:/etc/nginx/nginx.conf

    • 虚拟主机配置目录:/etc/nginx/conf.d/(默认配置文件:default.conf

    • 网站根目录(默认):/usr/share/nginx/html

(二)MariaDB 安装与安全配置

1. 安装与启动

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装MariaDB服务器与客户端

sudo yum install -y mariadb-server mariadb

# 启动并设置开机自启

sudo systemctl start mariadb

sudo systemctl enable mariadb

# 安全初始化(必做!设置root密码、禁用匿名用户)

sudo mysql_secure_installation
  • 初始化步骤提示:
  1. 无初始密码,直接回车 → 2. 设置 root 强密码 → 3. 全选 Y(删除匿名用户、禁止 root 远程登录、删除测试库、刷新权限)

2. 数据库用户与权限配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 登录数据库

mysql -u root -p

# 创建应用数据库

CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

# 创建专用数据库用户并授权

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'StrongPass@2025';

GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';


#或:grant all privileges on *.* to root@'%' identified by "000000";

FLUSH PRIVILEGES;

# 退出数据库

EXIT;

(三)PHP 与 PHP-FPM 安装配置(核心联动步骤)

1. 安装 PHP 及扩展(以 PHP7.4 为例,兼容主流应用)

1
2
3
4
5
6
7
8
9
10
11
# 安装Remi源(支持高版本PHP)

sudo yum install -y http://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm

# 启用PHP7.4仓库

sudo yum-config-manager --enable remi-php74

# 安装PHP及核心扩展(适配网站运行需求)

sudo yum install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml php-curl php-zip php-opcache

2. 关键配置(Nginx 与 PHP-FPM 联动)

(1)修改 PHP-FPM 用户组(适配 Nginx 权限)
1
sudo vim /etc/php-fpm.d/www.conf
  • 替换以下配置(默认用户为 apache,需改为 nginx):
1
2
3
4
5
user = nginx

group = nginx

listen = 127.0.0.1:9000 # 保持TCP监听(或使用unix:/tmp/php-cgi.sock)
(2)启动 PHP-FPM 并设置自启
1
2
3
4
5
6
7
sudo systemctl start php-fpm

sudo systemctl enable php-fpm

sudo systemctl status php-fpm # 验证状态

sudo netstat -nptl #查看工作端口
(3)配置 Nginx 支持 PHP 解析(核心联动)
1
sudo vim /etc/nginx/conf.d/default.conf
  • 替换 server 区块内容(确保 PHP 脚本能被正确解析):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
server {

listen 80;

server_name 服务器IP/域名; # 替换为实际IP或域名

location / {

root /usr/share/nginx/html; # 网站根目录

index index.php index.html index.htm; # 优先加载index.php

try_files $uri $uri/ =404; # 防止路径遍历

}

# PHP脚本处理规则(关键配置)

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000; # 与PHP-FPM监听地址一致

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 脚本路径拼接

include fastcgi_params; # 加载FastCGI参数

}

# 静态资源缓存优化(补充配置)

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

expires 30d; # 图片缓存30天

}

location ~ .*\.(js|css)?$ {

expires 12h; # 脚本样式缓存12小时

}

}
(4)验证 Nginx 配置并重启
1
2
3
sudo nginx -t  # 检查配置语法(无错误提示则正常)

sudo systemctl restart nginx # 重启生效

3. 环境验证

1
2
3
# 创建PHP信息测试文件

echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php
  • 浏览器访问 http://服务器IP/info.php,显示 PHP 版本及扩展信息即配置成功

(四)虚拟主机配置(多网站部署,补充内容)

1
2
3
# 创建虚拟主机配置文件

sudo vim /etc/nginx/conf.d/example.conf
  • 配置示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {

listen 80;

server_name www.example.com example.com; # 绑定域名



location / {

root /usr/share/nginx/html; # 自定义网站目录

index index.php index.html;

try_files $uri $uri/ =404;

}

location ~\.php$ {

fastcgi_pass 127.0.0.1:9000;

root /usr/share/nginx/html;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

access_log /var/log/nginx/example_access.log; # 访问日志

error_log /var/log/nginx/example_error.log; # 错误日志

}
  • 创建目录并授权:
1
2
3
4
5
6
7
8
sudo mkdir -p  /usr/share/nginx/html

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

sudo chmod -R 755 /usr/share/nginx/html

sudo nginx -t && sudo systemctl restart nginx
#或重新加载 nginx 使配置文件生效:nginx -s reload

四、应用部署实战(以 WordPress 为例)

1. 下载与解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 进入网站根目录(以默认目录为例)

cd /usr/share/nginx/html

# 下载WordPress安装包

wget https://wordpress.org/latest.tar.gz

# 解压并授权

tar -xzvf latest.tar.gz

mv wordpress/* ./

rm -rf wordpress latest.tar.gz

# 设置权限(关键!避免访问报错)

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

sudo find /usr/share/nginx/html -type d -exec chmod 755 {} \;

sudo find /usr/share/nginx/html -type f -exec chmod 644 {} \;

2. 修改数据库相关配置

1
2
3
4
5
#复制模板配置

cp wp-config_sample.conf wp-config.conf

vi wp-config.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
... 
/** WordPress 数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL 数据库用户名 */
define('DB_USER', 'root');

/** MySQL 数据库密码 */
define('DB_PASSWORD', '000000');

/** MySQL 主机 */
define('DB_HOST', 'localhost');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
...

3. 安装流程

  1. 浏览器访问 http://服务器IP, 点击 “现在就开始”

  2. 输入数据库信息(前面创建的 wordpress_dbwp_user、密码)

  3. 填写网站标题、管理员账号密码 → 完成安装

4. 适配注意事项

  • PHP5.4.x 仅支持 WordPress 3.7-4.9.x(需升级 PHP 版本以适配新版 WordPress)

  • 若提示 “缺少 MySQL 扩展”,重新安装扩展:sudo yum install -y php-mysqlnd

五、HTTPS 配置(双服务器架构:CA+Web,补充内容)

(一)CA 服务器配置(参考 LAMP 架构 CA 配置,适配 LNMP)

  1. 生成根证书(同 LAMP 步骤,路径不变)

  2. 签署 Web 服务器证书(流程一致)

(二)Web 服务器(Nginx)HTTPS 配置

1
2
3
# 编辑Nginx SSL配置文件

sudo vim /etc/nginx/conf.d/ssl.conf
  • 配置示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
server {

listen 443 ssl;

server_name 服务器IP/域名;

# 证书路径(替换为实际路径)

ssl_certificate /etc/nginx/ssl/web.crt; # CA签署的服务器证书

ssl_certificate_key /etc/nginx/ssl/web.key; # Web服务器私钥

ssl_trusted_certificate /etc/nginx/ssl/cacert.pem; # CA根证书

# SSL优化配置(补充)

ssl_protocols TLSv1.2 TLSv1.3;

ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;

ssl_prefer_server_ciphers on;

root /usr/share/nginx/html;

index index.php index.html;

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}
  • 重启 Nginx:sudo systemctl restart nginx

  • 测试访问:https://服务器IP(自签名证书首次访问需忽略信任警告)


LNMP架构搭建wordpress博客
https://netguy6.github.io/2026/02/05/LNMP架构搭建wordpress博客/
作者
net06
发布于
2026年2月5日
许可协议