一、nginx服务管理
1 nginx负载均衡器
https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
该页面是官方配置手册,后续配置参数均可在此查找。
1.1 给RS分配权重
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -s reload
[root@server25 ~]# curl www.westos.org

测试:server2出现的次数更多

1.2 备份服务器
此时将server22和server23的httpd服务停止,再测试,Nginx 反向代理服务器正常运行,但是无法连接后端真正提供网页服务的程序

将server26设置为备用节点,server22和server23的httpd停止时,自动访问server26
bash
[root@server26 ~]# echo server4 > /var/www/html/index.html
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -s reload
[root@server25 ~]# curl www.westos.org


如果将服务端server25设置为备用节点
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf #下图2.3是要将我们自定义的模块放在默认模块之后
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload
[root@server25 ~]# curl www.westos.org



此时测试,会直接返回server25的nginx主页面

再次开启server23和server23的httpd服务,测试页面显示server2和server3

1.3 ip_hash
Nginx 对客户端的 IP 地址进行哈希计算,根据计算结果决定请求去往哪台服务器。
同一 IP 发出的所有请求,永远被分配到同一台后端服务器。
服务器列表变化(如新增或宕机)时,哈希结果会重新分配。
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf

测试,一直访问到server23,停掉server23,将会访问server22

也可从网站访问

浏览器地址栏输入 chrome://net-internals/#hsts,在 "Delete domain security policies" 里输入 www.westos.org,点 Delete,再重新访问
刷新页面,保持一台RS的首页面不变

1.4 Sticky cookie
sticky cookie 的作用是:在客户端浏览器中植入一个特定的 Cookie,使该客户端后续的所有请求都被 Nginx 精准地转发到同一台后端服务器上。
工作原理:
-
首次访问:客户端(浏览器)第一次访问 http://192.168.40.145。
-
植入 Cookie:Nginx 将请求转发给某一台后端(比如 server22),同时在返回的 HTTP 响应头里,自动给浏览器设置了一个名为 srv_id 的 Cookie,值是一个哈希字符串(代表 server22)。
-
后续访问:客户端再次发起请求时,浏览器自动携带这个 srv_id Cookie。Nginx 看到这个 Cookie,直接识别出它绑定的是 server22,于是绝不轮询,直接将请求又发回 server22。
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload
- 配置

在C盘的hosts文件中加入域名解析

- 测试
注意:如果仅使用curl www.westos.org指令,不保存第一次得到的 Cookie 值,后续请求就无法携带同一个 Cookie,也就无法验证"固定后端"的效果,每次都会重新分配,结果必然是轮询

也可在网页端测试,可看见分配的srv_id

2 nginx模块编译
- 静态编译(内置编译,--add-module)
编译 Nginx 时,直接把第三方模块源码打包进 Nginx 主程序二进制文件 ,最终生成单一nginx可执行文件。
优点 :部署简单,单文件即可运行;运行效率略高;兼容性稳定,无版本依赖问题,老版本 Nginx 兼容好,早期无动态模块功能只能静态编译。
缺点:新增 / 删除模块必须重新完整编译 Nginx;二进制文件体积大;升级模块必须整体升级 Nginx;模块全部常驻,无法按需加载卸载。 - 动态编译(动态模块,--add-dynamic-module)
编译时将第三方模块编译为独立.so动态库文件,Nginx 运行时通过load_module指令按需加载,主程序和模块文件分离。
优点 :模块独立,可单独新增、替换、删除;按需加载,轻量化;模块升级不影响 Nginx 主程序,单独替换.so文件重载服务。
缺点 :部署依赖配套 .so 文件;存在版本匹配强约束,动态模块必须和编译它的 Nginx 大版本完全一致,混用不同版本.so直接崩溃;
准备工作
bash
[root@server26 ~]# wget https://nginx.org/download/nginx-1.28.3.tar.gz
[root@server26 ~]# yum install -y git
[root@server26 ~]# git clone https://github.com/fabianofurtado/nginx_sticky_module_ng.git
#git clone后得到下图中内容,为实验方便,后续我将使用本地的压缩包

2.1 静态编译
bash
[root@server26 ~]# tar zxf nginx_sticky_module_ng-0.0.2.tar.gz
[root@server26 ~]# tar zxf nginx-1.28.3.tar.gz
[root@server26 ~]# cd nginx-1.28.3/
[root@server26 nginx-1.28.3]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_sticky_module_ng-0.0.2 #写入下图查询到的编译参数,再将解压缩后的包加入编译
[root@server26 nginx-1.28.3]# make && make install
#若程序已经装好,存在二进制文件,只需执行make
注意:静态编译的 nginx 加模块必须全量重编,要把之前所有 --add-module 一起带上,可查看之前编译的选项

bash
[root@server26 ~]# vim /opt/nginx/conf/nginx.conf
#编辑内容如下方2图
[root@server26 ~]# /opt/nginx/sbin/nginx -t
[root@server26 ~]# /opt/nginx/sbin/nginx -s reload
[root@server26 ~]# /opt/nginx/sbin/nginx


bash
[root@server26 ~]# vim /etc/hosts

2.2 动态编译
bash
[root@server26 ~]# cd nginx-1.28.3/
[root@server26 nginx-1.28.3]# make clean
[root@server26 nginx-1.28.3]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-compat --add-dynamic-module=../nginx_sticky_module_ng-0.0.2
[root@server26 nginx-1.28.3]# make
#make结束可以在objs/看到.so程序
[root@server26 objs]# /opt/nginx/sbin/nginx -s stop
[root@server26 objs]# cp -f nginx /opt/nginx/sbin/nginx
[root@server26 objs]# mkdir /opt/nginx/modules
[root@server26 objs]# cp ngx_http_sticky_module.so /opt/nginx/modules/
[root@server26 objs]# /opt/nginx/sbin/nginx -t




3 安全控制
3.1 限制并发连接
同一个客户端 IP,最多只能同时建立 1 条活跃下载连接
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf


共享内存区域 addr 分配的大小为 0,语法非法,配置校验失败
加上容量配置即可正常运行

bash
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload
bash
[root@server25 ~]# mkdir /usr/local/nginx/html/download
#拖拽一个大小约400k的文件vim.jpg进入
[root@server25 ~]# cd /usr/local/nginx/html/download
[root@server25 download]# ls
vim.jpg
测试
bash
[root@server26 ~]# ab -c 10 -n 10 http://192.168.40.145/download/vim.jpg

bash
[root@server25 ~]# tail -f /usr/local/nginx/logs/access.log
显示一次成功,九次失败

3.2 限制请求数
3.2.1 并发与速率限制
同一个客户端 IP,每秒最多允许发起 1 次 HTTP 请求
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试:


3.2.2 突发请求处理
同一个客户端 IP,每秒最多允许发起 1 次 HTTP 请求,最多缓存 5 个请求

测试:


3.3 限制速率
限制速率在100k
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试:
bash
[root@server26 ~]# ab -c 1 -n 5 http://192.168.40.145/download/vim.jpg

4 自动索引
当用户访问 http://你的域名/download/ 时:
如果该目录下 没有 index.html 等默认首页文件
Nginx 会自动生成一个网页,列出该目录下的所有文件和子文件夹
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试:

5 缓存配置
所有 jpg/png/gif 图片开启一年浏览器缓存
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试

6 禁用日志记录
匹配到这一类图片请求,Nginx 不记录访问日志
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

7 日志轮转
将昨日访问日志改名归档,通知 nginx 生成全新空白日志,全程不中断网站服务
bash
[root@server25 ~]# vim /opt/nginx_log.sh
#不中断服务的情况下按天归档访问日志
[root@server25 ~]# chmod +x /opt/nginx_log.sh
[root@server25 ~]# /opt/nginx_log.sh
[root@server25 ~]# cd /usr/local/nginx/logs/
[root@server25 logs]# ls #可以看到前一天的日志


结合crontab周期化调用,可以实现每天凌晨 00:00 归档前一天日志
bash
[root@server25 ~]# crontab -e

8 日志可视化
部署GoAccess,实现 Nginx 访问日志可视化分析
GoAccess 是一款开源、轻量的 Web 日志分析工具,专门解析 Nginx/Apache 访问日志,自动生成可视化统计报表。
bash
[root@server25 ~]# wget https://tar.goaccess.io/goaccess-1.8.tar.gz
[root@server25 ~]# tar zxf goaccess-1.8.tar.gz
[root@server25 ~]# cd goaccess-1.8/
[root@server25 goaccess-1.8]# yum install -y ncurses-devel GeoIP-devel
[root@server25 goaccess-1.8]# ./configure --enable-utf8 --enable-geoip=mmdb #出现如图一的error
[root@server25 goaccess-1.8]# yum install -y libmaxminddb-devel
[root@server25 goaccess-1.8]# ./configure --enable-utf8 --enable-geoip=mmdb #安装好后再执行,出现图二即为成功
[root@server25 goaccess-1.8]# make && make install


bash
[root@server25 goaccess-1.8]# goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &
#启动,在网站访问http://主机IP/report.html

刷新页面,各项数据都有变化

9 站点限制
只允许本机访问
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload


10 中文乱码
解决网页中文乱码问题
bash
[root@server25 html]# vim /usr/local/nginx/html/index.html
[root@server25 html]# nginx -t
[root@server25 html]# nginx -s reload


bash
[root@server25 html]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 html]# nginx -t
[root@server25 html]# nginx -s reload


11 虚拟主机
搭建 Nginx 域名型虚拟主机(基于域名的虚拟主机)
bash
[root@server25 ~]# cd /usr/local/nginx/
[root@server25 nginx]# mkdir /www1/
[root@server25 nginx]# echo web1 > /www1/index.html
[root@server25 nginx]# vim conf/nginx.conf
[root@server25 nginx]# nginx -t
[root@server25 nginx]# nginx -s reload

测试
bash
[root@server26 ~]# vim /etc/hosts
[root@server26 ~]# curl www1.westos.org


12 https配置
给 Nginx 网站 配置 HTTPS 加密访问,实现安全访问,传输数据加密,防止抓包窃听、篡改
bash
[root@server25 ~]# cd /etc/pki/tls/certs
[root@server25 certs]# make cert.pem

bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload


测试:
在虚拟主机部分将web1写入了/www1/index.html

13 重定向
13.1 80重定向到443
HTTP (80 端口) 请求永久重定向到 HTTPS (443 端口)
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试
-I 只看响应头,-k 跳过证书验证,默认 curl 不跟随跳转

13.2 www1.westos.org/bbs 重定向bbs.westos.org
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

测试

14 防盗链
防止其他网站直接引用你服务器上的图片(盗链),节省服务器带宽
bash
[root@server25 ~]# cd /usr/local/nginx/html/download/
[root@server25 download]# cp vim.jpg /www1
[root@server25 download]# cd /www1
[root@server25 www1]# echo www.westos.org > /www1/index.html
[root@server25 www1]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 www1]# nginx -t
[root@server25 www1]# nginx -s reload

bash
[root@server22 ~]# cd /var/www/html/
[root@server22 html]# vim index.html
[root@server22 html]# vim /etc/hosts


访问server22的IP

将daolian.jpg放在/usr/local/nginx/html/
bash
[root@server25 www1]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 www1]# nginx -t
[root@server25 www1]# nginx -s reload




而原网址一直不受影响

二、php部署
PHP(Hypertext Preprocessor,超文本预处理器)
一门服务端脚本语言,专门用来做网页后端。
运行在 Linux 服务器上,浏览器看不到 PHP 源代码。
1 php安装
bash
[root@server25 ~]# wget https://www.php.net/distributions/php-8.3.32.tar.gz
[root@server25 ~]# tar zxf php-8.3.32.tar.gz
[root@server25 ~]# cd php-8.3.32/
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib
#--prefix=/usr/local/php
指定 PHP 最终安装目录,所有二进制、库文件都会安装到此目录
#--with-config-file-path=/usr/local/php/etc
指定php.ini 配置文件默认搜索目录
编译的过程若遇到没有的-devel,可用yum list 库名-devel列出,若有的包与相关的-devel名字不同,出现 No matching Packages,先用 yum search 关键词 搜一下,即会出现,注意在排错过程中要留意版本要求,每次安装完一个包之后都需要重新执行.configure,直到出现安装成功的界面。
以下包含完整排错过程:


bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib


bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# yum install -y epel-release
[root@server25 php-8.3.32]# yum install -y oniguruma-devel
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

bash
[root@server25 php-8.3.32]# yum install -y readline-devel
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib
版本不够高

升级版本
bash
[root@server25 php-8.3.32]# wget https://libzip.org/download/libzip-1.9.2.tar.gz
[root@server25 php-8.3.32]# tar -xf libzip-1.9.2.tar.gz
[root@server25 php-8.3.32]# cd libzip-1.9.2
[root@server25 libzip-1.9.2]# mkdir build && cd build
[root@server25 build]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libzip -DBUILD_SHARED_LIBS=ON
-bash: cmake: command not found
[root@server25 build]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@server25 build]# ln -s /usr/bin/cmake3 /usr/bin/cmake
[root@server25 build]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libzip -DBUILD_SHARED_LIBS=ON
若直接执行此指令,cmake 默认把当前目录(build)当成源码目录,但 build 里没有 CMakeLists.txt,所以报错,需在其源码目录里执行。

bash
[root@server25 build]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/libzip -DBUILD_SHARED_LIBS=ON
#结果如下图
[root@server25 build]# make && make install
[root@server25 build]# echo "/usr/local/libzip/lib64" > /etc/ld.so.conf.d/libzip.conf
#把libzip库目录写入系统动态链接器配置
[root@server25 build]# ldconfig #刷新系统动态链接缓存
#echo:登记库目录,告诉系统去哪里找 libzip;
#ldconfig:刷新缓存,让登记立刻生效;
#最终目的:装好 PHP 之后,操作系统能顺利找到 libzip 库,PHP 正常运行不崩溃。
[root@server25 build]# export PKG_CONFIG_PATH=/usr/local/libzip/lib64/pkgconfig
#临时定义环境变量,让pkg-config找到libzip的pc配置文件
[root@server25 build]# pkg-config --modversion libzip
#验证pkg-config能否正常识别libzip版本


bash
[root@server25 build]# cd /root/php-8.3.32
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib

出现此页面说明 ./configure 成功
bash
[root@server25 php-8.3.32]# nproc #查询 CPU 核心数
2
[root@server25 php-8.3.32]# make -j2 #用全部 CPU 核心并行编译 PHP
#也可以直接执行make -j$(nproc)
Openssl 扩展编译失败,需要升级

注意:此时应将旧版的openssl卸载掉!
bash
[root@server25 php-8.3.32]# yum remove openssl-devel
由于我在此处忘记卸载,后面需要重新执行./configure
bash
[root@server25 php-8.3.32]# wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
[root@server25 php-8.3.32]# tar -xf openssl-1.1.1w.tar.gz
[root@server25 php-8.3.32]# cd openssl-1.1.1w
[root@server25 openssl-1.1.1w]# ./config shared zlib --prefix=/usr/local/openssl

bash
[root@server25 openssl-1.1.1w]# make -j2
[root@server25 openssl-1.1.1w]# make install
[root@server25 openssl-1.1.1w]# export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig:$PKG_CONFIG_PATH
[root@server25 openssl-1.1.1w]# echo "/usr/local/openssl/lib" > /etc/ld.so.conf.d/openssl.conf
[root@server25 openssl-1.1.1w]# ldconfig
[root@server25 openssl-1.1.1w]# pkg-config --modversion openssl

升级成功,返回php继续编译
bash
[root@server25 openssl-1.1.1w]# cd
[root@server25 ~]# cd php-8.3.32/
[root@server25 php-8.3.32]# make -j2
仍旧出现这个问题,我才意识到我的旧版openssl未卸载

bash
[root@server25 php-8.3.32]# yum remove openssl-devel #卸载,不会影响新版本
[root@server25 php-8.3.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib
编译成功

bash
[root@server25 php-8.3.32]# make -j2
[root@server25 php-8.3.32]# make install

至此,安装完成!
2 php配置
bash
[root@server25 php-8.3.32]# cp php.ini-production /usr/local/php/etc/
#复制生产环境php配置模板
[root@server25 php-8.3.32]# cd /usr/local/php/etc/
[root@server25 etc]# mv php.ini-production php.ini
[root@server25 etc]# vim php.ini
#如下图1,设置 PHP 默认时区为中国上海时区(UTC+8,北京时间)。如果不配置这一行,使用 date()、time()、DateTime 等时间函数时,PHP 持续抛出时区警告;网页日志、数据库时间展示偏差 8 小时。
[root@server25 etc]# cd ~/php-8.3.32/
[root@server25 php-8.3.32]# cd sapi/
[root@server25 sapi]# cd fpm/
[root@server25 fpm]# cp php-fpm.service /etc/systemd/system
# 复制systemd服务文件到系统服务目录
[root@server25 fpm]# cd /etc/systemd/system
[root@server25 system]# vim php-fpm.service
#如下图2,关闭 systemd 严格系统只读防护,防止源码编译环境出现读写权限故障


bash
[root@server25 system]# cd /usr/local/php/etc
[root@server25 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server25 etc]# vim php-fpm.conf
#如下图1
[root@server25 etc]# cd php-fpm.d/
[root@server25 php-fpm.d]# cp www.conf.default www.conf


查看php所在位置

bash
[root@server25 ~]# systemctl daemon-reload #重新加载 systemd 的服务单元配置文件
[root@server25 ~]# systemctl start php-fpm.service
[root@server25 ~]# netstat -antlp|grep :9000

bash
[root@server25 ~]# vim .bash_profile
[root@server25 ~]# source .bash_profile
[root@server25 ~]# which php

3 nginx与php整合
Nginx:Web 静态服务器、请求入口、代理
- 接收浏览器访问请求
- 直接处理静态文件:.html .jpg .css .js .png,速度极快
- 不能直接运行 PHP 代码,看不懂 PHP 脚本
PHP-FPM:PHP 进程管理器(PHP 运行环境)
- 专门解析、执行 PHP 动态代码
- 连接 MySQL 数据库、处理业务逻辑、输出网页内容
只用 Nginx:只能放静态网页,不能写登录、不能操作数据库,做不了交互网站
只用 PHP-FPM:没有入口,浏览器无法直接访问,缺少安全防护、负载均衡、静态加速
两者结合可以搭建动态网站,静态 html 内容固定不变,PHP 可以动态生成页面
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx -s reload

bash
[root@server25 ~]# vim /usr/local/nginx/html/index.php
#在 Nginx 网站根目录,创建测试 PHP 页面, phpinfo() 用于输出一份完整的 PHP 环境信息网页,包含:PHP 版本、已加载所有扩展(mysqli、zip、openssl、mbstring......)、php.ini 加载路径、时区、环境变量、编译参数、php-fpm 运行相关信息

访问IP/index.php

4 php动态扩展模块
memcache:分布式内存缓存数据库
数据存在内存里,读写速度远远高于磁盘数据库 MySQL。
PHP memcache 扩展让 PHP 代码可以调用函数,读写 memcached 缓存服务。
- 模块下载
memcache-8.2.tgz扩展源码包来源于 PECL 官方源码
作用:给 PHP 装上Memcache客户端类,让 PHP 代码能连接 memcached 服务端

下载好8.2版本后,拖入/root下
bash
[root@server25 ~]# tar zxf memcache-8.2.tgz
[root@server25 ~]# cd memcache-8.2/
[root@server25 memcache-8.2]# phpize
#phpize 是 PHP 自带的扩展构建脚本,给 PHP 扩展源码自动生成 configure、Makefile 编译环境

- 编译memcahe模块
bash
[root@server25 memcache-8.2]# yum install -y autoconf
[root@server25 memcache-8.2]# phpize

bash
[root@server25 memcache-8.2]# ./configure #自动生成适配本机的编译脚本
[root@server25 memcache-8.2]# make
[root@server25 memcache-8.2]# make insatll

- 添加memcache模块
bash
[root@server25 no-debug-non-zts-20230831]# php -m |grep memcache
#调用 PHP 命令行程序,过滤输出内容,只抓取包含 memcache 的行,输出 memcache代表memcache.so 扩展正常加载,安装成功,此处应该无输出
[root@server25 no-debug-non-zts-20230831]# vim /usr/local/php/etc/php.ini

bash
[root@server25 no-debug-non-zts-20230831]# systemctl reload php-fpm.service
[root@server25 no-debug-non-zts-20230831]# php -m |grep memcache

bash
[root@server25 no-debug-non-zts-20230831]# yum install -y memcached
[root@server25 no-debug-non-zts-20230831]# cat /etc/sysconfig/memcached #查看端口
[root@server25 no-debug-non-zts-20230831]# systemctl enable --now memcached.service
[root@server25 no-debug-non-zts-20230831]# netstat -antlp|grep :11211

bash
[root@server25 no-debug-non-zts-20230831]# cd
[root@server25 ~]# cd memcache-8.2/
[root@server25 memcache-8.2]# cp memcache.php example.php /usr/local/nginx/html/
[root@server25 memcache-8.2]# cd /usr/local/nginx/html/
[root@server25 html]# vim memcache.php

- 测试
example.php:一个通用示例文件,本身没有特殊功能,主要用来演示代码或作为开发起点。在此处用途:PHP 操作 Memcache 缓存的示范代码
memcache.php:一个专业的监控工具,是官方PECL扩展memcache自带的、用于实时监控Memcached服务器状态的图形化界面。
访问IP/example.php

PHP 脚本成功连接 Memcached,数据正常存入并成功取出,类型完整还原。
访问IP/memcache.php,查看缓存命中状态
输入在memcache.php文件中设置的用户名和密码即可查看

查看缓存命中状态

- 压力测试
example.php:执行过程中,PHP进程不会发起任何外部网络请求。它的系统调用(System Call)主要集中在文件读取和标准输出(write)上,整个过程没有任何阻塞点,内核CPU时间片用完就能立刻返回。
memcache.php:执行过程中,PHP进程会触发多次系统调用:
- 创建Socket:调用 socket() 或 connect()。
发送请求包:调用 sendto() 或 write(),将统计查询指令发给Memcached(默认端口11211)。 - 阻塞式等待:调用 recv() 或 read() 等待Memcached服务端返回数据。此时,这个PHP-FPM子进程会被操作系统挂起(阻塞),从"运行态"切换为"睡眠态",让出CPU。
- 唤醒与接收:直到Memcached服务器响应数据包到达网卡,产生网络中断,操作系统才会唤醒该PHP进程,继续往下执行。


可以看到执行example.php的速度更快
三、nginx高速缓存

1 方法一:原生nginx编译
将下图中三个模块拖入/root


bash
[root@server25 ~]# unzip memc-nginx-module-master.zip
[root@server25 ~]# unzip echo-nginx-module-master.zip
[root@server25 ~]# unzip srcache-nginx-module-master.zip
bash
[root@server25 ~]# cd nginx-1.30.4/
[root@server25 nginx-1.30.4]# nginx -s stop
[root@server25 nginx-1.30.4]# make clean
[root@server25 nginx-1.30.4]# nginx -V #获取之前编译时的参数
[root@server25 nginx-1.30.4]# ./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../memc-nginx-module-master --add-module=../echo-nginx-module-master --add-module=../srcache-nginx-module-master #将新加的三个模块编译进去

报错

由于我们已经升级了openssl,故只需再加参数,将升级后的openssl加入编译
bash
[root@server25 nginx-1.30.4]# ./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../memc-nginx-module-master --add-module=../echo-nginx-module-master --add-module=../srcache-nginx-module-master --with-cc-opt="-I/usr/local/openssl/include" --with-ld-opt="-L/usr/local/openssl/lib -Wl,-rpath=/usr/local/openssl/lib"
#编译成功界面如下图
[root@server25 nginx-1.30.4]# make
[root@server25 nginx-1.30.4]# cd objs/ #找到编译后的nginx,拷贝
[root@server25 objs]# du -h nginx
[root@server25 objs]# cp nginx /usr/local/nginx/sbin/

查看版本,升级成功

bash
[root@server25 objs]# cd /usr/local/nginx/conf/
[root@server25 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#语法通过,完全兼容
[root@server25 conf]# nginx #开启服务
nginx配置高效缓存
bash
[root@server25 conf]# vim nginx.conf
[root@server25 conf]# nginx -t
[root@server25 conf]# nginx -s reload


测试:访问index.php
因为缓存规则仅针对网站真实入口(index.php)生效,example.php 和 memcache.php 通常被配置绕过或无法匹配缓存策略。
只有压测 index.php 才能模拟真实流量,验证 Nginx 缓存是否命中(HIT),从而测出 Nginx 直接返回静态页面的极限性能,而非 PHP 的处理能力。
用时缩短

再用example.php测,速度加快
原因:PHP-FPM 进程预热
PHP 是进程模型:
- 第一轮压测 index.php
PHP-FPM 刚开始空闲,请求进来后:
创建工作进程、加载 PHP 扩展(memcached 扩展、文件编译、初始化资源);
这部分一次性开销,体现在第一次压测平均耗时偏高。 - 第二轮压测 example.php
PHP-FPM 进程已经启动、常驻内存;扩展、代码、连接资源全部就绪,不需要重复初始化。
哪怕访问不同 php 文件,进程已经预热完成,响应天然变快。

2 方法二:openresty部署
bash
[root@server25 ~]# nginx -s stop #停掉上一步部署的nginx,避免冲突
[root@server25 ~]# wget https://openresty.org/package/rhel/openresty.repo
#确认结果为下图1
[root@server25 ~]# yum install -y openresty
[root@server25 ~]# rpm -ql openresty #查询其安装路径在/usr/local/openresty
[root@server25 ~]# cd /usr/local/openresty/nginx/conf
[root@server25 conf]# cp /usr/local/nginx/conf/nginx.conf .
#此时检查语法,报错,因为此前进行了加密
[root@server25 conf]# cp /usr/local/nginx/conf/cert.pem . #将证书也拷过来,再次检查,无误
[root@server25 conf]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server25 conf]# /usr/local/openresty/nginx/sbin/nginx #启动


测试
浏览器访问server25的IP,出现以下页面即为部署成功

此处应将php的配置文件拷贝到/usr/local/openresty/nginx/html,如未拷贝,访问出现如下页面

原因就是 openresty 在自己的 html 目录里找不到php文件,故需要将php文件拷贝到openresty 的 html 目录

bash
[root@server25 conf]# cp /usr/local/nginx/html/*.php /usr/local/openresty/nginx/html/
[root@server25 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
此时再重新访问



四、session共享
Tomcat 是 Java 程序的运行容器,专门用来跑 Java 网页项目。
Session:服务端保存用户登录、身份信息;浏览器只保存 Cookie:JSESSIONID(Session 唯一编号)不存用户数据,session 存在服务端(Tomcat);
Memcached:独立内存数据库,存放所有用户 Session
工作流程:
- 用户第一次访问网站,请求经过 Nginx 转发到某一台 Tomcat;Tomcat 创建用户会话 Session,MSM 插件把这份会话数据放到 Memcached 缓存里,同时把会话编号 JSESSIONID 发给浏览器保存。
- 用户继续浏览页面,浏览器每次请求都会带上这个编号;Nginx 可能把请求分给另一台 Tomcat。
- 新的 Tomcat 收到请求后,依靠 MSM 插件拿着 JSESSIONID 去 Memcached 查找对应的会话信息,识别用户,保持登录状态。
Session 不再锁死在某一台 Tomcat 内存,统一放到公共缓存 Memcached,所有 Tomcat 共用这一份会话。JSESSIONID 就是查找会话的钥匙。
1 部署tomcat
bash
[root@server22 ~]# yum install -y java-1.8.0-openjdk.x86_64
[root@server22 ~]# tar zxf apache-tomcat-9.0.120.tar.gz -C /usr/local
[root@server22 ~]# cd /usr/local
[root@server22 local]# ln -s apache-tomcat-9.0.120/ tomcat
[root@server22 local]# cd /usr/local/tomcat/bin
[root@server22 bin]# ./startup.sh
[root@server22 bin]# netstat -antlp|grep :8080

server23同上

测试:


2 nginx整合tomcat
bash
[root@server25 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server25 ~]# nginx -t
[root@server25 ~]# nginx
[root@server25 ~]# nginx -s reload


server22
bash
[root@server22 ~]# cd /usr/local/tomcat/webapps/ROOT/
#将test.jsp放入
server23同上
访问http://192.168.40.145/test.jsp

提交一些信息

停掉server22
bash
[root@server22 ~]# /usr/local/tomcat/bin/shutdown.sh
自动切换到192.168.40.143,但有信息丢失

3 tomcat整合memcached
tomcat服务通过msm模块把session信息交叉存储到memcached上
bash
[root@server22 ~]# yum install -y memcached
[root@server22 ~]# systemctl enable --now memcached.service

msm文件放入server22的/root
bash
[root@server22 ~]# tar zxf msm.tgz
[root@server22 ~]# cd msm/
[root@server22 msm]# cp * /usr/local/tomcat/lib
[root@server22 msm]# cd /usr/local/tomcat/conf
[root@server22 conf]# vim context.xml

server23同上,配置文件里的n1改为n2

bash
[root@server22 conf]# /usr/local/tomcat/bin/shutdown.sh
[root@server22 conf]# /usr/local/tomcat/bin/startup.sh
[root@server22 conf]# cat /usr/local/tomcat/logs/catalina.out

server23

出现上页面即为成功
测试:
在192.168.40.142上添加信息

停掉server22
bash
[root@server22 ~]# /usr/local/tomcat/bin/shutdown.sh
刷新后应该切换到server23的,但出错了

排错:


发现server22的页面正常,server23的页面不正常,应当是 tomcat 运行不正常,原因是配置好context.xml后未重启,而server22由于本来就是停掉的,所以直接开启没问题,故我在测试前加上了重启这一操作。
此时再重新测试,当server22停掉的时候,自动切换到server23,且数据无丢失
