1. 完成nginx编译安装脚本
编写nginx源码编译安装脚本,此脚本在rocky8.5和ubuntu22.04测试正常
[root@ubuntu205 ~J$vim nginx_install.sh
#!/bin/bash
#
#******************************************************************************************************
#Author:zhaoming
#QQ:599031583
#Date:2024-08-24
#FileName:nginx_install.sh
#URL:http://www.zmlinux.cn
#Description:The test script
#Copyright (c):2024 All rights reserved
#******************************************************************************************************
. /etc/os-release
color_success='\E[1;32m'
color_fail='\E[1;31m'
color_end='\E[0m'
NGINX_VERSION=1.24.0
INSTALL_DIR=/apps/nginx
env_check(){
[ -e nginx-$NGINX_VERSION.tar.gz ] || wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
[ $? -ne 0 ] && { echo -e "$color_fail下载失败,请手动下载nginx-$NGINX_VERSION.tar.gz$color_end"; exit 2; }
tar xvf nginx-$NGINX_VERSION.tar.gz -C /usr/local/src
[ -e $INSTALL_DIR ] && { echo -e "$color_fail'nginx已存在'$color_end";exit; } || mkdir $INSTALL_DIR -p
}
install(){
echo -e "$color_success开始安装nginx$color_end"
if id nginx &> /dev/null;then
echo -e "$color_fail'nginx用户已存在'$color_end"
else
useradd -s /sbin/nologin -r nginx && echo -e "$color_success创建nginx用户成功$color_end"
fi
if [[ $ID == centos ]];then
[[ $VERSION_ID =~ ^7 ]] && yum -y install gcc make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
[[ $VERSION_ID =~ ^8 ]] && yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
elif [[ $ID == rocky ]];then
yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
elif [[ $ID == ubuntu ]];then
apt update && apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
else
echo -e "$color_fail不支持的操作系统$color_end"
fi
[ $? -ne 0 ] && { echo -e "$color_fail安装依赖包失败$color_end";exit; }
cd /usr/local/src/nginx-$NGINX_VERSION/ && ./configure --prefix=$INSTALL_DIR --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[ $? -eq 0 ] && make && make install && echo -e "$color_success编译成功$color_end"
[ $? -ne 0 ] && { echo -e "$color_fail编译失败,请检查日志错误,重新编译$color_end";exit 3; }
chown -R nginx.nginx $INSTALL_DIR
ln -s $INSTALL_DIR/sbin/nginx /usr/local/sbin/nginx
echo "PATH=$INSTALL_DIR/sbin:${PATH}" > /etc/profile.d/nginx.sh
cat > /lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=${INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${INSTALL_DIR}/sbin/nginx -t
ExecStart=${INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
systemctl is-active nginx &> /dev/null || { echo -e "$color_fail'nginx 启动失败,退出!'$color_end"; exit; }
echo -e "$color_success'nginx 安装完成'$color_end"
}
env_check
install
[root@ubuntu205 ~J$bash nginx_install.sh
image.png
[22:22:38 root@rocky8 ~]#bash nginx_install.sh
image.png
2. 完成nginx平滑升级,总结步骤
平滑升级四个阶段
- 只有旧版nginx的master和worker进程
- 旧版和新版nginx的master和worker进程并存,由旧版nginx接收处理用户的新请求
- 旧版和新版nginx的master和worker进程并存,由新版nginx接收处理用户的新请求
- 只有新版nginx的master和worker进程
范例:nginx平滑升级案例(1.24升级到1.26)
实验环境说明:
ubuntu205的nginx从1.24版本升级到1.26
ubuntu200作为客户端
rocky8模拟用户访问nginx数据,保持升级期间用户无中断
#nginx服务器创建10M文件
[root@ubuntu205 nginx-1.24.0]$dd if=/dev/zero of=/apps/nginx/html/test.img bs=1M count=10
#nginx客户端限速下载
[08:45:04 root@rocky8 ~]#wget --limit-rate=1K http://10.0.0.205/test.img
image.png
#新版本下载到服务器中
[root@ubuntu205 nginx-1.24.0]$cd ..
[root@ubuntu205 src]$wget http://nginx.org/download/nginx-1.26.2.tar.gz
#解压缩
[root@ubuntu205 src]$tar xf nginx-1.26.2.tar.gz
[root@ubuntu205 src]$cd nginx-1.26.2/
#查看老版本的编译选项
[root@ubuntu205 nginx-1.26.2]$nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#新版本编译原来选项,有新的需求可以增加
[root@ubuntu205 nginx-1.26.2]$./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#编译
[root@ubuntu205 nginx-1.26.2]$make $CPUS
#查看
[root@ubuntu205 nginx-1.26.2]$ll objs/
[root@ubuntu205 nginx-1.26.2]$objs/nginx -v
nginx version: nginx/1.26.2
#老版本备份
[root@ubuntu205 nginx-1.26.2]$cp /apps/nginx/sbin/nginx /opt/nginx.old -a
#新版拷贝到原老版本路径
[root@ubuntu205 nginx-1.26.2]$cp -f objs/nginx /apps/nginx/sbin/nginx
#语法检查
[root@ubuntu205 nginx-1.26.2]$/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
#启动新版本,新老并存
[root@ubuntu205 nginx-1.26.2]$cat /apps/nginx/logs/nginx.pid
824
[root@ubuntu205 nginx-1.26.2]$kill -USR2 `cat /apps/nginx/logs/nginx.pid`
#查看日志路径
[root@ubuntu205 nginx-1.26.2]$ll /apps/nginx/logs/
total 24
drwxr-xr-x 2 nginx nginx 4096 Aug 25 08:58 ./
drwxr-xr-x 11 nginx nginx 4096 Aug 25 08:32 ../
-rw-r--r-- 1 root root 172 Aug 25 08:58 access.log
-rw-r--r-- 1 root root 71 Aug 25 08:58 error.log
-rw-r--r-- 1 root root 5 Aug 25 08:58 nginx.pid
-rw-r--r-- 1 root root 4 Aug 25 08:32 nginx.pid.oldbin
[root@ubuntu205 nginx-1.26.2]$cat /apps/nginx/logs/nginx.pid.oldbin
824
[root@ubuntu205 nginx-1.26.2]$cat /apps/nginx/logs/nginx.pid
4719
#新旧并存,新版本现在属于老版本的子进程,有新请求,仍由旧版本提供服务,此时做快照,方便模拟回滚旧版本
[root@ubuntu205 nginx-1.26.2]$ps auxf|grep nginx
root 4741 0.0 0.1 4024 1976 pts/0 S+ 09:00 0:00 \_ grep --color=auto nginx
root 824 0.0 0.0 10180 944 ? Ss 08:32 0:00 nginx: master process /apps/nginx/sbin/nginx
nginx 826 0.0 0.2 10916 4268 ? S 08:32 0:00 \_ nginx: worker process
root 4719 0.0 0.3 10188 6388 ? S 08:58 0:00 \_ nginx: master process /apps/nginx/sbin/nginx
nginx 4720 0.0 0.1 10924 3488 ? S 08:58 0:00 \_ nginx: worker process
客户端测试访问,此时仍由老版本提供服务
image.png
#优雅地关闭老版本进程(如果此时有新请求,由新版本提供服务)
[root@ubuntu205 nginx-1.26.2]$kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
客户端测试访问,此时新版本提供服务
image.png
用户端全程未中断
image.png
查看进程
image.png
老版本退出,升级完成
[root@ubuntu205 nginx-1.26.2]$kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
模拟用户完成下载,查看进程,此时老版本进程完全消失,新版本提供服务
image.png
image.png
3. 总结nginx核心配置,并实现nginx多虚拟主机
nginx核心配置
主配置文件结构:四部分
#事件驱动相关配置
event{
}
#http/https 协议相关配置段
http{
}
默认配置文件不包括下面两个块
#mail协议相关配置段
mail{
}
#stream 服务器相关配置段
stream{
}
范例:
events {
worker_connections 65536; #设置单个工作进程的最大并发连接数,默认512,生产建议根据性
能修改更大的值
use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如:select、poll、epoll,只
能设置在events模块中设置。
accept_mutex on; #mutex互斥为on表示同一时刻一个请求轮流由worker进程处理,而防止被同时唤
醒所有worker,避免多个睡眠进程被唤醒的设置,可以避免多个 worker 进程竞争同一连接而导致性能下降,
也可以提高系统的稳定性,默认为off,新请求会唤醒所有worker进程,此过程也称为"惊群",在高并发的场
景下多个worker进程可以各自同时接受多个新的连接请求,如果是多CPU和worker进程绑定,就可以提高吞吐
量
multi_accept on; #on时Nginx服务器的每个工作进程可以同时接受多个新的网络连接,此指令默认
为off,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。建议设置为on
}
http {
include mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录
default_type application/octet-stream; #除mime.types中文件类型外,设置其它文件默认
类型,访问其它类型时会提示下载不匹配的类型文件
#日志配置部分
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#自定义优化参数
sendfile on;
#tcp_nopush on; #开启sendfile的情况下,合并请求后统一发送给客户端,必须开启
sendfile
#tcp_nodelay off; #开启keepalived模式下的连接是否启用TCP_NODELAY选项,为off时,
延迟0.2s发送,默认On时,不延迟发送,立即发送用户响应报文。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间,第二个值为响应首部:keep�Alived:timeout=65,可以和第一个值不同
#gzip on; #开启文件压缩
server {
listen 80 default_server; #设置监听地址和端口,多个虚拟机时当前是否是默认的
虚拟主机,default_server表示是默认主机,否则排在前面server为默认主机
server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,
如:*.wang.com www.wang.* ~^www\d+\.wang\.com$ 示例: .wang.org 相当于
*.wang.org和wang.org
#charset koi8-r; #设置编码格式,默认是俄语格式,建议改为utf-8
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #定义错误页面
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以http的方式转发php请求到指定web服务器
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #以fastcgi的方式转发php请求到php处理
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来
改变自己的重定向等功能。
# deny all;
#}
location ~ /passwd.html {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server { #自定义虚拟server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm; #指定默认网页文件,此指令由
ngx_http_index_module模块提供
# }
#}
# HTTPS server
#
#server { #https服务器配置
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
实战案例多虚拟主机:京东页面手机和电脑分开
[root@ubuntu200 ~J$mkdir -p /data/nginx/html/{pc,mobile}
#电脑打开下面两个网站,另存为改名index.html,分别存在不同文件夹,拷贝到linux的pc和mobile
www.jd.com
m.jd.com
[root@ubuntu200 ~J$mkdir /apps/nginx/conf/conf.d
[root@ubuntu200 ~J$vim /apps/nginx/conf/nginx.conf
include conf.d/*.conf; 放在最后一个}前面,只要在http模块里就行
}
[root@ubuntu200 ~J$cd /apps/nginx/conf/conf.d/
[root@ubuntu200 conf.dJ$touch www.wang.org.conf
[root@ubuntu200 conf.dJ$touch m.wang.org.conf
[root@ubuntu200 conf.dJ$vim /apps/nginx/conf/conf.d/www.wang.org.conf
server {
server_name www.wang.org;
root /data/nginx/html/pc;
}
[root@ubuntu200 conf.dJ$cat /apps/nginx/conf/conf.d/www.wang.org.conf > /apps/nginx/conf/conf.d/m.wang.org.conf
[root@ubuntu200 conf.dJ$vim /apps/nginx/conf/conf.d/m.wang.org.conf
server {
server_name m.wang.org;
root /data/nginx/html/mobile;
}
[root@ubuntu200 conf.dJ$nginx -s reload
[root@ubuntu200 conf.dJ$nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
#测试,改host文件
root@ubuntu201:~# vim /etc/hosts
10.0.0.200 www.wang.org m.wang.org
root@ubuntu201:~# curl www.wang.org -v
root@ubuntu201:~# curl m.wang.org -v
通过host头部信息判断是www.wang.org还是m.wang.org
root@ubuntu201:~# curl -H"host: www.wang.org" 10.0.0.200/test.html
4. 总结nginx日志格式定制
Nginx 自定义访问日志
http://nginx.org/en/docs/http/ngx_http_log_module.html
语法格式
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time]
[if=condition]];
access_log off; #关闭访问日志,比如在反向代理上可以关闭日志功能
Default:
access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
日志格式的常见变量
$remote_addr # 记录客户端IP地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
$request # 记录请求的方法以及请求的http协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址
$request_length # 请求的长度(包括请求行,请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后,web服务器无法直接获取到客 户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址
自定义 json 格式日志
Nginx 的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中
通常将nginx日志转换为json日志,然后配合使用ELK做日志收集,统计和分析。(此处json暂时有问题格式,复制后要替换空格sed -i 's/\xc2\xa0//g' file)
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,' #总的处理时间
'"upstreamtime":"$upstream_response_time",' #后端应用服务器处理时间
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json;
#重启Nginx并访问测试日志格式,参考链接:http://json.cn/
{"@timestamp":"2019-02-
22T08:55:32+08:00","host":"10.0.0.8","clientip":"10.0.0.1","size":162,"responset
ime":0.000,"upstreamtime":"-","upstreamhost":"-
","http_host":"www.wang.org","uri":"/favicon.ico","xff":"-","referer":"-
","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64;
rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}
5. 总结 nginx反向代理及https安全加密
反向代理配置参数
官方文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
#示例:
location /web {
index index.html;
proxy_pass http://10.0.0.18:8080; #8080后面无uri,即无 / 符号,需要将location后面
url 附加到proxy_pass指定的url后面,此行为类似于root
server {
listen 80;
server_name www.wang.org;
root /data/nginx/html/pc;
location /static {
proxy_pass http://10.0.0.200/;
}
}
200后有/就是直接访问10.0.0.200
200后无/就是访问10.0.0.200/static/index.html
访问http重定向到https
vhost_traffic_status_zone;
server {
listen 80;
server_name www.wang.org;
return https://www.wang.org; 写死不合理
return https://$server_name$request_uri; 这样没有写死合理
}
server {
listen 443 ssl;
server_name www.wang.org;
root /data/nginx/html/pc;
ssl_certificate /apps/nginx/conf/conf.d/ssl/www.wang.org.pem;
ssl_certificate_key /apps/nginx/conf/conf.d/ssl/www.wang.org.key;
}
把80和443写到一个server里的话 需要加if判断,要不死循环
6. 实验完成基于LNMP和Redis的phpmyadmin的会话保持,记录完整步骤
image.png
#安装和配置 MySQL
在 10.0.0.38 上执行下面操作
范例:Rocky系统
[21:33:36 root@rocky8-3 ~]#yum -y install mysql-server
[21:33:36 root@rocky8-3 ~]#systemctl enable --now mysqld
[21:33:36 root@rocky8-3 ~]#mysql
mysql> create user admin@'10.0.0.%' identified with mysql_native_password by '123456';
mysql> grant all on *.* to admin@'10.0.0.%';
#安装和配置 Redis
在10.0.0.48上准备Redis服务
范例:Rocky系统
#redis安装
[21:35:03 root@rocky8-4 ~]#yum -y install redis
[21:35:03 root@rocky8-4 ~]#vim /etc/redis.conf
bind 0.0.0.0
[21:35:03 root@rocky8-4 ~]#systemctl enable --now redis
#安装和配置 Nginx
在10.0.0.18和10.0.0.28上执行下面,配置Nginx环境(下面针对10.0.0.18做的范例,28与18相同)
#创建用户和组
[21:28:21 root@rocky8-1 ~]#groupadd -g 666 -r www
[21:28:21 root@rocky8-1 ~]#useradd -u 666 -g www -s /sbin/nologin -r www
#编译安装nginx
[21:27:58 root@rocky8-1 ~]#cat nginx_install.sh
#!/bin/bash
#
#******************************************************************************************************
#Author:zhaoming
#QQ:599031583
#Date:2024-08-24
#FileName:nginx_install.sh
#URL:http://www.zmlinux.cn
#Description:The test script
#Copyright (c):2024 All rights reserved
#******************************************************************************************************
. /etc/os-release
color_success='\E[1;32m'
color_fail='\E[1;31m'
color_end='\E[0m'
NGINX_VERSION=1.24.0
INSTALL_DIR=/apps/nginx
env_check(){
[ -e nginx-$NGINX_VERSION.tar.gz ] || wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
[ $? -ne 0 ] && { echo -e "$color_fail下载失败,请手动下载nginx-$NGINX_VERSION.tar.gz$color_end"; exit 2; }
tar xvf nginx-$NGINX_VERSION.tar.gz -C /usr/local/src
[ -e $INSTALL_DIR ] && { echo -e "$color_fail'nginx已存在'$color_end";exit; } || mkdir $INSTALL_DIR -p
}
install(){
echo -e "$color_success开始安装nginx$color_end"
if id nginx &> /dev/null;then
echo -e "$color_fail'nginx用户已存在'$color_end"
else
useradd -s /sbin/nologin -r nginx && echo -e "$color_success创建nginx用户成功$color_end"
fi
if [[ $ID == centos ]];then
[[ $VERSION_ID =~ ^7 ]] && yum -y install gcc make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
[[ $VERSION_ID =~ ^8 ]] && yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
elif [[ $ID == rocky ]];then
yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
elif [[ $ID == ubuntu ]];then
apt update && apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
else
echo -e "$color_fail不支持的操作系统$color_end"
fi
[ $? -ne 0 ] && { echo -e "$color_fail安装依赖包失败$color_end";exit; }
cd /usr/local/src/nginx-$NGINX_VERSION/ && ./configure --prefix=$INSTALL_DIR --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[ $? -eq 0 ] && make && make install && echo -e "$color_success编译成功$color_end"
[ $? -ne 0 ] && { echo -e "$color_fail编译失败,请检查日志错误,重新编译$color_end";exit 3; }
chown -R nginx.nginx $INSTALL_DIR
ln -s $INSTALL_DIR/sbin/nginx /usr/local/sbin/nginx
echo "PATH=$INSTALL_DIR/sbin:${PATH}" > /etc/profile.d/nginx.sh
cat > /lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=${INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${INSTALL_DIR}/sbin/nginx -t
ExecStart=${INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
systemctl is-active nginx &> /dev/null || { echo -e "$color_fail'nginx 启动失败,退出!'$color_end"; exit; }
echo -e "$color_success'nginx 安装完成'$color_end"
}
env_check
install
[21:28:02 root@rocky8-1 ~]#bash nginx_install.sh
#配置nginx支持php
[21:28:21 root@rocky8-1 ~]#mkdir /apps/nginx/conf.d
[21:29:22 root@rocky8-1 ~]#vim /apps/nginx/conf/nginx.conf
user www;
http {
....
include /apps/nginx/conf.d/*.conf
}
[21:30:03 root@rocky8-1 ~]#vim /apps/nginx/conf.d/www.wang.org.conf
[21:30:33 root@rocky8-1 ~]#mkdir /data/www
[21:30:37 root@rocky8-1 ~]#chown -R www.www /apps/nginx
[21:30:51 root@rocky8-1 ~]#systemctl enable --now nginx
#安装和配置 PHP-FPM服务使用Redis保存会话信息
在10.0.0.18和10.0.0.28上执行下面,配置PHP-FPM服务
[21:44:57 root@rocky8-1 ~]#yum -y install php-fpm php-json php-mysqlnd php-mbstring
#配置PHP-FPM服务
[21:46:24 root@rocky8-1 ~]#vim /etc/php-fpm.d/www.conf
user = www
group = www
listen = 127.0.0.1:9000
pm.status_path = /php-status
ping.path = /ping
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://10.0.0.38:6379"
[root@rocky8 ~]# systemctl restart php-fpm
在10.0.0.18和10.0.0.28上执行下面操作,编译Redis模块
#安装相关包
[21:47:24 root@rocky8-1 ~]#yum -y install php-cli php-devel
#编译PHP的Redis模块
[21:51:21 root@rocky8-1 ~]#wget https://pecl.php.net/get/redis-5.3.7.tgz
[21:52:14 root@rocky8-1 ~]#tar xf redis-5.3.7.tgz
[21:52:35 root@rocky8-1 ~]#cd redis-5.3.7/
[21:52:55 root@rocky8-1 redis-5.3.7]#phpize
[21:53:28 root@rocky8-1 redis-5.3.7]#./configure
[21:54:27 root@rocky8-1 redis-5.3.7]#make -j 2 && make install
[21:55:04 root@rocky8-1 redis-5.3.7]#ll /usr/lib64/php/modules/redis.so
-rwxr-xr-x 1 root root 3530840 Aug 25 21:55 /usr/lib64/php/modules/redis.so
[21:55:27 root@rocky8-1 redis-5.3.7]#vim /etc/php.d/66-redis.ini
extension=redis
[21:56:08 root@rocky8-1 redis-5.3.7]#systemctl enable --now php-fpm
#部署 phpMyAdmin 代码上线
官方链接https://www.phpmyadmin.net/
在10.0.0.18和10.0.0.28上执行下面操作,部署PhpMyAdmin代码
[21:56:55 root@rocky8-1 redis-5.3.7]#wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
[21:58:57 root@rocky8-1 redis-5.3.7]#unzip phpMyAdmin-5.2.0-all-languages.zip -d /opt/
[21:58:57 root@rocky8-1 redis-5.3.7]#mv /opt/phpMyAdmin-5.2.0-all-languages/* /data/www
[22:00:33 root@rocky8-1 redis-5.3.7]#cp /data/www/config.sample.inc.php /data/www/config.inc.php
[22:00:59 root@rocky8-1 redis-5.3.7]#vim /data/www/config.inc.php
$cfg['Servers'][$i]['host'] = '10.0.0.38';
[root@rocky8 ~]#chown -R www.www /data/www/
#配置Nginx反向代理实现负载均衡
在10.0.0.8的主机执行,实现负载均衡
#编译安装安装nginx服务
略
#申请证书文件
略
[root@rocky8 ~]#ls /apps/nginx/ssl/
www.wang.org.key www.wang.org.pem
#反向代理的配置
[root@rocky8 ~]#vim /apps/nginx/conf/nginx.conf
user www;
http {
....
include /apps/nginx/conf.d/*.conf
}
[root@rocky8 ~]#mkdir /apps/nginx/conf.d/
[root@rocky8 ~]#vim /apps/nginx/conf.d/proxy-www.wang.org.conf
upstream webservers {
server 10.0.0.18:80 ;
server 10.0.0.28:80;
}
server {
listen 80;
server_name www.wang.org;
root /data/www/pc;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name www.wang.org;
ssl_certificate /apps/nginx/ssl/www.wang.org.pem;
ssl_certificate_key /apps/nginx/ssl/www.wang.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
proxy_pass http://webservers;
proxy_set_header Host $http_host;
}
}
[root@rocky8 ~]#systemctl enable --nginx
[root@rocky8 ~]#systemctl restart nginx
验证访问
配置解析 /etc/hosts
10.0.0.8 www.wang.org
浏览器访问
http://www.wang.org
刷新页面验证