wordpress/Rocky9.0部署Wordpress.md
2025-06-19 09:53:53 +08:00

383 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">WordPress</font><font style="color:rgb(51, 51, 51);"> 是由 </font><font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">PHP</font><font style="color:rgb(51, 51, 51);"> 语言开发的一款开源博客平台。运行在典型的</font><font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">LNMP/LAMP</font><font style="color:rgb(51, 51, 51);"> 环境中。</font>
# <font style="color:rgb(51, 51, 51);">安装MySQL数据库</font>
| **主机名称** | **IP地址** | **操作系统** | **硬件最低配置** |
| --- | --- | --- | --- |
| wordpress-db | 192.168.0.50 | Rocky9.0 | 1核心CPU/1G内存/20G磁盘 |
添加MySQL8.0仓库
```shell
tee /etc/yum.repos.d/mysql-8.0.repo <<EOF
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=https://repo.mysql.com/yum/mysql-8.0-community/el/9/x86_64/
enabled=1
gpgcheck=0
EOF
```
安装MySQL8.0版本
```shell
dnf install -y mysql-community-server-8.0.30-1.el9.x86_64
```
启动MySQL
```shell
systemctl enable mysqld --now
```
过滤MySQL ROOT初始密码
```shell
grep "password" /var/log/mysqld.log
```
使用初始密码登录数据库,格式: `mysql -u 用户名 -p密码` -p与密码之间不能有空格其他参数可有可无密码如果有特殊字符需要用引起来
```shell
mysql -u root -p"9VZ<oQcwi=qL"
```
需要重新修改root密码
```shell
mysql> alter user root@"localhost" identified by "Admin123...";
```
退出数据库,并使用新密码登录
```shell
mysql -uroot -p'Admin123...'
```
<font style="color:rgb(51, 51, 51);">创建数据库远程访问账号</font>
```shell
CREATE USER 'wordpress'@'%' IDENTIFIED BY 'wp123...A';
GRANT ALL PRIVILEGES ON *.* TO 'wordpress'@'%';
FLUSH PRIVILEGES;
```
创建 wordpress 库用于项目存储数据
```shell
create database wordpress;
```
# <font style="color:rgb(51, 51, 51);">安装Nginx WEB服务</font>
| **主机名称** | **IP地址** | **操作系统** | **硬件最低配置** |
| --- | --- | --- | --- |
| wordpress | 192.168.0.51 | Rocky 9.0 | 1核心CPU/1G内存/20G磁盘 |
<font style="color:rgb(51, 51, 51);">准备Nginx稳定版仓库仓库地址</font>[https://nginx.org/en/linux_packages.html#RHEL](https://nginx.org/en/linux_packages.html#RHEL)
```shell
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
```
安装Nginx与PHP软件包
```shell
dnf install nginx-1.20.2 php php-fpm php-mysqlnd php-gd freetype-devel libjpeg-turbo-devel -y
```
<details class="lake-collapse"><summary id="ue10c6b96"><strong><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 24px">Nginx常用相关文件</span></strong></summary><p id="u67d3967e" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">/etc/nginx/nginx.conf //Nginx主配置文件</span></p><p id="u9e59934c" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">/usr/share/nginx/html/ //Nginx网页目录</span></p><p id="ud90cb43a" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">/var/log/nginx/error.log //Nginx错误日志文件</span></p><p id="u962499d9" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">/var/log/nginx/access.log //Nginx访问日志文件</span></p><p id="udf251277" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">/etc/nginx/conf.d/ //虚拟web主机目录</span></p></details>
<details class="lake-collapse"><summary id="u95c78b7a"><strong><span class="ne-text" style="font-size: 24px">PHP软件包介绍</span></strong></summary><p id="u270e465b" class="ne-p"><span class="ne-text" style="font-size: 16px">php </span><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">//是php代码的解释器</span></p><p id="u96418c5d" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">php-fpm //管理php进程接收请求</span></p><p id="uf3ed27f2" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">php-mysqlnd //PHP连接MySQL的扩展连接MySQL数据库并进行增删改查</span></p><p id="uc23a693e" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">php-gd //php处理图片扩展如生成图片、裁剪图片、缩放图片等</span></p><p id="u349839b8" class="ne-p"><span class="ne-text" style="color: rgb(51, 51, 51); font-size: 16px">freetype-devellibjpeg-turbo-devel //处理字体和图片的软件包</span></p></details>
<font style="color:rgb(51, 51, 51);">修改配置文 </font><font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">/etc/php-fpm.d/www.conf</font><font style="color:rgb(51, 51, 51);"> 指定PHP运行的用户与组并开启PHP状态页面用于监控PHP状态。</font>
```shell
#指定PHP程序运行时的用户和组为Nginx
user = nginx
group = nginx
```
<font style="color:rgb(51, 51, 51);">修改配置文件</font>`<font style="color:rgb(51, 51, 51);">/etc/php.ini</font>`<font style="color:rgb(51, 51, 51);">指定php时区</font>
```shell
#取消注释修改PHP时区为亚洲/上海
date.timezone ="Asia/shanghai"
```
# 准备WordPress网站配置文件
删除Nginx默认的网站虚拟web主机配置文件
```shell
rm -rf /etc/nginx/conf.d/default.conf
```
创建wordpress网站配置文件`/etc/nginx/conf.d/wordpress.conf`
```shell
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param DOCUMENT_ROOT /usr/share/nginx/html;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_param PATH_TRANSLATED /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
```
启动Nginx与PHP
```shell
systemctl enable nginx php-fpm --now
```
# 测试Nginx与PHP连接
在Nginx网页目录创建文件测试Nginx与PHP之间的连接
```php
<?php
phpinfo();
?>
```
访问页面测试:<font style="color:#117CEE;">http://192.168.0.51/phpinfo.php</font>
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724161776089-680264ea-7f3b-4a60-89f8-21db091c2b99.png) |
| --- |
# 测试PHP与MySQL连接
在Nginx网页目录创建文件测试PHP与MySQL之间的连接
```shell
<?php
$host = '192.168.0.50'; // 替换为你的数据库主机地址
$user = 'wordpress'; // 替换为你的数据库用户名
$password = 'wp123...A'; // 替换为你的数据库密码
$database = 'wordpress'; // 替换为你的数据库名
// 创建连接
$conn = new mysqli($host, $user, $password, $database);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
$conn->close();
?>
```
访问页面测试:<font style="color:#117CEE;">http://192.168.0.51/mysql.php</font>
# 部署WordPress项目
在wordpress主机下载WordPress项目
```shell
wget https://cn.wordpress.org/wordpress-6.0-zh_CN.tar.gz
```
解压WordPress压缩包
```shell
tar -xf wordpress-6.0-zh_CN.tar.gz
```
删除/usr/share/nginx/html/目录下所有文件
```shell
rm -rf /usr/share/nginx/html/*
```
将wordpres目录下所有项目文件移动到/usr/share/nginx/html/目录
```shell
mv wordpress/* /usr/share/nginx/html/
```
切换到/usr/share/nginx/html/目录
```shell
cd /usr/share/nginx/html/
```
准备wordpress连接数据库配置文件
```shell
cp wp-config-sample.php wp-config.php
```
修改wp-config.php文件内容指定数据库相关信息
```shell
define( 'DB_NAME', 'wordpress' );
/** Database username */
define( 'DB_USER', 'wordpress' );
/** Database password */
define( 'DB_PASSWORD', 'wp123...A' );
/** Database hostname */
define( 'DB_HOST', '192.168.0.50' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
define( 'WP_CACHE', true );
//配置wordpress认证参数
define('AUTH_KEY', 'S2i6bE#^eKLwc|2$kn*#1;W,#?.7u~it_bC#lyd{qmddoQyndWqET6|G<{,l ]-a');
define('SECURE_AUTH_KEY', '59Un.*`*,Ojv-+<>aEGfl9wH#Ik,G,+Ab:f?X^a%)_kz{VI/Rq+TY5?rt3^q10Hm');
define('LOGGED_IN_KEY', '=`}q)k2-9p<eH^c+NbZ&*a5{|W$6JfUlu-t-H4PlgL&!JqG[0{!K{JtIw1aEb%L/');
define('NONCE_KEY', 'xb??HV#|H{F6fw@IqI2}KZ-Hf$QTcB0&,X<Y4zz|RkS=T#=`.Xr>Hqc1_^@&ye*z');
define('AUTH_SALT', 'YH.8P.h&oAF&H]-A71adaJ#~}i)Gh/dC:GEUckG%pEp{#)W&&IO$W,kA]sSS6&hZ');
define('SECURE_AUTH_SALT', 'wPd$jX3|Yq5:Tm!Bd/EGhRU2G_Srfn/R9iNHc/ao_[mpGklZbOmk2Sq+zr^>T3^l');
define('LOGGED_IN_SALT', 'PibdSiB$%.Zy%8B$^nU]7DEf_8Wb{}d7?GxFm)8B?g,1%}qa(lN9q$Pr#-s`1Qsf');
define('NONCE_SALT', 'OBW;+_cF$S?4s -lrTpC+zJ,,0:0;I>).>~(h_`]Ga?byYqLFP)4xD4LOjF-ySI{');
```
修改项目文件权限确保Nginx用户对所有项目文件具备完全权限
```shell
chown -R nginx.nginx .
```
# 访问WordPress页面
浏览器访问WordPress页面<font style="color:#117CEE;">http://192.168.0.51</font>
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724137225111-b83ff1ff-499a-4643-9558-941fc6a5e158.png) |
| --- |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724137246345-f249b7f8-3799-4840-a785-afc1f07d84cb.png) |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724137273164-998edbd0-a8a2-43d0-91ca-c6644e8c1bef.png) |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724137305114-ca3ebc1b-bf36-4290-8681-6257b1493935.png) |
# Redis为网站提供缓存
<font style="color:rgb(51, 51, 51);">通过Redis将网站访问频次高的数据存储在内存中来提高网站的性能。</font>
<font style="color:rgb(51, 51, 51);">在wp-db主机安装Redis数据库</font>
```shell
dnf install redis -y
```
<font style="color:rgb(51, 51, 51);">Redis默认只允许本地访问需要修改Redis配置文件允许远程访问</font>
```shell
#查找bind 127.0.0.1这一行修改为0.0.0.0允许远程连接。
bind 0.0.0.0
```
启动Redis
```shell
systemctl enable redis --now
```
在wordpress安装<font style="color:rgb(51, 51, 51);">PHP连接Redis扩展模块</font>
```shell
#安装epel仓库提供PHP连接Redis扩展模块
dnf install epel-release -y
#安装扩展模块
dnf install php-pecl-redis5 -y
```
重启PHP以识别新的扩展模块
```shell
systemctl restart php-fpm
```
查看PHP扩展模块
```shell
php -m | grep redis
```
<font style="color:rgb(51, 51, 51);">修改wp-config.php文件配置连接Redis</font>
```shell
//在连接MySQL下边添加连接Redis配置
//指定与Redis通信的客户端(php-pecl-redis)
define('WP_REDIS_CLIENT', 'pecl');
//连接数据库的协议
define('WP_REDIS_SCHEME', 'tcp');
//Redis主机地址
define('WP_REDIS_HOST', '192.168.0.50');
//使用的数据库编号默认0-15
define('WP_REDIS_DATABASE', '0');
//设置所有缓存键的前缀
define('WP_CACHE_KEY_SALT', 'wp_');
//缓存失效时间以秒为单位86400为1天
define('WP_REDIS_MAXTTL', '86400');
```
在WordPress中安装支持Redis插件
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724138048168-d98328b8-8dfc-4f64-b20d-d9ce839d45d7.png) |
| --- |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724138156653-a1a0d279-e3fc-410f-a93e-03bcfb5e43ee.png) |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724165294230-42e7104f-0bff-4ecd-a1f7-a4b063445df5.png) |
| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724165322131-92a3c770-e9b6-48c1-ae42-eb25012d68e2.png) |