Centos下openresty-1.19.9-1环境搭建

Centos下openresty-1.19.9-1环境搭建

开发测试需要用到nginx作为服务端,这里选择直接用openresty(nginx+lua)来搭建更加便利。
查看了不少的openrest安装帖子,发现不少帖子都因为版本比较旧,和新版本差异较大,在安装过程中遇到一些坑,因此把自己的安装过程mark下以供参考。

一、背景

搭建线上测试源站,支持下列功能:
支持自定义头部添加
支持POST并且保存文件
支持ssl、http/2
支持自定义状态码
致辞post body校验
支持chunked编码的post文件保存

涉及第三方模块:
自定义头部模块:headers-more-nginx-module
POST模块:lua-resty-upload (内置于OpenResty中)
lua模块:lua-nginx-module(openresty本身是有内置,额外安装模块提供更加强大的lua支持)
upload模块:nginx-upload-module

系统版本:CentOS 6.5

二、openresty安装配置

下面所有的安装包都可以通过官网或者github获取,openresty官网:OpenResty® - 中文官方站
openresty版本:openresty-1.19.9.1.tar.gz

openssl版本:openssl-OpenSSL_1_1_1h.tar.gz

upload模块:nginx_upload_module-2.2.0.tar.gz

自定义头部:headers-more-nginx-module-0.33.tar.gz

lua模块:lua-nginx-modulev0.10.20.tar.gz

备注】不要急急忙忙去下载安装包,先把安装部分看完会省不少事哦

2.1 编译安装

开始安装openresty之前需要先安装依赖环境,相关帖子很多,这里就不赘述
可以参考:CentOS 平台 · OpenResty最佳实践 (gitbooks.io)

【编译配置】:采用默认安装路径 /usr/local/openresty

# cd openresty-1.19.9.1
#./configure --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-pcre --with-http_ssl_module --with-openssl=/root/openssl-1.1.1h --with-http_gunzip_module --with-http_v2_module --with-http_auth_request_module --with-http_random_index_module --with-http_slice_module --with-http_perl_module --with-stream_ssl_preread_module --with-http_addition_module 

// 配置成功后执行编译和安装
#gmake
#gmake install

备注:–with-ipv6 –with-luajit 选项在新版本中默认支持不再需要额外开启

安装成功查看当前版本信息:

# ./nginx -V
nginx version: openresty/1.19.9.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.1.1h  22 Sep 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.20 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.10 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-pcre --with-http_ssl_module --with-openssl=/root/openssl-1.1.1h --with-http_gunzip_module --with-http_v2_module --with-http_auth_request_module --with-http_random_index_module --with-http_slice_module --with-http_perl_module --with-stream_ssl_preread_module --with-http_addition_module --with-openssl-opt=-g --with-stream --with-stream_ssl_module

备注

​ 开始想要–add-module 自己下载ngx_devel_kit-0.3.1 、headers-more-nginx-module-0.33模块,编译中提示很多文件多次定义冲突,后面在一个帖子上看到openresty源码解压后bundle目录下已经带有不少第三方模块源码了
如:headers-more-nginx-module-0.33 、ngx_lua-0.10.20、ngx_devel_kit-0.3.1、lua-resty-upload-0.10

​ 如果是下载的近期新版本的安装包,很多第三方模块都默认安装了,不需要自己添加,先确认bundle目录下没有自己需要的模块或版本再采用–add-module添加到configure配置

2.2 配置环境变量

为了后面启动 OpenResty 的命令简单一些,不用在 OpenResty 的安装目录下进行启动,我们设置环境变量来简化操作。 将 nginx 目录添加到 PATH 中。打开文件 /etc/profile, 在文件末尾加入export PATH=$PATH:/opt/openresty/nginx/sbin

# vim /etc/profile
export PATH=$PATH:/opt/openresty/nginx/sbin  # 添加

若你的安装目录不一样,则做相应修改。 注意:这一步操作需要重新加载环境变量才会生效,可通过命令source /etc/profile或者重启服务器等方式实现。

之后可以在任意位置执行nignx命令:

(1)启动
# nginx  // 单独一个nginx即可
# ps -ef| grep nginx   // 查看nginx是否成功启动    
(2)重新加载配置
# nginx -s reload
(3)退出
# nginx -s stop    
(4)查看状态
# nginx -s status    

更多命令可以执行nginx -help查看

备注】个人认为如果不需要设置开机启动nginx,那到这一步就可以了,可以跳过2.4添加到service服务的步骤

2.3 添加lua的http模块

lua-resty-http 是lua实现的http客户端,支持nginx中发起http请求

将http.lua、http_headers.lua、lua-resty-http-master文件上传到/usr/local/nginx/lualib/resty/路径下
注意:若没有以上模块,Nginx配置文件中的lua命令执行会报错

依赖:lua-resty-http
可直接下载http.lua/http_headers.lua放到/usr/local/openresty/lualib/resty/目录下即可

如下载:lua-resty-http-0.16.1.tar.gz

http_connect.lua
http_headers.lua
http.lua

解压后将上述三个文件拷贝到/usr/local/openresty/lualib/resty/目录下

参考:如何引用第三方 resty 库 · OpenResty最佳实践 (gitbooks.io)

2.4 添加到service服务

如果没有将nginx添加到service服务每次都需要在/usr/local/openresty/nginx/sbin下执行相关命令比较麻烦。

nginx源码安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建nginx文件,并更改权限其即可。

# vim /etc/init.d/nginx   // 根据实际目录修改
#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# this script create it by caffreyxin at 2007.10.15.

# it is v.0.0.1 version.

# if you find any errors on this scripts, please contact caffreyxin.

# and send mail to xinyflove at sina dot com.

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/openresty/nginx/sbin/nginx               # 根据本机实际情况修改
nginx_config=/usr/local/openresty/nginx/conf/nginx.conf    # 根据本机实际情况修改
nginx_pid=/var/run/nginx.pid
    
RETVAL=0
prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.

start() {

    if [ -e $nginx_pid ];then
        echo "nginx already running...."
        exit 1
    fi
    
    echo -n $"Starting $prog: "
    daemon $nginxd -c ${nginx_config}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    return $RETVAL

}


# Stop nginx daemons functions.

stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}


# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo

}

# See how we were called.

case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac

exit $RETVAL
  • 修改文件权限
# chmod +x /etc/init.d/nginx
  • 添加开机启动
# /sbin/chkconfig --level 345 /etc/init.d/nginx on
  • 查看nginx是否已经添加到开机启动项
# chkconfig --list  // 如果成功添加会在列表中看到已经包含了nginx

设置完成后验证能够在任何位置运行service 命令执行 nginx start | stop | restart | reload | status | help 命令

# service nginx status
nginx (pid 19726 19725 17169 10120) is running...
# service nginx stop
Stopping nginx:                                            [  OK  ]
# service nginx start
Starting nginx:                                            [  OK  ]
# ps -ef |grep nginx
root     10811     1  0 15:18 ?        00:00:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
nobody   10812 10811  0 15:18 ?        00:00:00 nginx: worker process                                                              
nobody   10813 10811  0 15:18 ?        00:00:00 nginx: worker process                                                              
nobody   10814 10811  0 15:18 ?        00:00:00 nginx: worker process                                                              
root     11513 20070  0 15:18 pts/0    00:00:00 grep nginx

至此openresty安装完成,根据需要编辑nginx.conf修改配置,每次修改后执行service nginx reload命令重新加载即可。

  • over
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值