Linux Web服务器与WordPress部署笔记
web服务器
nginx
配置基本认证
用户名和密码使用plain text发送,所以最好配置SSL/TLS。
# 安装工具[root@server ~ 09:21:43]# yum -y install httpd-tools[root@server ~ 09:28:30]# vim /etc/nginx/conf.d/ssl.confserver {location /auth-basic/ {auth_basic "Basic Auth";auth_basic_user_file "/etc/nginx/.htpasswd";}}[root@server ~ 09:31:16]# systemctl restart nginx[root@server ~ 09:31:17]# htpasswd -c /etc/nginx/.htpasswd yuxbNew password: Re-type new password: Adding password for user yuxb[root@server ~ 09:31:59]# mkdir /usr/share/nginx/html/auth-basic[root@server ~ 09:32:05]# vim /usr/share/nginx/html/auth-basic/index.html<html><body><div style="width: 100%; font-size: 40px; font-weight: bold; text-align: yuxber;">Test Page for Basic Authentication</div></body></html># 测试[root@client ~ 10:56:26]# curl -k http://www.yuxb.cloud/auth-basic/<html><body><div style="width: 100%; font-size: 40px; font-weight: bold; text-align: yuxber;">Test Page for Basic Authentication</div></body></html>
支持动态脚本
使用 PHP
# 安装PHP和php-fpm,建议把其他的扩展包一起安装[root@server ~ 09:33:46]# yum install -y php php-fpm[root@server ~ 09:33:52]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt# 查看 php 版本[root@server ~ 09:34:07]# php -vPHP 5.4.16 (cli) (built: Apr 1 2020 04:07:17) Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies# 先写入然后再测试 php 是否正常[root@server ~ 10:10:22]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > php_test.php[root@server ~ 10:14:37]# php php_test.phpPHP Test Page# 启动服务[root@server ~ 10:17:00]# systemctl enable php-fpm.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.# 准备测试页,使用phpinfo查看详细信息[root@server ~ 10:14:41]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php# 修改配置文件[root@server ~ 10:24:53]# vim /etc/nginx/nginx.conflocation ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}# 测试[root@client ~ 10:56:25]# curl http://www.yuxb.cloud/info.php<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">body {background-color: #ffffff; color: #000000;}body, td, th, h1, h2 {font-family: sans-serif;}pre {margin: 0px; font-family: monospace;}a:link {color: #000099; text-decoration: none; background-color: #ffffff;}a:hover {text-decoration: underline;}table {border-collapse: collapse;}.center {text-align: center;}......
Tomcat
Apache Tomcat 是一个开源的 Java Servlet 容器和 Web 服务器,常用于部署 Java Web 应用。它实现了 Java EE 中的 Servlet、JSP 等规范。
主要特点
-
Servlet 容器:支持运行基于 Servlet 技术的 Java Web 应用。
-
支持 JSP:可以编译和运行 JSP 页面。
-
轻量级:相比完整的 Java EE 服务器(如 JBoss、WebLogic),Tomcat 更轻便,适合中小型 Web 项目。
-
开源免费:由 Apache 软件基金会维护。
-
配置灵活:通过 XML 配置文件(如
server.xml
、context.xml
)进行管理。
Tomcat 部署
# 安装[root@server ~ 11:10:04]# yum install -y tomcat# 验证版本[root@server ~ 11:10:32]# java -versionopenjdk version "1.8.0_412"OpenJDK Runtime Environment (build 1.8.0_412-b08)OpenJDK 64-Bit Server VM (build 25.412-b08, mixed mode)# 启动tomcat[root@server ~ 11:13:56]# systemctl enable --now tomcat.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.# 准备测试页面[root@server ~ 11:14:07]# cd /var/lib/tomcat/webapps/[root@server webapps 11:14:21]# mkdir test[root@server webapps 11:14:25]# vim test/index.jsp<html><head><title>第一个 JSP 程序</title></head><body><%out.println("Hello World!");%></body></html># 测试[root@server ~ 11:15:24]# curl http://www.yuxb.cloud:8080/test/index.jsp<html><head><title>第一个 JSP 程序</title></head><body>Hello World!</body></html>
项目实战:Ngnix+Tomcat动静分离
动静分离就是:
-
Nginx 负责静态资源(动静分离中的“静”)
-
Tomcat 负责动态请求(动静分离中的“动”)
这样能减轻 Tomcat 压力,提高整体响应速度和并发能力。
# server端部分在上面# 安装软件[root@client ~ 11:00:39]# yum install -y nginx# 文件配置[root@client ~ 11:28:19]# vim /etc/nginx/nginx.confhttp {......upstream tomcat {server www.yuxb.cloud:8080;}......server {......proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;location /tomcat/ {proxy_pass http://tomcat/;}}} # 重启服务[root@client ~ 11:32:58]# systemctl restart nginx.service# 测试前提操作[root@client ~ 11:33:02]# echo hello world > /usr/share/nginx/html/index.html[root@client ~ 11:33:27]# mkdir /usr/share/nginx/html/web1[root@client ~ 11:33:30]# echo hello WEB1 > /usr/share/nginx/html/web1/index.html# 测试客户端网页浏览http://www.yuxb.cloud访问静态首页 index.htmlhttp://www.yuxb.cloud/web1访问静态目录下的 web1/index.htmlhttp://www.yuxb.cloud/tomcat/test/index.jsp通过代理,访问 Tomcat 上的 JSP 动态页面
LAMP/LNMP 最佳实践
ALL-IN-ONE
将操作系统、Web服务器、数据库、脚本语言等全部集成到一台服务器中,就像盖一栋小房子
典型架构示意图
┌───────────────────────────────┐│ 用户浏览器 │└───────────────┬───────────────┘│ HTTP 请求(80端口)▼┌───────────────────────────────┐│ 单台服务器 ││ (IP: 10.1.8.10) ││ ┌───────────────┬─────────────┐ ││ │ Nginx餐厅 │ MySQL厨房 │ ││ │ (Web服务器) │ (数据库服务) │ ││ └──────┬────────┴─────────────┘ ││ │ ││ │ PHP厨师(php-fpm) ││ │ 动态请求处理 ││ ▼ ││ ┌───────────────────────────┐││ │ PHP脚本处理动态请求 │││ └───────────────────────────┘│└───────────────────────────────┘
解释:
-
Nginx餐厅(Web服务器) 负责接收浏览器请求,处理静态文件(菜单、图片、JS、CSS)和转发PHP请求给厨师(php-fpm)。
-
PHP厨师(php-fpm) 负责执行PHP脚本(做菜),处理动态请求,必要时访问数据库拿食材。
-
MySQL厨房(数据库服务) 负责存储和管理数据(食材),供PHP厨师调用。
运行流程比喻:
-
客人(浏览器)来到餐厅(Nginx),点菜(访问网页)。
-
餐厅对静态资源直接响应,动态请求交给厨师(php-fpm)做菜。
-
厨师去厨房(MySQL)取食材,做好菜(生成网页内容)。
-
厨师端上菜,餐厅把菜送给客人(浏览器显示页面)。
部署 Mysql 服务(搭建厨房)
部署软件
# 安装服务端# 安装厨房设备[root@server ~ 14:01:09]# yum install -y mariadb-server# 启动服务# 启动厨房[root@server ~ 14:01:50]# systemctl enable mariadb --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.# 加固 mariadb# 给厨房加锁,确保厨房只有你和信任的人能进,防止外人乱动。[root@server ~ 14:02:16]# mysql_secure_installation
准备数据库
准备厨房的储藏室(创建数据库、用户)
专门留一个储藏室放“食材”(wordpress 数据),还有给“厨师”(wordpress 用户)分配钥匙和权限,保证厨师可以随时取用储藏室的食材。
[root@server ~ 14:06:59]# mysql -uroot -p123MariaDB [(none)]> create database wordpress;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> create user wordpress@'%' identified by '123';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> grant all privileges on wordpress.* to wordpress@'%';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> exitBye
部署 Web 服务(搭建餐厅大厅)
部署 Nginx
# 安装餐厅大厅[root@server ~ 14:10:28]# yum install -y nginx# 启动餐厅大厅[root@server ~ 14:10:48]# systemctl enable nginx.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.# 放置欢迎牌(创建 index.html)[root@server ~ 14:10:58]# echo hello world from nginx hhhhh > /usr/share/nginx/html/index.html[root@server ~ 14:11:22]# curl http://10.1.8.10hello world from nginx hhhhh
部署 PHP 服务
部署 php 服务
# 安装厨师# 安装厨师(PHP)[root@server ~ 14:13:41]# yum install -y php php-fpm php-mysqlnd[root@server ~ 14:14:04]# systemctl enable php-fpm.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
配置文件
# 配置厨师的工作间(Nginx 配置 PHP 处理):告诉餐厅大厅,碰到“点菜单”(PHP 文件)要交给厨师去做。[root@server ~ 14:15:05]# vim /etc/nginx/conf.d/php.conflocation ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}[root@server ~ 14:22:59]# mv /etc/nginx/conf.d/php.conf /etc/nginx/default.d/[root@server ~ 14:23:15]# systemctl restart nginx
准备 php 测试文件
# 准备 index.php[root@server ~ 14:24:28]# cd /usr/share/nginx/html/[root@server html 14:26:52]# cat > index.php <<EOF> <?php> echo "<h1>Hello World !</h1>\n";> ?>> EOF# 准备 test-mysql.php[root@server html 14:27:24]# cat > test-mysql.php <<'EOF'> <?php> $link=mysqli_connect('10.1.8.20','wordpress','123');> if($link)> echo "<h1>Connect Mysql Success !</h1>\n";> else> echo "<h1>Connect Mysql Failed !</h1>\n";> $link->close();> ?>> EOF# 准备 info.php[root@server html 14:30:28]# cat > info.php <<EOF> <?php> phpinfo()> ?>> EOF[root@server html 14:32:46]# cp *.php /usr/share/nginx/htmlcp: "index.php" 与"/usr/share/nginx/html/index.php" 为同一文件cp: "info.php" 与"/usr/share/nginx/html/info.php" 为同一文件cp: "test-mysql.php" 与"/usr/share/nginx/html/test-mysql.php" 为同一文件
php 程序测试
# 测试厨师(写 PHP 测试文件):先让厨师做个简单的菜,确认厨师能正常工作,也能从厨房(数据库)拿食材。[root@server html 14:33:20]# php -f index.php <h1>Hello World !</h1>[root@server html 14:45:16]# php -f test-mysql.php <h1>Connect Mysql Success !</h1>[root@server html 14:33:28]# php -f test-mysql.php PHP Warning: mysqli_connect(): (HY000/2002): No route to host in /usr/share/nginx/html/test-mysql.php on line 2<h1>Connect Mysql Failed !</h1>PHP Fatal error: Call to a member function close() on a non-object in /usr/share/nginx/html/test-mysql.php on line 7
部署 wordpress 应用(搭建菜单)
下载wordpress,上传到家目录。
下载菜单(WordPress 程序):把 WordPress 应用(菜单、菜谱)准备好。
# 解压[root@server ~ 15:02:34]# unzip -o wordpress-4.9.4-zh_CN.zip# 把菜单放到餐厅里(复制到 nginx 的目录):确保客人能看到菜单,点菜。# 给厨师权限(调整文件权限):让厨师能访问菜单里的菜谱,方便做菜。[root@server ~ 15:02:51]# cp -r wordpress /usr/share/nginx/html/[root@server ~ 15:03:32]# chown -R nginx:nginx /usr/share/nginx/html/wordpress/# 调整厨师身份(修改 PHP-FPM 配置):让厨师用餐厅指定的身份工作,保证安全和权限合理。[root@server ~ 15:08:36]# vim /etc/php-fpm.d/www.conf user = nginx; RPM: Keep a group allowed to write in log dir.group = nginx# 重新启动厨师(重启 PHP-FPM):让新配置生效。[root@server ~ 15:09:20]# systemctl restart php-fpm.service# 去浏览器验证# 打开餐厅网站(浏览器访问):客人打开网页,开始点菜、享受美食。http://10.1.8.10/wordpress/index.php
运行时的整个流程比喻:
-
客人(浏览器)来到餐厅(Nginx),想点一道菜(请求网页)。
-
餐厅把菜单交给厨师(PHP 处理器)。
-
厨师看菜单,去厨房(MySQL 数据库)取食材。
-
厨师做菜,端给客人(把网页内容返回给浏览器)。
Standalone
将各个组件(Web服务器、数据库、PHP运行环境)分别部署在不同的服务器上,通过网络进行通信。
以部署 wordpress 应用为例。
架构特点
-
组件分离:Web服务器、数据库服务器和应用服务器分开部署
-
网络通信:各节点通过局域网或专用网络互联
-
独立维护:每个服务器独立维护和扩展
-
高可用性:方便做负载均衡、数据库主从复制、备份容灾
典型架构示意
┌───────────────────────────────────┐│ 用户浏览器 │└───────────────┬───────────────────┘│ HTTP请求(80端口)▼┌───────────────────────────────────┐│ www.yuxb.cloud ││ (Apache Web服务器) │└───────────────┬───────────────────┘│静态资源(图片、HTML、JS、CSS) ││直接读取NFS共享文件系统挂载的目录│▼┌─────────────────────────────────────────────┐│ /var/www/html 挂载自 NFS存储 ││ (storage.yuxb.cloud:10.1.8.24) │└─────────────────────────────────────────────┘│动态请求转发(代理PHP请求)到│▼┌───────────────────────────────────┐│ php.yuxb.cloud ││ (PHP-FPM应用服务器) │└───────────────┬───────────────────┘│PHP代码执行,调用数据库查询和写入│▼┌───────────────────────────────────┐│ db.yuxb.cloud ││ (MariaDB数据库服务器) │└───────────────────────────────────┘
场景设定
你要开一个"WordPress 餐厅",让顾客(浏览器用户)来点菜(访问网站),后厨有不同的部门,各司其职:
-
www.yuxb.cloud(Web 服务员):负责迎客,把菜单(网页)递给顾客。
-
php.yuxb.cloud(后厨主厨):负责接到菜单后做菜(处理 PHP 脚本)。
-
db.yuxb.cloud(食材仓库):保存食谱和食材(数据库)。
-
storage.yuxb.cloud(公共储藏室):放各种厨房和餐厅都要用到的东西(WordPress 程序、静态文件)。
实验环境
主机名 | IP 地址 | 角色 |
---|---|---|
www.yuxb.cloud | 10.1.8.21 | apache |
php.yuxb.cloud | 10.1.8.22 | php |
db.yuxb.cloud | 10.1.8.23 | mariadb |
storage.yuxb.cloud | 10.1.8.24 | nfs |
简要说明:
-
www.yuxb.cloud (Apache) 负责接收所有客户端HTTP请求,处理静态资源,PHP请求通过反向代理转发到php服务器。静态文件目录挂载自NFS共享存储。
-
php.yuxb.cloud (PHP-FPM) 处理动态PHP脚本执行,连接数据库服务器进行数据操作。
-
db.yuxb.cloud (MariaDB) 提供数据库服务,支持远程访问仅允许php服务器IP。
-
storage.yuxb.cloud (NFS存储) 提供共享存储,挂载至www服务器以统一管理静态资源。
部署 NFS 服务器(公共储藏室)
由于静态文件和动态文件没有分开,所以 Web 服务器和 PHP 服务器都要存一份。实验环境通过NFS共享提供wordpress应用。
下载 wordpress,上传到家目录。
# 安装nfs工具[root@storage ~ 16:15:08]# yum install -y nfs-utils# 创建 /www 仓库目录,权限 777 让谁都可以进出。[root@storage ~ 16:17:18]# mkdir -m 777 /www[root@storage ~ 16:17:39]# echo '/www 10.1.8.0/24(rw)' > /etc/exports[root@storage ~ 16:17:39]# systemctl enable nfs-server.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.# 解压 WordPress 到仓库里 [root@storage ~ 16:17:41]# unzip -o wordpress-4.9.4-zh_CN.zip -d /www/# 放几个测试文件(index.html、index.php、info.php、test-mysql.php)[root@storage ~ 16:22:11]# echo 'Hello World !' > /www/index.html[root@storage ~ 16:22:18]# cat > /www/index.php <<EOF> <?php> echo "<h1>Hello World !</h1>\n";> ?>> EOF[root@storage ~ 16:22:30]# cat > /www/test-mysql.php <<'EOF'> <?php> $link=mysqli_connect('db.yuxb.cloud','wp','yuxb@123');> if($link)> echo "<h1>Connect Mysql Success !</h1>\n";> else> echo "<h1>Connect Mysql Failed !</h1>\n";> $link->close();> ?>> EOF[root@storage ~ 16:24:45]# cat > /www/info.php <<EOF> <?php> phpinfo();> ?>> EOF# vim /etc/hosts# 四台都要配置10.1.8.21 www.yuxb.cloud www10.1.8.22 php.yuxb.cloud php10.1.8.23 db.yuxb.cloud db10.1.8.24 storage.yuxb.cloud storage
部署 Mysql 服务器(食材仓库)
WordPress 需要菜谱(数据),所以要有一个数据库当作食材仓库。
# 安装 MariaDB 并启动 → 开仓。[root@db ~ 16:32:05]# yum install -y mariadb-server[root@db ~ 16:32:05]# systemctl enable mariadb --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.# 加固 mariadb# 给仓库上锁[root@db ~ 16:34:46]# mysql_secure_installation # 准备wordpress数据库和用户# 创建 wordpress 数据库和 wp 用户 → 给 WordPress 单独配一个仓库钥匙(用户名和密码)。[root@db ~ 16:34:46]# mysql -uroot -p123Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 10Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> CREATE DATABASE wordpress;Query OK, 1 row affected (0.00 sec)# 创建用户# '%'允许这个用户从任何主机连接到数据库。# IDENTIFIED BY 'yuxb@123'# 为新用户设置密码,这里是 yuxb@123。# 密码会被加密存储到 mysql.user 系统表中MariaDB [(none)]> CREATE USER wp@'%' identified by 'yuxb@123';Query OK, 0 rows affected (0.00 sec)# 权限MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO 'wp'@'%';Query OK, 0 rows affected (0.00 sec)# 让数据库立即重新加载权限表。MariaDB [(none)]> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> exitBye
部署 Web 服务器(服务员)
服务员的工作是接到顾客的点单,把静态的菜单直接递给顾客,如果菜单上有要做的菜(PHP 请求),就转交给厨师(PHP 服务器)。
部署 Nginx 服务
# 安装 Nginx 招来服务员。[root@www ~ 16:39:58]# yum install -y nginx[root@www ~ 16:39:58]# yum install -y nfs-utils[root@www ~ 16:40:23]# systemctl enable nginx --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.# 挂载存储# 挂载 NFS 仓库到 /usr/share/nginx/html 服务员能直接从仓库拿菜单。[root@www ~ 16:53:13]# echo 'storage.yuxb.cloud:/www /usr/share/nginx/html nfs defaults 0 0' >> /etc/fstab[root@www ~ 16:57:12]# mount /usr/share/nginx/html/mount.nfs: Operation already in progress# 检查挂载 确认服务员能看到菜单内容。[root@www ~ 16:57:35]# df -h /usr/share/nginx/html/文件系统 容量 已用 可用 已用% 挂载点storage.yuxb.cloud:/www 50G 1.7G 49G 4% /usr/share/nginx/html[root@www ~ 16:58:01]# ls /usr/share/nginx/html/index.html index.php info.php test-mysql.php wordpress
部署 PHP 服务器(主厨)
主厨负责做菜(执行 PHP 代码)。
部署 php 服务
# 安装 PHP 和 php-fpm 给主厨配好厨房工具。[root@php ~ 16:02:58]# yum install -y php php-fpm php-mysqlnd# 修改 www.conf 配置 让主厨听得到所有人的点单(listen = 9000,不限 IP)。[root@php ~ 16:41:31]# vim /etc/php-fpm.d/www.conf#使用;号注释掉原有listen行;listen = 127.0.0.1:9000# 新增listen 监听所有ip的9000端口listen = 9000# 支持监听特定ip的9000端口,例如listen = 10.1.8.22:9000# 使用;号注释掉原有 listen.allowed_clients 行# 允许所有客户端访问;listen.allowed_clients = 127.0.0.1
[root@php ~ 16:59:03]# systemctl enable php-fpm.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
挂载存储
# 挂载 NFS 到 /www 主厨能直接去仓库取做菜所需的材料(PHP 文件)。[root@php ~ 16:59:53]# yum install -y nfs-utils[root@php ~ 16:59:53]# echo 'storage.yuxb.cloud:/www /www nfs defaults 0 0' >> /etc/fstab[root@php ~ 17:00:15]# mkdir /www[root@php ~ 17:00:21]# mount /www[root@php ~ 17:00:37]# df -h /www文件系统 容量 已用 可用 已用% 挂载点storage.yuxb.cloud:/www 50G 1.7G 49G 4% /www[root@php ~ 17:00:40]# ls /wwwindex.html index.php info.php test-mysql.php wordpress
php 程序测试
# 测试 PHP 能正常运行 比如 php /www/index.php 看能不能端出菜来。[root@php ~ 17:00:46]# php /www/index.php<h1>Hello World !</h1>[root@php ~ 17:00:55]# php /www/test-mysql.php<h1>Connect Mysql Success !</h1>
配置 Web 对接 PHP(Web 服务员与主厨对接)
Nginx 对接 PHP
服务员和主厨要有一个传菜窗口。
配置 /etc/nginx/conf.d/vhost-www.conf:
-
静态文件(菜单图片、CSS 等)直接给顾客。
-
.php 请求送到 php.yuxb.cloud:9000 的主厨窗口去做菜。
-
指明
SCRIPT_FILENAME
(在 /www 找到菜谱)。
[root@www ~ 17:01:42]# cat > /etc/nginx/conf.d/vhost-www.conf <<'EOF'> server {> listen 80;> server_name www.yuxb.cloud;> > # 静态资源处理> location / {> root /usr/share/nginx/html;> index index.html index.htm index.php;> }> > # PHP 请求处理> location ~ \.php$ {> # 配置 PHP-FPM 监听的地址和端口> fastcgi_pass php.yuxb.cloud:9000;> fastcgi_index index.php;> # 配置 php 服务器上 wordpress 应用所在位置> fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;> include fastcgi_params;> }> }> EOF
# 重启 Nginx 让服务员按新规矩工作。[root@www ~ 17:02:30]# systemctl restart nginx
配置存储权限
# 更改仓库 /www 的所有者 UID 为 997 让主厨和服务员有相同的钥匙。[root@storage ~ 17:16:36]# chown -R 997 /www# 在 PHP 服务器上创建 nginx 用户(UID 997)保证权限一致,防止“开不了门”的尴尬。[root@php ~ 17:16:56]# useradd -u 997 -s /sbin/nologin nginx[root@php ~ 17:17:40]# vim /etc/php-fpm.d/www.conf user=nginxgroup=nginx# 重启 php-fpm 让主厨按照新身份继续做菜。[root@php ~ 17:19:04]# systemctl restart php-fpm
最终效果
-
顾客访问 www.yuxb.cloud → 服务员(Nginx)先看菜单:
-
静态菜单 → 直接给顾客。
-
要做的菜(PHP) → 送到 php.yuxb.cloud 主厨去做,主厨去仓库(NFS)拿食材,去数据库(MariaDB)取菜谱,做好后传给服务员,再送给顾客。
-
这样,一个分布式的 WordPress 餐厅就开业了!
http://10.1.8.21/wordpress:这个地址就是部署好的 WordPress 网站的入口,只不过是直接用 Web 服务器的 IP 地址 来访问,而不是用域名 www.yuxb.cloud。
http://www.yuxb.cloud/test-mysql.php :这个地址,其实是专门写的一个数据库连接测试脚本,用来验证 Web/PHP 环境能不能正常连接到你部署的 MariaDB 数据库。