基于openEuler系统部署WordPress个人博客网站

1. 规划节点

节点规划,见表1。

表1 节点规划

IP 主机名 节点
192.168.100.3 wordpress nginx、php、mysql、wordpress

2. LNMP简介

LNMP 是由 Linux 操作系统Nginx Web 服务器MySQL 数据库PHP 脚本语言 组成的 高性能动态网站部署方案,广泛应用于现代 Web 开发领域。它通过各组件的协同工作,实现了对静态资源、动态逻辑和数据存储的高效管理,是部署 WordPress、电商系统、API 服务等动态网站的核心技术栈之一。LNMP 以开源免费轻量灵活高并发支持 为核心优势,解决了传统 LAMP(Apache + MySQL + PHP)架构在性能和可扩展性上的局限,成为云原生时代动态网站开发的标准解决方案。

  1. Linux 操作系统
    • 提供稳定运行环境,管理硬件资源和系统服务。
    • 支持主流发行版(如 Ubuntu、CentOS),适配性强。
  2. Nginx Web 服务器
    • 高性能反向代理与负载均衡,优化静态资源(HTML/CSS/JS/图片)的响应速度。
    • 通过 FastCGI 协议与 PHP-FPM 协作,高效处理动态请求。
  3. MySQL 数据库
    • 支持结构化数据存储,提供事务处理与 ACID 特性,保障数据一致性。
    • 适配 PHP 的 mysqli/PDO 扩展,实现灵活的数据交互。
  4. PHP 动态脚本语言
    • 解析 .php 文件,生成动态 HTML 内容,执行业务逻辑(如用户认证、表单处理)。
    • 与 MySQL 协同工作,构建动态网站的核心功能。

3. LNMP 工作原理与协作流程

当用户通过浏览器访问网站时,请求将按以下标准化流程高效流转:

(1) Nginx 接收请求

  • 用户输入 URL(如 http://example.com/index.php

  • Nginx 根据 nginx.conf

    配置智能判断请求类型:

    • 静态资源.html, .css, .js, .jpg):直接返回文件
    • 动态资源.php):转发至 PHP 处理器(PHP-FPM)

(2) PHP-FPM 解析动态请求

  • Nginx 通过 FastCGI 协议.php 请求精准转发至 PHP-FPM
  • PHP-FPM 启动子进程执行 PHP 脚本,完成以下关键步骤:
    • 加载 PHP 代码(如 WordPress 的 wp-blog-header.php
    • 连接 MySQL 数据库,执行 SQL 查询(如获取文章列表)
    • 将查询结果嵌入 HTML 模板,生成最终页面

(3)MySQL 提供数据支持

  • PHP 脚本通过 wp-config.php 中的数据库参数连接 MySQL:
    • DB_NAME:数据库名
    • DB_USER:数据库用户名
    • DB_PASSWORD:数据库密码
  • 执行 SQL 语句(如 SELECT * FROM wp_posts
  • 将查询结果安全返回给 PHP 用于动态内容生成

(4)Nginx 返回响应

  • PHP-FPM 将生成的 HTML 完整返回给 Nginx
  • Nginx 通过 HTTP 协议将响应高效传输至用户浏览器
  • 全流程完成:用户浏览器渲染最终页面

关键特性
该流程通过 Nginx 静态资源直连 + FastCGI 动态请求转发 实现最优性能,避免 Apache 的进程模型开销,单服务器可支撑 10,000+ 并发连接(实测数据)。

4. 基础准备

修改主机名

[root@localhost ~]# hostnamectl set-hostname wordpress
[root@localhost ~]# bash


Welcome to 6.6.0-28.0.0.34.oe2403.x86_64

System information as of time:  Wed Dec  3 09:05:12 PM CST 2025

System load:  0.01
Memory used:  4.4%
Swap used:  0%
Usage On:   3%
IP address:   192.168.100.4
Users online:   1


[root@wordpress ~]# 

设置防火墙和selinux开机不自启

[root@wordpress ~]# systemctl disable firewalld --now && setenforce 0

永久关闭selinux

[root@wordpress ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

配置华为云YUM源,openEuler常用repo源链接

[root@wordpress ~]# cp -rvf /etc/yum.repos.d/openEuler.repo /tmp/
[root@wordpress ~]# sed -i \
  -e '/^baseurl/ s#http://repo.openeuler.org#https://mirrors.huaweicloud.com/openeuler#g' \
  -e '/^gpgkey/ s#http://repo.openeuler.org#https://mirrors.huaweicloud.com/openeuler#g' \
  /etc/yum.repos.d/openEuler.repo

清理YUM/DNF的所有缓存文件(包括元数据、软件包缓存等)并快速生成YUM缓存

[root@wordpress ~]# yum clean all
[root@wordpress ~]# yum makecache

安装工具

[root@wordpress ~]# yum install -y vim tar net-tools lrzsz lsof bash-com*
[root@wordpress ~]# source /etc/profile

案例实施

1. 部署 Nginx 环境

(1)安装nginx
[root@wordpress ~]# yum install -y nginx
(2)启动nginx
[root@wordpress ~]# systemctl enable nginx --now

浏览器测试访问nginx首页

image-20251203212620547

2. 部署 MySQL 环境

(1)安装mysql
[root@wordpress ~]# yum install -y mysql-server
(2)设置mysql开机自启
[root@wordpress ~]# systemctl enable mysqld --now
(3)创建wordpress数据库

设置mysql的root密码

[root@wordpress ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.44 Source distribution

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by '000000';
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

直接登录失败需要使用密码登录

[root@wordpress ~]# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.44 Source distribution

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

创建wordpress用户和wordpress数据库

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> create user 'wordpress'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'wordpress'@'localhost';
+------------------------------------------------------------------+
| Grants for wordpress@localhost                                   |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `wordpress`@`localhost`                    |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO `wordpress`@`localhost` |
+------------------------------------------------------------------+
2 rows in set (0.00 sec)

3. 部署 PHP 环境

(1)安装php
[root@wordpress ~]# yum install -y php php-fpm php-mysqlnd php-gd php-common php-cli php-mbstring php-xml php-opcache
# PHP核心解释器:解析执行PHP代码,PHP运行的基础
# PHP-FPM进程管理器:接收Nginx请求并管理PHP进程,实现PHP与Nginx协同
# MySQL原生驱动:高效连接MySQL/MariaDB数据库,支持数据操作
# GD图形扩展:处理图片生成、裁剪等,满足图片功能需求
# PHP通用组件库:提供基础配置和公共函数,支撑其他扩展
# PHP命令行工具:终端执行PHP脚本,用于调试和命令行操作
# 多字节字符串扩展:处理中文等多字节字符,解决编码问题
# XML解析扩展:支持XML数据处理,适配依赖XML的功能
# Opcode缓存扩展:缓存预编译代码,提升PHP运行性能
(2)设置php开机自启
[root@wordpress ~]# systemctl enable php-fpm --now

4. 配置 Nginx 解析 PHP

(1)配置 nginx 文件
[root@wordpress ~]# vim /etc/nginx/conf.d/wordpress.conf
# WordPress站点专属配置(适配LNMP架构,基于Unix Socket连接PHP-FPM)
server {
    # 监听80端口(HTTP默认端口),接收所有网卡的HTTP请求
    listen 80;
    # 设置网站根目录:Nginx查找网页文件的基础路径
    root /usr/share/nginx/html;
    # 设置服务器匹配域名:本地测试用localhost,生产环境可改为实际域名
    server_name localhost;
    
    # 字符集配置(如需启用可去掉#,指定网页字符编码)
    #charset koi8-r;
    # 访问日志路径(如需记录站点访问日志可去掉#)
    #access_log /var/log/nginx/log/wordpress.access.log main;

    # ========== 超时与上传优化配置 ==========
    # 允许客户端最大上传文件大小(解决WordPress上传主题/插件过大问题)
    client_max_body_size 64M;
    # FastCGI读取超时时间(延长PHP脚本执行时长,适配长任务)
    fastcgi_read_timeout 300;
    # FastCGI发送超时时间(Nginx向PHP-FPM传输请求的超时阈值)
    fastcgi_send_timeout 300;
    # 客户端请求体读取超时时间(防止慢速客户端占用连接)
    client_body_timeout 300;
    # 客户端请求头读取超时时间(提升连接复用效率)
    client_header_timeout 300;

    # ========== 根路径请求处理 ==========
    location / {
        # 定义默认首页优先级:优先执行PHP脚本,再加载静态页面
        index index.php index.html index.htm;
        # 支持WordPress固定链接(伪静态),若无需求可注释
        try_files $uri $uri/ /index.php?$args;
    }

    # ========== 错误页面配置 ==========
    # 自定义404页面(如需启用可去掉#,并创建对应文件)
    #error_page 404 /404.html;
    # 配置5xx服务器错误跳转页面
    error_page 500 502 503 504 /50x.html;
    # 精确匹配50x错误页面请求
    location = /50x.html {
        # 错误页面文件所在目录(与网站根目录一致)
        root /usr/share/nginx/html;
    }

    # ========== PHP脚本解析配置 ==========
    # 正则匹配所有.php结尾的请求(~区分大小写,适配PHP文件)
    location ~ .php$ {
        # 转发到PHP-FPM的Unix Socket(通过upstream php-fpm定义)
        fastcgi_pass php-fpm;
        # 设置PHP默认索引文件
        fastcgi_index index.php;
        # 传递PHP脚本真实路径给PHP-FPM(核心参数,避免文件找不到)
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # 引入FastCGI通用参数(包含请求方法、HTTP头信息等)
        include fastcgi_params;
    }

    # ========== 静态资源优化(可选) ==========
    # 对图片、CSS、JS等静态文件设置缓存,减轻服务器压力
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;  # 缓存有效期30天
        add_header Cache-Control "public, max-age=2592000";
    }
}
(2)重新加载 nginx 配置

检查nginx配置文件语法是否正确(包含主配置及所有子配置文件),提前发现语法错误避免服务异常

[root@wordpress ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx配置热加载(平滑重载):不中断现有服务的情况下,使新配置生效(旧连接不中断,新请求用新配置)

[root@wordpress ~]# nginx -s reload
(3)测试 nginx 解析 php
[root@wordpress ~]# vim /usr/share/nginx/html/info.php
<?php
/**
 * 测试PHP解析是否正常
 * 访问方式:http://你的服务器IP/info.php
 */
phpinfo();  // 输出PHP环境配置、模块信息等,确认PHP解析生效
?>

浏览器访问:http://你的服务器IP/info.php

image-20251203221242874

(4)测试 mysql 数据库连接
[root@wordpress ~]# vim /usr/share/nginx/html/mysql_connection.php
<?php
/**
 * MySQL连接测试
 * 访问方式:http://你的服务器IP/mysql_connection.php
 */
$db_host = 'localhost';
$db_user = 'wordpress';
$db_pass = '123456';
$db_name = 'wordpress';

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if (!$conn) {
    $status = "❌ 连接失败";
    $message = "错误: " . mysqli_connect_error();
} else {
    $status = "✅ 连接成功";
    $message = "数据库: <strong>{$db_name}</strong><br>版本: " . mysqli_get_server_info($conn);
    mysqli_close($conn);
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MySQL连接测试结果</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', 'PingFang SC', sans-serif;
            background: linear-gradient(135deg, #1e3a8a 0%, #0c4a6e 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            text-align: center;
            padding: 20px;
        }
        .container {
            background: rgba(0, 0, 0, 0.6);
            border-radius: 25px;
            padding: 45px 55px;
            max-width: 90%;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .status {
            font-size: 48px;
            margin-bottom: 25px;
            text-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
            animation: pulse 2s infinite;
        }
        .message {
            font-size: 22px;
            line-height: 1.6;
            margin-bottom: 35px;
            font-weight: 500;
            color: #e0f2fe;
        }
        .version {
            font-size: 18px;
            color: #a5b4fc;
            margin-top: 20px;
            font-style: italic;
        }
        .note {
            margin-top: 40px;
            font-size: 15px;
            color: #cbd5e0;
            font-style: italic;
            border-top: 1px dashed rgba(255, 255, 255, 0.2);
            padding-top: 20px;
        }
        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.4); }
            70% { box-shadow: 0 0 0 15px rgba(255, 255, 255, 0); }
            100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="status"><?= $status ?></div>
        <div class="message"><?= $message ?></div>
        <div class="version">—— 由MySQL连接测试工具生成</div>
        <div class="note">💡 提示:测试完成后请立即删除此文件!生产环境切勿暴露密码</div>
    </div>
</body>
</html>

浏览器访问:http://你的服务器IP/mysql_connection.php

image-20251203223350591

5. 部署 WordPress 至 Nginx 根目录

(1)下载源代码并解压
[root@wordpress ~]# wget https://cn.wordpress.org/wordpress-latest-zh_CN.zip
[root@wordpress ~]# unzip wordpress-latest-zh_CN.zip
(2)配置 wordpress

复制源码至nginx

[root@wordpress ~]# cp -rvf wordpress/* /usr/share/nginx/html/

将 WordPress 默认提供的配置示例文件复制并重命名为 WordPress 运行必需的正式配置文件

[root@wordpress ~]# cp /usr/share/nginx/html/wp-config-sample.php /usr/share/nginx/html/wp-config.php

编辑正式配置文件

[root@wordpress ~]# vim /usr/share/nginx/html/wp-config.php
...
// ** MySQL 设置 - 具体信息来自您的主机服务提供商 ** //
/** WordPress数据库的名称 */
define( 'DB_NAME', 'wordpress' ); // 填写之前创建的数据库名

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

/** MySQL数据库密码 */
define( 'DB_PASSWORD', '你的数据库密码' ); // 填写用户对应的密码

/** MySQL主机 */
define( 'DB_HOST', 'localhost' ); // 数据库主机(本地填localhost即可)

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

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

设置权限

[root@wordpress ~]# chmod -R 777 /usr/share/nginx/html/
(3)浏览器安装 wordpress

image-20251203224929981

(4)登录 wordpress

用户名/密码:admin/000000

image-20251203225011179

(5)美化网站

使用提供的WordPress项目包和数据库文件上传到服务器并替换源文件

[root@wordpress ~]# tar -zxvf wordpress-dist.tar.gz
[root@wordpress ~]# cp -rvf dist/* /usr/share/nginx/html/
[root@wordpress ~]# chmod -R 777 /usr/share/nginx/html/

导入数据库文件

[root@wordpress ~]# mysql -uroot -p wordpress < dist/wordpress.sql

浏览器访问

image-20251203232937072

posted @ 2026-04-26 03:05  陌殇殇殇  阅读(21)  评论(0)    收藏  举报