nginx 迁移 openResty

1,795 阅读3分钟

一、nginx 迁移 openResty

1.1 版本选择

  • 当前2020-12-31nginx最新稳定版本1.18.0

  • openResty 1.17.8.X 版本

  • openResty 1.17.8.X 版本

综合以上信息,openResty选择 1.17.8.2 版本

1.2 openResty 安装

  • 下载&解压
cd /usr/local
# 下载
wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
# 解压
tar -xvf openresty-1.17.8.2.tar.gz
  • 源码编译
# 切换目录
cd openresty-1.17.8.2

# 安装
# 方式一:
./configure # 或者下面,后面是参数,不写默认就行
gmake
gmake install

# 方式二
./configure --prefix=/usr/local/openresty/ --with-http_stub_status_module --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module --with-stream gmake && gmake install
  • 添加环境变量
1、打开配置文件: vim /etc/profile
2、添加代码:
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
3、退出保存
4、执行生效命令: source /etc/profile
5、查看是否生效: echo $PATH

  • 添加仓库执行命令 (安装 openresty nginx)
yum install yum-utils

添加仓库需按照linux系统内核决定该下载对应仓库,如果不知道自己系统内核版本,请参考下列指令:

uname -a    #可显示电脑以及操作系统的相关信息。 
cat /proc/version   #说明正在运行的内核版本。
cat /etc/issue  #显示的是发行版本信息。
lsb_release -a  #(适用于所有的linux,包括Redhat、SuSE、Debian等发行版,但是在debian下要安装lsb)

此处内核为red hat,非本内核的请参考官网:https://openresty.org/cn/linux-packages.html

# add the yum repo:*
wget https://openresty.org/package/rhel/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d
  • 执行安装
# update the yum index:* 
sudo yum check-update

#安装openresty
yum install openresty

# 若报错
 Loaded plugins:product-id, subscription-manager 
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process tps://openresty. org/package/centos/sErver/x86 64/repodata/repomd. xml:【Errno 14】 problem making ssl connection Trying other mirror.
Error:Cannot retrieve repository metadata (repomd. xml) for repository:openresty. Please verify its path and try again

错误原因是服务器使用ssl连接不上远程仓库,解决办法为:修改仓库配置,使用非ssl协议的http协议。

执行:
vim /etc/yum.repos.d/openresty.repo
将其中:baseurl中的地址中的https改成http;gpgkey 也顺便改一下,待会安装时按照证书会请求此地址也将报错

安装成功后 会在默认的目录如下:/usr/local/openresty
  • 检查 nginx nginx -t

nginx -v 或者nginx -V

到此, openResty 的nginx已安装完毕, 系统已切换到openResty, 重启(nginx -s reload)nginx读取的将是 openresty目录下的配置文件。

1.3 重启 nginx

1.3.1 指定启动配置文件方式

# conf-path=*path*:  -c file
nginx -s reload -c /etc/nginx/nginx.conf

若执行nginx -s reload重启nginx, 需注意 /usr/local/openresty/nginx/conf/nginx.conf 中的 pid 配置项与历史安装的nginx配置文件中保持一致

1.3.2 读取默认配置文件方式

  • 重新编译openResty
# 切换目录
cd /us/local/openresty-1.17.8.2

# 指定 conf-path 文件
./configure --conf-path=/etc/nginx/nginx.conf

# 安装
gmake
gmake install
  • 验证 nginx -t

  • 重启 nginx -s reload

二、 openResty与nginx切换

  • nginx 安装路径 本测试机安装路径: /usr/local/nginx

2.1 openresty 切到 nginx

  • 检查当前版本

  • 备份openresty nginx
# 切换目录
cd /usr/local/openresty/nginx/sbin

# 备份 nginx 文件
mv nginx nginx.openresty

  • nginx 文件替换
 # 使用已安装的nginx服务下的nginx文件替换调openResty服务下的nginx文件
cp /usr/local/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/

  • 再次检查nginx版本

2.2 nginx 切回 openResty

  • 思路 切回openResty 编译的 nginx 文件即可
  • 操作目录&命令 目录:cd /usr/local/openresty/nginx/sbin
    命令: mv nginx nginx.1.x mv nginx.openresty nginx

  • 检查版本