-
完成nginx编译安装脚本
#!/bin/bash #下载 wget https://nginx.org/download/nginx-1.26.1.tar.gz tar xf nginx-1.26.1.tar.gz #安装依赖包 yum -y install gcc pcre-devel openssl-devel zlib-devel yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed #创建用户和组 useradd -s /sbin/nologin nginx cp -r /root/nginx-1.26.1 ./ ./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 ##编译安装
-
完成nginx平滑升级,总结步骤
#当前版本 root@localhost:~# /apps/nginx/sbin/nginx -v nginx version: nginx/1.26.1 #下载最新版本 root@localhost:/apps/nginx/sbin# wget https://nginx.org/download/nginx-1.27.0.tar.gz root@localhost:~# mv /apps/nginx/sbin/nginx-1.27.0.tar.gz ./ root@localhost:~# tar xf nginx-1.27.0.tar.gz root@localhost:~# cd ./nginx-1.27.0 #配置 root@localhost:./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 root@localhost:~/nginx-1.27.0# make make -f objs/Makefile root@localhost:~/nginx-1.27.0# objs/nginx -v nginx version: nginx/1.27.0 #查看两个版本 root@localhost:~/nginx-1.27.0# ll objs/nginx /apps/nginx/sbin/nginx -rwxr-xr-x. 1 root root 5746888 Jun 30 20:33 /apps/nginx/sbin/nginx -rwxr-xr-x. 1 root root 5747264 Jun 30 20:59 objs/nginx #备份之前的旧版的nginx root@localhost:~/nginx-1.27.0# cp /apps/nginx/sbin/nginx /opt/nginx.old #覆盖 root@localhost:~/nginx-1.27.0# cp -f ./objs/nginx /apps/nginx/sbin/ cp: overwrite '/apps/nginx/sbin/nginx'? y #检测新版本和配置文件语法兼职容性 root@localhost:~/nginx-1.27.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 #查看旧nginx是否工作 root@localhost:~/nginx-1.27.0# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 511 0.0.0.0:80 0.0.0.0:* root@localhost:~/nginx-1.27.0# curl -I http://10.0.0.159 HTTP/1.1 200 OK Server: nginx/1.26.1 Date: Sun, 30 Jun 2024 13:11:23 GMT Content-Type: text/html Content-Length: 615 #先关闭旧nginx的worker进程,而不关闭旧nginx主进程方便回滚 #向原老的Nginx主进程发送WINCH信号,它会平滑关闭老的工作进程(主进程不退出),这时所有新请求都会 由新版Nginx处理 root@localhost:~/nginx-1.27.0# kill -WINCH `cat /apps/nginx/logs/nginx.pid` #查看进程 root@localhost:~/nginx-1.27.0# ps auxf|grep nginx root 14159 0.0 0.2 3876 1920 pts/0 S+ 21:15 0:00 | \_ grep --color=auto nginx root 10961 0.0 0.3 9932 2568 ? Ss 20:34 0:00 nginx: master process /apps/nginx/sbin/nginx root@localhost:~/nginx-1.27.0# kill -QUIT `cat /apps/nginx/logs/nginx.pid` oot@localhost:~/nginx-1.27.0# /apps/nginx/sbin/nginx -V nginx version: nginx/1.27.0
平滑升级四个阶段
-
只有旧版nginx的master和worker进程
-
旧版和新版nginx的master和worker进程并存,由旧版nginx接收处理用户的新请求
-
旧版和新版nginx的master和worker进程并存,由新版nginx接收处理用户的新请求
-
只有新版nginx的master和worker进程
编译新版本,生成新版本的二进制文件 用新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
-
-
总结nginx核心配置,并实现nginx多虚拟主机
Nginx的配置文件的组成部分:
主配置文件:nginx.conf
子配置文件: include conf.d/*.conf
fastcgi, uwsgi,scgi 等协议相关的配置文件
mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮
件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某
种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动
使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
多虚拟主机
server { listen 80; server_name huang.com; location / { proxy_set header Host $host; proxy_set header X-Real-IP $remote_addr; proxy_set header X-Forwarded-For $remote_addr; root /data/huang.com/html index index.html; } } server { listen 80; server_name huang.org; location / { proxy_set header Host $host; proxy_set header X-Real-IP $remote_addr; proxy_set header X-Forwarded-For $remote_addr; root /data//huang.org/html index index.html; } } server { listen 80; server_name huang.net; location / { proxy_set header Host $host; proxy_set header X-Real-IP $remote_addr; proxy_set header X-Forwarded-For $remote_addr; root /data/huang.net/html index index.html; } }
-
总结nginx日志格式定制
默认日志格式
http { 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 /var/log/nginx/access.log main;
自定义 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"}
$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地址和客户端请求的服务器地址
-
总结 nginx反向代理及https安全加密
代理Proxy有两种
正向代理:代理客户端访问服务器,可以实现缓存,科学上网,访问控制等功能
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给
用户的一种方式,这是用的比较多的一种方式。
通过使用 Nginx 反向代理,可以保护目标服务器的资源安全,节省有限的 IP 地址资源,并提高对服务器的访问速度。通过合理配置,可以灵活应对不同的需求,并提供安全、高性能的代理服务。
保护目标服务器的资源安全:Nginx 反向代理可以隐藏目标服务器的真实 IP 地址和端口,防止恶意用户直接攻击目标服务器。只有 Nginx 反向代理服务器对外可见,从而保护了目标服务器的资源安全。
节省有限的 IP 地址资源:由于 IPv4 地址资源有限,通过使用 Nginx 反向代理,可以将多个后端服务器隐藏在同一个公网 IP 地址后面。这样,可以节省 IP 地址资源并最大限度地提供服务。
提高对服务器的访问速度:Nginx 反向代理可以缓存静态资源,减少后端服务器的负载并提高响应速度。此外,Nginx 使用高效的事件驱动模型,能够处理大量并发连接。通过合理配置和使用,Nginx 反向代理可以提高对服务器的访问速度。Web网站的登录页面通常都会使用https加密传输的,加密数据以保障数据的安全,HTTPS能够加密信息,以免敏感信息被第三方获取,所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用
HTTPS协议,HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信
息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。
客户端访问某个web端的https地址,一般都是443端口。
https 实现过程如下:
1.客户端发起HTTPS请求:
2.服务端的配置:
采用https协议的服务器必须要有一套证书,可以通过一些组织申请,也可以自己制作,目前国内很多网站都
自己做的,当你访问一个网站的时候提示证书不可信任就表示证书是自己做的,证书就是一个公钥和私钥匙,
就像一把锁和钥匙,正常情况下只有你的钥匙可以打开你的锁,你可以把这个送给别人让他锁住一个箱子,里
面放满了钱或秘密,别人不知道里面放了什么而且别人也打不开,只有你的钥匙是可以打开的。
3.传送证书:
服务端给客户端传递证书,其实就是公钥,里面包含了很多信息,例如证书得到颁发机构、过期时间等等。
4.客户端解析证书:
这部分工作是有客户端完成的,首先回验证公钥的有效性,比如颁发机构、过期时间等等,如果发现异常则会
弹出一个警告框提示证书可能存在问题,如果证书没有问题就生成一个随机值,然后用证书对该随机值进行加
密,就像2步骤所说把随机值锁起来,不让别人看到。
5.传送4步骤的加密数据:
就是将用证书加密后的随机值传递给服务器,目的就是为了让服务器得到这个随机值,以后客户端和服务端的
通信就可以通过这个随机值进行加密解密了。
6.服务端解密信息:
服务端用私钥解密5步骤加密后的随机值之后,得到了客户端传过来的随机值(私钥),然后把内容通过该值进
行对称加密,对称加密就是将信息和私钥通过算法混合在一起,这样除非你知道私钥,不然是无法获取其内部
的内容,而正好客户端和服务端都知道这个私钥,所以只要机密算法够复杂就可以保证数据的安全性。
7.传输加密后的信息:
服务端将用私钥加密后的数据传递给客户端,在客户端可以被还原出原数据内容。
8.客户端解密信息:
客户端用之前生成的私钥获解密服务端传递过来的数据,由于数据一直是加密的,因此即使第三方获取到数据
也无法知道其详细内容。
-
实验完成基于LNMP和Redis的phpmyadmin的会话保持,记录完整步骤
准备 MySQL 和 Redis
apt -y install mysql-server redis
#配置远程连接
[root@ubuntu2004 ~]#vim /etc/mysql/mysql.conf.d/mysql.cnf
bind-address =127.0.0.1
mysqlx-bind-address = 127.0.0.1
[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 --enable
mbstring --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-php
config=/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
#安装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
[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
#测试访问 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/