From 257425a1d02fe3aba4f44879e5136d52aced6686 Mon Sep 17 00:00:00 2001 From: yaml <17622739710@163.com> Date: Thu, 19 Jun 2025 09:53:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rocky9.0部署Wordpress.md | 382 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 Rocky9.0部署Wordpress.md diff --git a/Rocky9.0部署Wordpress.md b/Rocky9.0部署Wordpress.md new file mode 100644 index 0000000..85a0ae7 --- /dev/null +++ b/Rocky9.0部署Wordpress.md @@ -0,0 +1,382 @@ +WordPress 是由 PHP 语言开发的一款开源博客平台。运行在典型的LNMP/LAMP 环境中。 + +# 安装MySQL数据库 +| **主机名称** | **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 < alter user root@"localhost" identified by "Admin123..."; +``` + +退出数据库,并使用新密码登录 + +```shell +mysql -uroot -p'Admin123...' +``` + +创建数据库远程访问账号 + +```shell +CREATE USER 'wordpress'@'%' IDENTIFIED BY 'wp123...A'; +GRANT ALL PRIVILEGES ON *.* TO 'wordpress'@'%'; +FLUSH PRIVILEGES; +``` + +创建 wordpress 库用于项目存储数据 + +```shell +create database wordpress; +``` + +# 安装Nginx WEB服务 +| **主机名称** | **IP地址** | **操作系统** | **硬件最低配置** | +| --- | --- | --- | --- | +| wordpress | 192.168.0.51 | Rocky 9.0 | 1核心CPU/1G内存/20G磁盘 | + + + + +准备Nginx稳定版仓库,仓库地址:[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 +``` + +
Nginx常用相关文件:

/etc/nginx/nginx.conf //Nginx主配置文件

/usr/share/nginx/html/ //Nginx网页目录

/var/log/nginx/error.log //Nginx错误日志文件

/var/log/nginx/access.log //Nginx访问日志文件

/etc/nginx/conf.d/ //虚拟web主机目录

+
PHP软件包介绍:

php //是php代码的解释器

php-fpm //管理php进程接收请求

php-mysqlnd //PHP连接MySQL的扩展,连接MySQL数据库并进行增删改查

php-gd //php处理图片扩展,如生成图片、裁剪图片、缩放图片等

freetype-devel,libjpeg-turbo-devel //处理字体和图片的软件包

+修改配置文 /etc/php-fpm.d/www.conf 指定PHP运行的用户与组,并开启PHP状态页面用于监控PHP状态。 + +```shell +#指定PHP程序运行时的用户和组为Nginx +user = nginx +group = nginx +``` + +修改配置文件`/etc/php.ini`指定php时区 + +```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 + +``` + + + +访问页面测试:http://192.168.0.51/phpinfo.php + +| ![](https://cdn.nlark.com/yuque/0/2024/png/44499768/1724161776089-680264ea-7f3b-4a60-89f8-21db091c2b99.png) | +| --- | + + +# 测试PHP与MySQL连接 +在Nginx网页目录创建文件,测试PHP与MySQL之间的连接 + +```shell +connect_error) { + die("连接失败: " . $conn->connect_error); +} +echo "连接成功"; +$conn->close(); +?> +``` + + + +访问页面测试:http://192.168.0.51/mysql.php + +# 部署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-9pHqc1_^@&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页面:http://192.168.0.51 + +| ![](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为网站提供缓存 +通过Redis将网站访问频次高的数据存储在内存中,来提高网站的性能。 + +在wp-db主机安装Redis数据库 + +```shell +dnf install redis -y +``` + +Redis默认只允许本地访问,需要修改Redis配置文件允许远程访问 + +```shell +#查找bind 127.0.0.1这一行修改为0.0.0.0允许远程连接。 +bind 0.0.0.0 +``` + +启动Redis + +```shell +systemctl enable redis --now +``` + +在wordpress安装PHP连接Redis扩展模块 + +```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 +``` + +修改wp-config.php文件,配置连接Redis + +```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) | + + + + + + + +