1. 完成nginx编译安装脚本
#!/bin/bash
NGINX_VERSION=1.22.1
NGINX_FILE=nginx-${NGINX_VERSION}.tar.gz
NGINX_URL=http://nginx.org/download/
NGINX_INSTALL_DIR=/apps/nginx
SRC_DIR=/usr/local/src
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`
. /etc/os-release
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
check () {
[ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
cd ${SRC_DIR}
if [ -e ${NGINX_FILE}${TAR} ];then
color "相关文件已准备好" 0
else
color '开始下载 nginx 源码包' 0
wget ${NGINX_URL}${NGINX_FILE}${TAR}
[ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; }
fi
}
install () {
color "开始安装 nginx" 0
if id nginx &> /dev/null;then
color "nginx 用户已存在" 1
else
useradd -s /sbin/nologin -r nginx
color "创建 nginx 用户" 0
fi
color "开始安装 nginx 依赖包" 0
if [ $ID == "centos" ] ;then
if [[ $VERSION_ID =~ ^7 ]];then
yum -y install gcc make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
elif [[ $VERSION_ID =~ ^8 ]];then
yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
color '不支持此系统!' 1
exit
fi
elif [ $ID == "rocky" ];then
yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
apt update
apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
fi
[ $? -ne 0 ] && { color "安装依赖包失败" 1; exit; }
cd $SRC_DIR
tar xf ${NGINX_FILE}
NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
cd ${NGINX_DIR}
./configure --prefix=${NGINX_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
make -j $CPUS && make install
[ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; }
chown -R nginx.nginx ${NGINX_INSTALL_DIR}
ln -s ${NGINX_INSTALL_DIR}/sbin/nginx /usr/local/sbin/nginx
echo "PATH=${NGINX_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=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_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 || { color "nginx 启动失败,退出!" 1 ; exit; }
color "nginx 安装完成" 0
}
check
install
2. 完成nginx平滑升级,总结步骤
#下载最新稳定版
[root@ubuntu200406 ~]rz -E
[root@ubuntu200406 ~]ls
nginx-1.22.1 nginx-1.22.1.tar.gz nginx-1.24.0.tar.gz snap
[root@ubuntu200406 ~]tar xf nginx-1.24.0.tar.gz
[root@ubuntu200406 ~]ls
nginx-1.22.1 nginx-1.22.1.tar.gz nginx-1.24.0 nginx-1.24.0.tar.gz snap
[root@ubuntu200406 ~]cd nginx-1.24.0/
[root@ubuntu200406 nginx-1.24.0]ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#查看当前使用的版本及编译选项。结果如下:
[root@ubuntu200406 nginx-1.24.0]nginx -V
nginx version: nginx/1.22.1
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
built with OpenSSL 1.1.1f 31 Mar 2020
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
#configure arguments后面是以前编译时的参数。现在编译使用一样的参数
#开始编译新版本
[root@ubuntu200406 nginx-1.24.0]./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
#只执行make,不执行make install
[root@ubuntu200406 nginx-1.24.0]make
[root@ubuntu200406 nginx-1.24.0]ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@ubuntu200406 nginx-1.24.0]ls objs/
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@ubuntu200406 nginx-1.24.0]objs/nginx -v
nginx version: nginx/1.24.0
[root@ubuntu200406 nginx-1.24.0]/apps/nginx/sbin/nginx -v
nginx version: nginx/1.22.1
#把之前的旧版的nginx命令备份
[root@ubuntu200406 nginx-1.24.0]cp /apps/nginx/sbin/nginx /opt/nginx.old -a
[root@ubuntu200406 nginx-1.24.0]ls /opt/nginx.old
/opt/nginx.old
#把新版本的nginx命令复制过去覆盖旧版本程序文件,注意:需要加 -f 选项强制覆盖,否则会提示Text file busy
[root@ubuntu200406 nginx-1.24.0]cp -f objs/nginx /apps/nginx/sbin/nginx
[root@ubuntu200406 nginx-1.24.0]ls /apps/nginx/sbin/
nginx
#检测新版本和配置文件语法兼职容性
[root@ubuntu200406 nginx-1.24.0]/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
#发送信号USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,且这个新master进程会生成新的worker进程,即升级后的Nginx进 程,此时老的进程不会自动退出,新的请求仍由旧进程处理。
[root@ubuntu200406 nginx-1.24.0]kill -USR2 `cat /apps/nginx/logs/nginx.pid`
#可以看到两个master,新的master是旧版master的子进程,并生成新版的worker进程
#注意:在Nginx-1.22.1版中如果看不到下面新版进程,需要重新使用service方式重新启动nginx服务再发 送USR2信号
[root@ubuntu200406 nginx-1.24.0]ps aux | grep nginx
root 1790 0.0 0.0 8600 840 ? Ss 01:53 0:00 nginx: master process nginx
nginx 1791 0.0 0.1 9292 3424 ? S 01:53 0:00 nginx: worker process is shutting down
root 6868 0.0 0.0 8616 836 ? Ss 02:59 0:00 nginx: master process nginx
nginx 6869 0.0 0.1 9308 3460 ? S 02:59 0:00 nginx: worker process
root 7136 0.0 0.0 6300 720 pts/0 S+ 03:10 0:00 grep --color=auto nginx
#如果有新请求,仍由旧版本提供服务
[11:12:32 root@rocky88 ~]curl http://192.168.31.179 -I
HTTP/1.1 200 OK
Server: nginx/1.22.1
#先关闭旧nginx的worker进程,而不关闭旧nginx主进程方便回滚
#向原老的Nginx主进程发送WINCH信号,它会平滑关闭老的工作进程(主进程不退出),这时所有新请求都会 由新版Nginx处理
[root@ubuntu200406 nginx-1.24.0]kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#如果有新请求,由新版本提供服务
[11:10:20 root@rocky88 ~]curl http://192.168.31.179 -I
HTTP/1.1 200 OK
Server: nginx/1.24.0
#经过一段时间测试,新版本服务没问题,最后发送QUIT信号,退出老的master,完成全部升级过程
[root@ubuntu200406 nginx-1.24.0]kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
#查看版本是不是已经是新版了
[root@ubuntu200406 nginx-1.24.0]nginx -v
nginx version: nginx/1.24.0
[root@ubuntu200406 nginx-1.24.0]curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.24.0
#如果有旧的连接,不会立即关闭旧版本的Master和对应的Worker进程,直到所有旧连接断开,才会关闭所的旧的进程
[root@ubuntu200406 nginx-1.24.0]ps aux | grep nginx
root 1790 0.0 0.0 8600 840 ? Ss 01:53 0:00 nginx: master process nginx
nginx 1791 0.0 0.1 9292 3424 ? S 01:53 0:00 nginx: worker process is shutting down
root 6868 0.0 0.0 8616 836 ? Ss 02:59 0:00 nginx: master process nginx
nginx 6869 0.0 0.1 9308 3460 ? S 02:59 0:00 nginx: worker process
root 7136 0.0 0.0 6300 720 pts/0 S+ 02:40 0:00 grep --color=auto nginx
#如果升级的新版本发现问题需要回滚,可以发送HUP信号,重新拉起旧版本的worker
[root@ubuntu200406 nginx-1.24.0]kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
#最后关闭新版的master和worker,如果不执行上面的HUP信号,此步QUIT信号也可以重新拉起旧版本的 worker进程
[root@ubuntu200406 nginx-1.24.0]kill -QUIT `cat /apps/nginx/logs/nginx.pid`
步骤总结:
编译新版本,生成新版本的二进制文件
用新Nginx程序文件替换旧Nginx二进制文件(注意先备份旧版本的二进制文件)
向旧版本的master进程发送USR2信号启动新nginx进程
master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
将新生成的master进程的PID存放至新生成的pid文件nginx.pid
master进程用新Nginx二进制文件启动新master进程及worker子进程成为旧master的子进程
系统中将有新旧两个Nginx主进程和对应的worker子进程并存
当前新的请求仍然由旧Nginx的worker进程进行处理
向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止,旧的Nginx worker 进程将不再接收新请求
当前新的请求由新Nginx的worker进程进行处理
旧的Nginx Master进程仍然存在
测试访问确认新版本是否正常工作
如果发现升级正常,向旧master进程发送QUIT信号,关闭旧master,并删除Nginx.pid.oldbin文 件,到此旧版本的Nginx彻底下线,新版本正式上线
如果发现升级有问题,可以回滚∶向旧master发送HUP,旧版本的worker开始接收新请求,向新 master发送QUIT
3. 总结nginx核心配置,并实现nginx多虚拟主机
#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID 路径,日志路径等。
user nginx nginx;
worker_processes 1; #启动工作进程数数量
events { #events设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连 接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的 网络连接进行序列化等。
worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发,作为web服务器 的时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为 (worker_connections * worker_processes)/2
}
http { #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模 块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块, server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
include mime.types;
default_type application/octet-stream;
sendfile on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用 sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操 作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
keepalive_timeout 65; #长连接超时时间,单位是秒
server { #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个location模块。比如 本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供 web服务、
listen 80; #配置server监听的端口
server_name localhost; #本server的名称,当访问此名称的时候nginx会调用当前 serevr内部的配置进程匹配。
location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指 令,都是在location中体现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特 定的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的 配置也是在location模块中配置。
root html; #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使用绝对路 径配置。
index index.html index.htm; #默认的页面文件名称
}
error_page 500 502 503 504 /50x.html; #错误页面的文件名称
location = /50x.html { #location处理对应的不同错误码的页面定义到/50x.html,这个 跟对应其server中定义的目录下。
root html; #定义默认页面所在的目录
}
}
4. 总结nginx日志格式定制
自定义错误日志
Syntax: error_log file [level];
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location
level: debug, info, notice, warn, error, crit, alert, emerg
#关闭错误日志
error_log /dev/null;
自定义默认格式日志
#注意:此指令只支持http块,不支持server块
log_format access_log_format '$remote_addr - $remote_user [$time_local]
"$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
#注意:此指令一定要在放在log_format命令后
access_log logs/access.log access_log_format;
#重启nginx并访问测试日志格式
==> /apps/nginx/logs/access.log <==
10.0.0.1 - - [22/Feb/2019:08:44:14 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-
" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/2
0100101 Firefox/65.0" "-"www.wang.org:80
自定义 json 格式日志
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安全加密
代理Proxy有两种
正向代理:代理客户端访问服务器,可以实现缓存,科学上网,访问控制等功能
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给 用户的一种方式,这是用的比较多的一种方式。
四层(LVS)和七层(Nginx)反向代理的区别
监听端口
参与三次握手和四次挥手
异构协议
应用数据修改
后端是否能看到客户端真实地址
性能
端口修改
工作层:LVS四层,Nginx七层
监听端口:LVS不监听,Nginx监听端口
后端服务器看到客户端地址:LVS可以,Nginx不可以
连接(三次握手):LVS 不参与连接,只负责转发,Nginx代替后端服务器和客户建立连接
HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信 息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。
https实现过程如下:
1.客户端发起HTTPS请求:
客户端访问某个web端的https地址,一般都是443端口
2.服务端的配置:
采用https协议的服务器必须要有一套证书,可以通过一些组织申请,也可以自己制作,目前国内很多网站都 自己做的,当你访问一个网站的时候提示证书不可信任就表示证书是自己做的,证书就是一个公钥和私钥匙, 就像一把锁和钥匙,正常情况下只有你的钥匙可以打开你的锁,你可以把这个送给别人让他锁住一个箱子,里 面放满了钱或秘密,别人不知道里面放了什么而且别人也打不开,只有你的钥匙是可以打开的。
3.传送证书:
服务端给客户端传递证书,其实就是公钥,里面包含了很多信息,例如证书得到颁发机构、过期时间等等。
4.客户端解析证书:
这部分工作是有客户端完成的,首先回验证公钥的有效性,比如颁发机构、过期时间等等,如果发现异常则会 弹出一个警告框提示证书可能存在问题,如果证书没有问题就生成一个随机值,然后用证书对该随机值进行加 密,就像2步骤所说把随机值锁起来,不让别人看到。 5.传送4步骤的加密数据:
就是将用证书加密后的随机值传递给服务器,目的就是为了让服务器得到这个随机值,以后客户端和服务端的 通信就可以通过这个随机值进行加密解密了。
6.服务端解密信息:
服务端用私钥解密5步骤加密后的随机值之后,得到了客户端传过来的随机值(私钥),然后把内容通过该值进 行对称加密,对称加密就是将信息和私钥通过算法混合在一起,这样除非你知道私钥,不然是无法获取其内部 的内容,而正好客户端和服务端都知道这个私钥,所以只要机密算法够复杂就可以保证数据的安全性。
7.传输加密后的信息:
服务端将用私钥加密后的数据传递给客户端,在客户端可以被还原出原数据内容。
8.客户端解密信息:
客户端用之前生成的私钥获解密服务端传递过来的数据,由于数据一直是加密的,因此即使第三方获取到数据 也无法知道其详细内容。
6. 实验完成基于LNMP和Redis的phpmyadmin的会话保持,记录完整步骤
准备 MySQL 和 Redis
[root@ubuntu2004 ~]apt -y install mysql-server redis
#配置可不修改
[root@ubuntu2004 ~]vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
default_authentication_plugin=mysql_native_password
[root@ubuntu2004 ~]systemctl restart mysql
#创建用户并授权
[root@ubuntu2004 ~]mysql
mysql> create user admin@'localhost' identified with mysql_native_password by
'123456';
mysql> grant all on *.* to admin@'localhost';
编译安装 PHP-7.4 和 PHP-Redis 模块
[root@ubuntu2004 ~]apt -y install gcc make autoconf libpcre3 libpcre3-dev
openssl libssl-dev zlib1g-dev libxml2-dev pkg-config libsqlite3-dev libtool
[root@ubuntu2004 ~]groupadd -g 80 www && useradd -u 80 -g www www
#编译oniguruma
[root@ubuntu2004 ~]wget -O oniguruma-6.9.4.tar.gz
https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.4.tar.gz
[root@ubuntu2004 ~]tar xf oniguruma-6.9.4.tar.gz
[root@ubuntu2004 oniguruma-6.9.4]./autogen.sh
[root@ubuntu2004 oniguruma-6.9.4]./configure && make && make install
#编译PHP7.4
[root@ubuntu2004 ~]wget https://www.php.net/distributions/php-7.4.30.tar.gz
[root@ubuntu2004 ~]tar xf php-7.4.30.tar.gz
[root@ubuntu2004 ~]cd ../php-7.4.30
[root@ubuntu2004 php-7.4.30]./configure --prefix=/apps/php --enable-mysqlnd --
with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --
with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enablembstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --
disable-fileinfo
[root@ubuntu2004 php-7.4.30]make -j 2 && make install
#查看版本验证编译成功
[root@ubuntu2004 ~]/apps/php/sbin/php-fpm -v
PHP 7.4.30 (fpm-fcgi) (built: Sep 18 2022 18:27:30)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
#编译php-redis
[root@ubuntu2004 ~]wget https://pecl.php.net/get/redis-5.3.7.tgz
[root@ubuntu2004 ~]tar xf redis-5.3.7.tgz
[root@ubuntu2004 ~]cd redis-5.3.7/
[root@ubuntu2004 redis-5.3.7]/apps/php/bin/phpize && ./configure --with-phpconfig=/apps/php/bin/php-config && make -j 2 && make install
[root@ubuntu2004 redis-5.3.7]ls /apps/php/lib/php/extensions/no-debug-zts-20190902
opcache.a opcache.so redis.so
[root@ubuntu2004 redis-5.3.7]cd /root/php-7.4.30/
[root@ubuntu2004 php-7.4.30]cp php.ini-production /etc/php.ini
[root@ubuntu2004 php-7.4.30]cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@ubuntu2004 php-7.4.30]chmod +x /etc/init.d/php-fpm
[root@ubuntu2004 php-7.4.30]cd /apps/php/etc/
[root@ubuntu2004 etc]cp php-fpm.conf.default php-fpm.conf
[root@ubuntu2004 etc]cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@ubuntu2004 ~]vim /etc/php.ini
date.timezone = Asia/Shanghai
post_max_size = 8M
upload_max_filesize = 100M
display_errors = On
error_log = syslog
;extension=/apps/php/lib/php/extensions/no-debug-zts-20190902/redis.so #不写路径
也可以
extension=redis.so
[root@ubuntu2004 ~]vim /apps/php/etc/php-fpm.d/www.conf
user = www
group = www
listen = 127.0.0.1:9000
pm.status_path = /php-status
ping.path = /ping
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://127.0.0.1:6379" #指定Redis地址
#创建访问日志文件路径
[root@ubuntu2004 ~]mkdir /apps/php/log
[root@ubuntu2004 ~]systemctl daemon-reload
[root@ubuntu2004 ~]systemctl enable php-fpm.service
#启动php-fpm
[root@ubuntu2004 ~]systemctl start php-fpm
#或者直接运行程序也可以,默认是后台运行
[root@ubuntu2004 ~]/apps/php/sbin/php-fpm
#验证服务启动
[root@ubuntu2004 ~]ss -ntlp|grep 9000
LISTEN 0 511 127.0.0.1:9000 0.0.0.0:*
users:(("php-fpm",pid=154390,fd=6),("php-fpm",pid=154389,fd=6),("phpfpm",pid=154388,fd=8))
[root@ubuntu2004 ~]ps aux|grep php
root 154388 0.0 0.2 62812 4920 ? Ss 19:45 0:00 php-fpm:
master process (/apps/php/etc/php-fpm.conf)
www 154389 0.0 1.3 74556 27172 ? S 19:45 0:02 php-fpm: pool
www
www 154390 0.0 0.9 67344 19012 ? S 19:45 0:02 php-fpm: pool
www
root 157770 0.0 0.0 9392 724 pts/0 S+ 20:43 0:00 grep --
color=auto php
编译安装 Nginx
[root@ubuntu2004 ~]wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@ubuntu2004 ~]tar xf nginx-1.22.0.tar.gz
[root@ubuntu2004 ~]cd nginx-1.22.0/
[root@ubuntu2004 nginx-1.22.0]./configure --prefix=/apps/nginx --user=www --
group=www --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@ubuntu2004 nginx-1.22.0]make -j 2 && make install
[root@rocky8 ~]mkdir /apps/nginx/conf.d/
#准备配置文件
[root@rocky8 ~]vim /apps/nginx/conf/nginx.conf
user www; #修改此行
http {
....
include /apps/nginx/conf.d/*.conf; #添加此行
}
#配置nginx支持php
[root@ubuntu2004 ~]vim /apps/nginx/conf.d/www.wang.org.conf
server {
listen 80;
server_name www.wang.org;
root /data/www/;
index index.php;
client_max_body_size 20m;
location ~ \.php$|/ping|/php-status {
root /data/www/;
fastcgi_pass 127.0.0.1:9000 ;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}
}
[root@ubuntu2004 ~]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=/apps/nginx/logs/nginx.pid
ExecStartPre=/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
EOF
[root@ubuntu2004 ~]systemctl daemon-reload
[root@ubuntu2004 ~]systemctl enable --now nginx
测试访问 PHP
[root@ubuntu2004 ~]mkdir /data/www -p
#测试访问
[root@ubuntu2004 ~]vim /data/www/test.php
<?php
phpinfo();
?>
#在客户端实现名称解析,也可以配置DNS实现
[root@ubuntu2004 ~]vim /etc/hosts
10.0.0.100 www.wang.org
#访问下面查看是否成功
http://www.wang.org/ping
http://www.wang.org/php-status
http://www.wang.org/test.php
准备 phpMyAdmin 程序
[root@ubuntu2004 ~]wget
https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
[root@ubuntu2004 ~]unzip phpMyAdmin-5.2.0-all-languages.zip
[root@ubuntu2004 ~]mv phpMyAdmin-5.2.0-all-languages/* /data/www
[root@ubuntu2004 ~]cp /data/www/config.sample.inc.php config.inc.php
[root@ubuntu2004 ~]vim /data/www/config.inc.php
$cfg['Servers'][$i]['host'] = '127.0.0.1'; #本机也必须改为127.0.0.1,否则会出现错
误:mysqli::real_connect(): (HY000/2002): No such file or directory
[root@ubuntu2004 ~]chown -R www.www /data/www/
测试访问网站
http://www.wang.org/
用在MySQL创建的用户admin/123456登录
查看 redis 中是否有 session 数据
[root@ubuntu2004 ~]redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:ee7bn6eu6orjn1ajt9j9d06s6c"
127.0.0.1:6379> type "PHPREDIS_SESSION:ee7bn6eu6orjn1ajt9j9d06s6c"
string
127.0.0.1:6379> get "PHPREDIS_SESSION:ee7bn6eu6orjn1ajt9j9d06s6c"
" PMA_token |s:32:\"255a38782c5e615b667d6154352f2922\"; HMAC_secret
|s:16:\"lrIb|hzERrs1et%*\";browser_access_time|a:1:
{s:7:\"default\";i:1663504436;}encryption_key|s:32:\"\x8f\xf3\xb0\x90\x92\x95F\x
8e_\xd8CD\x8b\x9a*\xb7\xed*\xde\x17
\xf4Z\xbb\x1e\xf2l$$&\xe7\x10\";relation|a:1:{i:1;a:41:
{s:7:\"version\";s:5:\"5.2.0\";s:4:\"user\";N;s:2:\"db\";N;s:8:\"bookmark\";N;s:
15:\"central_columns\";N;s:11:\"column_info\";N;s:17:\"designer_settings\";N;s:1
6:\"export_templates\";N;s:8:\"favorite\";N;s:7:\"history\";N;s:16:\"navigationh
iding\";N;s:9:\"pdf_pages\";N;s:6:\"recent\";N;s:8:\"relation\";N;s:13:\"savedse
arches\";N;s:12:\"table_coords\";N;s:10:\"table_info\";N;s:13:\"table_uipref
#删除session
127.0.0.1:6379> flushall
OK
#刷新浏览器,查看是否注销
http://www.wang.org/
如果php-fpm 停止,网站会出现502
[root@ubuntu2004 ~]systemctl stop php-fpm.service