Devops实现之 nginx(一)

1.手动安装部署

1.0 检查软件安装的系统环境

[root@web01 ~]# cat /etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

[root@web01 ~]# uname –r

3.10.0-693.2.2.el7.x86_64

安装nginx的依赖包(pcre-devel openssl-devel)

yum install -y pcre-devel openssl-devel ###安装依赖包

pcre:兼容perl语言正则表达式,perl compatible regular expressions

rewirte模块 参数信息(perl方式定义正则表达式)

openssl:ssh—openssh/openssl—https

总结:所有安装依赖软件,后面都要加上-devel

2.0 下载nginx软件

wget http://nginx.org/download/nginx-1.10.2.tar.gz

解压软件

tar xf nginx-1.10.2.tar.gz

创建管理用户 www

useradd -M -s /sbin/nologin www

3.0 nginx软件编译安装过程

1、配置软件,在软件的解压目录中

[root@web01 nginx-1.10.2]# ./configure --prefix=/application/nginx-1.10.2
–user=www --group=www --with-http_stub_status_module --with-http_ssl_module

编译参数说明:

官网查询地址

–prefix 表示指定软件安装到哪个目录中,指定目录不存在会自动创建

–user/–group nginx工作进程由哪个用户运行管理

–with-http_stub_status_module 启动nginx状态模块功能(用户访问nginx的网络信息)

–with-http_ssl_module 启动https功能模块

通过软件编译过程中的返回值是否正确,确认配置是否正确

[root@web01 nginx-1.10.2]# echo $?

0

2、编译软件

[root@web01 nginx-1.10.2]# make

3、编译安装

[root@web01 nginx-1.10.2]# make install

3.1.6 创建软连接

[root@web01 application]# ln -s /application/nginx-1.10.2/ /application/nginx

3.1.7 精简化nginx.conf 主配置文件内容

[root@web01 conf]# egrep -v “#|^$” nginx.conf.default >nginx.conf

3.1.8 启动程序

[root@web01 application]# /application/nginx/sbin/nginx

检查是否启动

[root@web01 application]# ps -ef |grep nginx

root 26548 1 0 20:13 ? 00:00:00 nginx: master process
/application/nginx/sbin/nginx

www 26549 26548 0 20:13 ? 00:00:00 nginx: worker process

root 26551 23431 3 20:13 pts/0 00:00:00 grep –color=auto nginx

检查端口信息

[root@web01 application]# netstat -lntup |grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26548/nginx

服务部署完成

nginx命令简化方法

echo ‘export PATH=/application/nginx/sbin:$PATH’ >>/etc/profile

source /etc/profile

which nginx

安装后的nginx 目录结构

[root@web01 nginx]# ll

total 36

drwxr-xr-x 2 root root 4096 Oct 21 19:34 conf #配置文件保存目录

drwxr-xr-x 2 root root 4096 Oct 21 19:34 html #站点目录

drwxr-xr-x 2 root root 4096 Oct 21 20:26 logs #nginx
服务相关日志文件保存目录(错误日志访问日志)

drwxr-xr-x 2 root root 4096 Oct 21 19:34 sbin #
服务命令目录(只有一个nginx文件)

2.nginx软件的编译安装常见错误说明

2.0.1 nginx软件安装过程中遇到的问题

软件依赖包未正确安装问题—PCRE依赖包没有安装

./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 pcre pcre-devel -y

软件依赖包未正确安装问题—OPENSSL依赖包没有安装

./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 openssl openssl-devel -y

2.0.2 nginx软件启动过程中遇到的问题

nginx软件重复启动产生的错误信息

[root@web01 nginx-1.10.2]# /application/nginx/sbin/nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

解决方法:

nginx软件已经启动无需反复启动,如果需要重新启动需要停止nginx进程或者用reload方式进行重启

2.0.3 启动 Nginx 时如下报错“nginx:[emerg]getpwnam(“nginx”〉failed”

解答这是因为没有对应的Nginx服务用户,执行useradd
nginx-s/sbin/no丨ogin-M创建 Nginx

用户即可。为了让读者理解问题,重现上述错误过程,命令如下:

[root@web01 tools]# pkill nginx

[root@web01 tools]# userdel nginx

[root@web01 tools]# /application/nginx/sbin/nginx

nginx: [emerg] getpwnam(Mnginx”) failed

[root@web01 tools]# useradd nginx -s /sbin/nologin -M

[root@web01 tools]# /application/nginx/sbin/nginx

2.0.4 编译安装pcre编译软件时,gcc不全导致报错(本文使用yum安装不存在此问题)。

报错信息如下:

[root@gjlin2 pcre-8.30]# make && make install

make all-am

make[l] : Entering directory 7home/gjlin/tools/pcre-8.30′

CXX pcrecpp.lo

libtool : compile : unrecognized option ‘-DHAVE_CONFIG_H’

libtool : compile : Try ‘libtool –help* for more information.

make[l】:*** [pcrecpp.lo】错误 1

make[l] : Leaving directory Vhome/gjlin/tools/pcre-8.30′

make : *** [all]错误 2

解答:执行“yum -y install gcc-c++”命令安装gcc-c++依赖包。

2.0.5 nginx软件编译安装后,看不到程序目录(/application)

说明:编译安装步骤不对(配置 编译 编译安装生成/appliation)

2.0.6 nginx软件排查问题三部曲说明

  a 在客户端上ping服务器端IP,检查链路是否通畅

  b 在客户端上telnet服务器端IP、端口,检查链路访问是否通畅

  c 在客户端上wget检测模拟页面访问是否正常

2.0.7 【注意】403状态码出现情况原因

  1. 服务阻止客户端访问

  2. 服务端站点目录中,没有指定首页文件信息

3.nginx软件使用命令参数

官网地址:http://nginx.org/en/docs/switches.html

3.0.1 nginx 启动方法

[root@web01 application]# /application/nginx/sbin/nginx

3.0.2 nginx 停止方法

[root@web01 application]# /application/nginx/sbin/nginx -s stop

3.0.3 nginx 重启方法 (平滑重启)

[root@web01 application]# /application/nginx/sbin/nginx -s reload

3.0.4 检查配置文件语法是否正确

[root@web01 application]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax
is ok

nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is
successful

3.0.5 显示配置参数 -V (大写V)

[root@web01 application]# /application/nginx/sbin/nginx -V

nginx version: nginx/1.10.2

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: –prefix=/application/nginx-1.10.2 –user=www –group=www
–with-http_stub_status_module –with-http_ssl_module

3.0.6 nginx软件使用过程中深入说明

①. nginx软件语法检查方法:

nginx -t

②. nginx软件访问测试过程:

curl -v www.baidu.com

③. nginx软件编译参数查看:

nginx -V <— 查看原有的编译参数信息

4.设置nginx开机自启动(centos7.x)

第一步:进入到/lib/systemd/system/目录

[root@iz2z init.d]# cd /lib/systemd/system/

第二步:创建nginx.service文件,并编辑

# vim nginx.service

内如如下:

Unit

Description=nginx service

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

[Install]

WantedBy=multi-user.target

Description:描述服务

After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart为服务的具体运行命令

ExecReload为重启命令

ExecStop为停止命令

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出。

第三步:加入开机自启动

# systemctl enable nginx

如果不想开机自启动了,可以使用下面的命令取消开机自启动

# systemctl disable nginx

第四步:服务的启动/停止/刷新配置文件/查看状态

# systemctl start nginx.service  启动nginx服务

# systemctl stop nginx.service  停止服务

# systemctl restart nginx.service  重新启动服务

# systemctl list-units --type=service 查看所有已启动的服务

# systemctl status nginx.service 查看服务当前状态

# systemctl enable nginx.service 设置开机自启动

# systemctl disable nginx.service 停止开机自启动

一个常见的错误

Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload’ to reload
units.

直接按照提示执行命令systemctl daemon-reload 即可。

5.Nginx的平滑升级

1、查看原先系统Nginx版本和编译参数并记录

[root@localhost ~]# nginx -v

nginx version: nginx/1.13.12

[root@localhost ~]# nginx -V

nginx version: nginx/1.13.12

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

configure arguments: --with-http_realip_module --with-http_sub_module
–with-http_gzip_static_module --with-http_stub_status_module
–with-http_addition_module --with-http_xslt_module --with-pcre

2、下载最新版本Nginx并解压

[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.1.tar.gz

–2018-11-15 02:24:36-- http://nginx.org/download/nginx-1.14.1.tar.gz

Resolving nginx.org (nginx.org)… 95.211.80.227, 206.251.255.63,
2001:1af8:4060:a004:21::e3, …

Connecting to nginx.org (nginx.org)|95.211.80.227|:80… connected.

HTTP request sent, awaiting response… 200 OK

Length: 1014040 (990K) [application/octet-stream]

Saving to: ‘nginx-1.14.1.tar.gz’

100%[==================================================================================================================================>]
1,014,040 117KB/s in 7.6s

2018-11-15 02:24:44 (130 KB/s) - ‘nginx-1.14.1.tar.gz’ saved [1014040/1014040]

[root@localhost ~]# tar -zxf nginx-1.14.1.tar.gz

3、编译Nginx 1.14.1

[root@localhost ~]# cd nginx-1.14.1

[root@localhost nginx-1.14.1]# ./configure --with-http_realip_module
–with-http_sub_module --with-http_gzip_static_module
–with-http_stub_status_module --with-http_addition_module
–with-http_xslt_module --with-pcre

[root@localhost nginx-1.14.1]# make #到make这一步即可,不需要执行make install

4、备份原来的nginx启动脚本

[root@localhost nginx-1.14.1]# cd /usr/local/nginx/sbin/

[root@localhost sbin]# mv nginx nginx_old

5、拷贝nginx-1.14.1目录下的obj目录下的nginx到nginx的sbin目录下

[root@localhost sbin]# cp /root/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/

6、回到nginx-1.14.1目录下执行make upgrade

[root@localhost sbin]# cd /root/nginx-1.14.1

[root@localhost nginx-1.14.1]# make upgrade
#这一步会将结束旧进程,并开启新的进程进行管理nginx的任务,从而达到平滑升级的效果

/usr/local/nginx/sbin/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

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

sleep 1

test -f /usr/local/nginx/logs/nginx.pid.oldbin

kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

这里需要注意的是,使用make
upgrade进行平滑升级时,会默认发送USR2信号到/usr/local/nginx/logs/nginx.pid,但是如果你的pid文件位置不一致,就会出现文件不存在的ERROR

而我们需要做的是,放弃使用make
upgrade,而是直接使用以下命令,假设nginx.pid的路径为:/var/run/nginx.pid

[root@localhost nginx-1.14.1]# /usr/local/nginx/sbin/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@localhost nginx-1.14.1]# kill -USR2 `cat /var/run/nginx.pid`

此时会在/var/run/下生成一个nginx.pid.oldbin的pid文件

[root@localhost nginx-1.14.1]# kill -QUIT `cat /var/run/nginx.pid.oldbin`
#退出旧进程

7、查看nginx的最新版本

[root@localhost nginx-1.14.1]# nginx -v

nginx version: nginx/1.14.1

[root@localhost nginx-1.14.1]# nginx -V

nginx version: nginx/1.14.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

configure arguments: --with-http_realip_module --with-http_sub_module
–with-http_gzip_static_module --with-http_stub_status_module
–with-http_addition_module --with-http_xslt_module --with-pcre

Nginx1.12升级到Nginx1.16稳定版本

环境说明

服务器中最开始使用的是Nginx1.3.14版本,目前正在运行Nginx1.12版本,现在想将当前运行的Nginx服务的版本进行升级到最新的稳定版,(从1.12升级到1.16,版本的跨度不要太大,容易造成服务的崩溃),且在服务不停止的前提下经行升级。

1. 在不停掉老进程的情况下,启动新进程。

2. 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。

3. 新进程接受新请求。

4. 老进程处理完所有请求,关闭所有连接后,停止。

实现步骤:

1、获取nginx的安装目录

[root@XiaoFeng logs]# ps -ef | grep nginx | grep master | awk -F" "
‘{print $11}’

/usr/local/nginx/sbin/nginx

2、查看升级之前的nginx版本,同时获取旧nginx版本的编译参数

[root@XiaoFeng sjd]# /usr/local/nginx/sbin/nginx -V #注意是大写的V

nginx version: nginx/1.12.2

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/
–with-http_v2_module --with-http_ssl_module --with-http_sub_module
–with-http_stub_status_module --with-http_gzip_static_module --with-pcre
–with-http_realip_module

3、到官网下载1.16.0版本的nginx (下载地址:http://nginx.org/en/download.html)

[root@XiaoFeng sjd]# wget http://nginx.org/download/nginx-1.16.0.tar.gzCopy

4、解压下载好的文件,并进入文件夹进行编译(只编译不安装)

[root@XiaoFeng sjd]# tar xf nginx-1.16.0.tar.gz

[root@XiaoFeng sjd]# cd nginx-1.16.0

[root@XiaoFeng nginx-1.16.0]# ./configure --user=nginx --group=nginx
–prefix=/usr/local/nginx/ --with-http_v2_module --with-http_ssl_module
–with-http_sub_module --with-http_stub_status_module
–with-http_gzip_static_module --with-pcre --with-http_realip_module

[root@XiaoFeng nginx-1.16.0]# make

5、备份旧版本的nginx的执行程序

[root@XiaoFeng nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx12.old

6、替换旧的Nginx的执行程序

[root@XiaoFeng nginx-1.16.0]# cp objs/nginx /usr/local/nginx/sbin/

[root@webserver nginx-1.16.0]# cd /usr/local/nginx/sbin/

[root@XiaoFeng sbin]# ls

nginx nginx12.old nginx.old

7、发送USR2信号给旧版本主进程号,使nginx的旧版本停止接收请求,用nginx新版本接替,且老进程处理完所有请求,关闭所有连接后,停止

[root@webserver sbin]# cd …

[root@webserver nginx]# cd logs/

[root@XiaoFeng logs]# ls

access.log error.log nginx.pid shunjie.access.log

[root@XiaoFeng logs]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

8、查看nginx pid目录,多了个nginx.pid.oldbin文件,存放了旧版本nginx的pid号

[root@XiaoFeng logs]# ls

access.log error.log nginx.pid nginx.pid.oldbin

#查看服务运行状态

[root@XiaoFeng logs]# netstat -aupt | grep nginx

tcp 0 0 *:http *😗 LISTEN 793/nginx

9、从容关闭旧进程

[root@XiaoFeng logs]# kill -QUIT `cat nginx.pid.oldbin`

[root@XiaoFeng logs]# ls

access.log error.log nginx.pid shunjie.access.log

10、查看升级后的版本

[root@XiaoFeng logs]# /usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.16.0

平滑升级完毕!

附:如果是yum安装的nginx,升级就很简单了,直接yum update
nginx即可安装到仓库有的最新版,不过通常库更新的比较慢。

rpm包安装的话,就直接rpm -Uvh nginx-1.16.0.rpm即可

6.Nginx添加Lua扩展模块

编译安装LuaJIT

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar xf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make PREFIX=/usr/local/luajit

make install PREFIX=/usr/local/luajit

下载扩展模块

cd /usr/local/src/

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xf v0.3.0.tar.gz

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz

tar xf v0.10.8.tar.gz

编辑安装nginx

yum groupinstall -y “Development Tools”

yum install -y libxml2-devel curl-devel pcre-devel openssl-devel siege
traceroute vim openssl

cd /usr/local/src

wget http://nginx.org/download/nginx-1.10.3.tar.gz

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

tar xf nginx-1.10.3.tar.gz && cd nginx-1.10.3

./configure \

–prefix=/usr/local/nginx-1.10.2 \ # nginx安装目录

–with-http_ssl_module \ # 支持 SSL

–with-http_stub_status_module \ # nginx状态模块

–add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ # lua模块

–add-module=/usr/local/src/lua-nginx-module-0.10.8 # lua扩展模块

make && make install

mkdir /usr/local/nginx-1.10.2/conf/vhost

ln -s /usr/local/nginx-1.10.3/sbin/nginx /bin/nginx

重新编译nginx

# 查看之前的编译参数

nginx -V

# 设置环境变量

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

# 进入源码包目录

cd /opt/software/nginx-1.10.2/

./configure \

–prefix=/usr/local/nginx-1.10.2 \ # nginx安装目录

–with-http_ssl_module \ # 支持 SSL

–with-http_stub_status_module \ # nginx状态模块

–add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ # lua模块

–add-module=/usr/local/src/lua-nginx-module-0.10.8 # lua扩展模块

make

make install

遇到的报错:

# nginx -t

nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open
shared object file: No such file or directory

# 解决: 查找文件,创建软连接
find / -name “libluajit-5.1.so.2”

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/

第一个lua脚本

在server块中添加

location /lua {

default_type ‘text/html’;

content_by_lua_file conf/lua/test.lua;    # 相对于nginx安装目录

}

# 编写lua脚本

[root@yunwei-test conf]# pwd

[root@yunwei-test conf]# /usr/local/nginx-1.10.2/conf

[root@yunwei-test conf]# mkdir lua && cd lua

[root@yunwei-test conf]# vim test.lua

ngx.say(“hello world”);

# 启动nginx

[root@yunwei-test conf]# nginx -t

[root@yunwei-test conf]# nginx

浏览器访问:10.0.3.56/lua

显示 hello world,表示正常

nginx + lua获取url请求参数

有个需求就是获取 url 中 clientId

参数的值,根据clientid中的参数upstream到不同服务器,url有GET和POST请求。

代码如下:

upstream sdk_proxy {

server 127.0.0.1:188;

keepalive 64;

}

upstream default_sdk {

server 127.0.0.1:288;

keepalive 64;

}

server {

listen 6443;

server_name 127.0.0.1;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

location / {

default_type text/plain;

access_by_lua ’

local request_method = ngx.var.request_method

local clientids = {“112”,“113”,“114”}

if (request_method == “GET”) then

local arg = ngx.req.get_uri_args()[“clientId”] or 0

for i,clientid in ipairs(clientids) do

if (arg == clientid) then

ngx.exec("@sdk")

end

end

elseif (request_method == “POST”) then

ngx.req.read_body()

local arg = ngx.req.get_post_args()[“clientId”] or 0

for i,clientid in ipairs(clientids) do

if (arg == clientid) then

ngx.exec("@sdk")

end

end

end

';

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_pass http://default_sdk;

}

location @sdk {

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_pass http://sdk_proxy;

}

}

7.自动化安装脚本

#!/bin/bash

# info : 运行时 后面更上升级的压缩包名(sh ngxup.sh nginx-1.18.0.tar.gz)

myfile=$1 # 定义升级时所需的版本压缩包名

date=`date +%Y%m%d%H%M%S` # 获取当前系统日期

pid=`ps aux | grep -Ev “grep|$0” | grep nginx: | awk ‘NR==1{print $2}’`
# 获取当前运行 nginx 的 PID

nginxpath=`ls -l /proc/$pid/cwd 2>1 | awk ‘{print $NF}’` # 通过 PID
获取安装目录

ck_status(){

if [ $? -eq 0 ];then

echo ‘正在进行,请稍等。。。’

else

echo ‘失败,请查看日志( ~/nginx_up.log 升级日志,~/nginx_install.log
安装日志,~/nginx_add.log 添加模块日志)’

fi

} # 状态检查函数

jys(){

cd /tmp/

if [ ! -f $myfile ];then

echo “请将升级压缩包放在 /tmp 目录下”

else

read -p ‘请指定安装目录:’ path

tar -xf $myfile -C $path

fi

} # 升级版本判断包和解压缩函数

file_if(){ # 判断当期环境是否存在 nginx 函数

if [ ! -n “$pid” ];then # 如果不存在则进行安装

read -p “没有检测到 nginx 进程是否安装[Y/n]?” x

case $x in

[yY][eE][sS]|[yY])

mkdir $path/nginx # 以下为安装流程

newyspath=`tar -tf $myfile | head -1`

cd $newyspath

./configure --prefix=$path/nginx --with-http_stub_status_module
–with-http_ssl_module >> ~/nginx_install.log

ck_status

make >> ~/nginx_install.log && make install >> ~/nginx_install.log

ck_status && echo “安装完成”

;;

[yY][eE][sS]|[yY])

echo “退出脚本”

exit

;;

esac

else # 存在则进行升级

ngx_up

versionif

fi

}

ngx_up(){ # nginx 升级脚本

yspath=`tar -tf $myfile | head -1` # 获取解压缩目录

cd $yspath # 以下为升级流程

./configure --prefix=$nginxpath --with-http_stub_status_module
–with-http_ssl_module >> ~/nginx_up.log

ck_status

make >> ~/nginx_up.log

ck_status

mv $nginxpath/sbin/nginx $nginxpath/sbin/nginx$date

cp objs/nginx $nginxpath/sbin/

make upgrade >> ~/nginx.up.log

#ck_status

cd $nginxpath && oldversion=`./sbin/nginx-bak -V 2>&1 | awk -F":"
‘NR==1{print $2}’`

}

versionif(){

cd $nginxpath # 版本判断函数,升级之前同升级之后版本号是否相同

version=`./sbin/nginx -V 2>&1 | awk -F":" ‘NR==1{print $2}’`

if [ $version != $oldversion ];then

echo “升级成功”

else

echo “升级失败”

fi

}

ngx_add(){ # nginx 添加 nginx 自带模块

read -p ‘请输入 nginx 编译路径:’ inpath # 输入获取 nginx
的编译目录(解压缩目录)

cd $inpath # 以下为添加流程

read -p ‘请输入重新编译的参数(如:–prefix=/home/nginx
–with-http_stub_status_module --with-http_ssl_module):’ cs #
获取要添加的模块选项

./configure --prefix=$nginxpath $cs >> ~/nginx_add.log

ck_status

make >> ~/nginx_add.log

ck_status

mv $nginxpath/sbin/nginx $nginxpath/sbin/nginx$date

cp $inpath/objs/nginx $nginxpath/sbin/

}

read -p “请选择执行操作(输入序号): 1.安装nginx,2.添加模块,3.升级版本:” i #
相当于主

case $i in

“1”)

jys

file_if

;;

“2”)

ngx_add

;;

“3”)

jys

file_if

;;

esac

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页