①nginx 安装简介及热升级

https://w3techs.com/technologies/overview/web_server web服务排名
nginx程序功能介绍
特点:nginx因具有高并发(特别是静态资源)、占用系统资源少等特性 用线程来处理请求,共享worker进程的内存。所以占用资源少。apache以进程的方式处理请求,占用资源高。
功能:nginx程序功能强大
1)反向代理 支持判断表达式,根据不同的表达式,采取不同的转发策略。
2)可以满足负载均衡应用 把请求分发到后端相同功能的服务器 可以有效的提升服务的响应时间 减少客户的等待时间
3)可以满足缓存应用 Squid
局限:无法处理动态资源请求

盗链概念:网站图片被别的站点引用
网站服务apache vs nginx
01. 从特点进行说明   nginx高并发 占用物理资源少
02. 从功能方面说明   功能多  负载均衡  方便学习 容易上手
03. 从软件网络模型   网络编程 socket 
    select:apache  
	事件01: 宿舍管理员  找人---一个一个房间进行查找(遍历)
	事件02:幼儿园阿姨  负责看住小孩上厕所---一个一个进行确认
	epoll: nginx
    事件01: 宿舍管理员  找人---查找名单册
    事件02:幼儿园阿姨  负责看住小孩上厕所---有感觉就站到教室圈里

nginx软件部署安装过程:

1)利用yum安装软件:
   官方源安装:最新稳定版软件  目录结构信息(企业环境相符)
   其他源安装:稳定版软件	   目录结构信息(企业环境不太相符)  
2)利用编译方式安装:自定义安装功能 自定义程序安装的目录
   第一步:解决软件依赖关系
   第二步:进行软件配置过程  配置软件目录  指定软件功能
   第三步:进行软件编译过程  翻译解释的过程   C-gcc  python-python解释器
   第四步:进行软件编译安装

web01:采用官方源安装

第一个历程:修改yum源

官方参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
第二个历程:清除yum缓存信息
yum clean all
第三个历程:yum安装软件
yum install -y nginx
第四个历程: 管理nginx

查看nginx的状态

systemctl status nginx  

启动nginx服务

systemctl start  nginx   

硬重启nginx服务

systemctl restart nginx  

软重启nginx服务

systemctl reload  nginx  

Nginx编译安装软件

第一步:下载源码包

cd /opt
wget http://nginx.org/download/nginx-1.22.1.tar.gz

补充:解决软件依赖:

yum install -y pcre-devel

说明:pcre-perl兼容正则表达式
未安装报错信息:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

yum install -y openssl-devel

说明:openssl-devel 实现支持HTTPs 需要有私钥 公钥(证书)
未安装报错信息:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.

 yum install -y gcc-c++ -y

说明:gcc c语言的解释器(nginx -- c语言 python)
checking for OS
+ Linux 2.6.32-642.el6.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

第二步:解压源码包

 tar xf nginx-1.22.1.tar.gz

第三步:软件编译配置过程

cd /opt/nginx-1.22.1/
useradd www -s /sbin/nologin -M
./configure  --prefix=/app/nginx-1.22.1  \
--user=www \
--group=www \
--with-compat \
--with-file-aio \
--with-threads  \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module  \
--with-http_flv_module  \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module  \
--with-http_random_index_module \
--with-http_realip_module  \
--with-http_secure_link_module \
--with-http_slice_module  \
--with-http_ssl_module  \
--with-http_stub_status_module \
--with-http_sub_module  \
--with-http_v2_module  \
--with-mail  \
--with-mail_ssl_module  \
--with-stream  \
--with-stream_realip_module \
--with-stream_ssl_module   \
--with-stream_ssl_preread_module

=======================================================================================
--prefix=PATH                      set installation prefix
	                                   指定软件程序安装目录(不需要创建)
--user=USER                        set non-privileged user for worker processes
	                                   为worker进程设置一个非特权用户(必须存在)
--group=GROUP                      set non-privileged group for worker processes
	                                   为worker进程设置一个非特权用户组(必须存在)
--with-http_ssl_module             enable ngx_http_ssl_module
	                                   启用HTTPS功能
--with-http_stub_status_module     enable ngx_http_stub_status_module
	                                   启动nginx状态监控功能

第四步:编译过程

make

第五步:编译安装

make install

第六步:程序目录创建软链接

ln -s /app/nginx-1.22.1/   /app/nginx
ln -s /app/nginx-1.22.1/sbin/nginx  /usr/local/bin/
ln -s /app/nginx-1.22.1/sbin/nginx /usr/sbin/
ln -s /app/nginx/conf  /etc/nginx

第七步:设置systemd管理
cat /lib/systemd/system/nginx.service

[Unit]
Description=nginx service
After=network.target
 
   
[Service]
Type=forking
ExecStart=/app/nginx/sbin/nginx
ExecReload=/app/nginx/sbin/nginx -s reload
ExecStop=/app/nginx/sbin/nginx -s quit
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

nginx日志切割

# cat /etc/nginx/nginx.conf

pid /var/run/nginx.pid;

# cat /etc/logrotate.d/nginx 

/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 www www
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

nginx程序目录结构信息

参见图 
/etc/nginx/nginx.conf      --- 主配置文件
/etc/nginx/*cgi(通用接口)  --- nginx(无法处理动态请求) -fast_cgi- php   
                                                         -uwsgi   - python 

Nginx热升级

1.下载源码

[root@centos7 ~]# cd /usr/src/
[root@centos7 src]# ls
debug  kernels  nginx-1.15.12  nginx-1.15.12.tar.gz  nginx-1.20.2  nginx-1.20.2.tar.gz

2.编译nginx1.15源码

mkdir /nginx
cd /usr/src/nginx-1.15.12 
./configure --prefix=/nginx      #生成Makefile
make.                                    #生成二进制文件
make install                            #把二进制文件cp到/nginx目录
nginx
nginx -v
[root@centos7 nginx-1.20.2]# curl -I http://192.168.1.241
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Tue, 01 Mar 2022 09:01:12 GMT

3.编译nginx1.20


]# cd /usr/src/nginx-1.20.2/
]# vim auto/cc/gcc                                                          #注释。  #CLFLAGS。。。。
]# ./configure. --prefix=/nginx
]# make                                                                          #不能make install  这样会大致主程序失控
]# ls objs/
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src

3.热升级

]# cp -f /nginx/sbin/nginx  /nginx/sbin/nginx.old.               #备份原来的nginx启动文件
cp:是否覆盖"/nginx/sbin/nginx.old"? y
]# cp -f /usr/src/nginx-1.20.2/objs/nginx  /nginx/sbin/nginx   #把编译的nginx文件复制到指定位置
cp:是否覆盖"/nginx/sbin/nginx"? y

]# ps -ef|grep nginx
root         26118      1    0 16:03 ?          00:00:00 nginx: master process ./sbin/nginx
nobody    26119  26118  0 16:03 ?          00:00:00 nginx: worker process
]# kill -USR2 26118                                                        #启动新进程 老进程缓慢停止
]# ps -ef|grep nginx
root         26118      1    0 16:03 ?          00:00:00 nginx: master process ./sbin/nginx
nobody    26119  26118  0 16:03 ?          00:00:00 nginx: worker process
root         29617  26118  0 17:06 ?         00:00:00 nginx: master process ./sbin/nginx
nobody    29618  29617  0 17:06 ?         00:00:00 nginx: worker process
]# kill -WINCH 26118
]# ps -ef|grep nginx
root       26118      1       0 16:03 ?         00:00:00 nginx: master process ./sbin/nginx
root       29617  26118    0 17:06 ?         00:00:00 nginx: master process ./sbin/nginx
nobody    29618  29617  0 17:06 ?         00:00:00 nginx: worker process
]# curl -I http://192.168.1.241
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 01 Mar 2022 09:11:45 GMT
posted @ 2021-03-31 17:35  老夫聊发少年狂88  阅读(82)  评论(0)    收藏  举报