centos离线安装openresty

找一台机器可以访问外网的机器,通过yum 下载相应的依赖包,使用docker启动一个centos6.8的容器,如果机器是新服务器,安装脚本直接使用,如果是有其他服务及业务的服务器,在运行安装脚本前去掉强制安装更新依赖。

一、准备依赖包及openresty包

# docker run -d -it --name=centos6.8 centos:6.8
# docker exec -it centos6.8 bash
# mkdir /usr/local/src/nginx_packages
# yum install --downloadonly --downloaddir=/usr/local/src/nginx_packages/ gcc gcc-c++ curl perl postgresql-devel openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
# yum install wget -y
# cd /usr/local/src/nginx_packages && wget https://openresty.org/download/openresty-1.15.8.2.tar.gz

二、openresty服务启动脚本:

# cat /usr/local/src/nginx_packages/openresty

#!/bin/sh
#
# openresty - this script starts and stops the nginx daemon of OpenResty
#
# chkconfig:   345 85 15
# description: OpenResty is a scalable web platform by extending
#              NGINX with Lua
# processname: openresty
# config:      /usr/local/openresty/nginx/conf/nginx.conf
# config:      /etc/sysconfig/openresty
# pidfile:     /usr/local/openresty/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/openresty/nginx/sbin/nginx"
prog=$(basename $nginx)
pidfile=/usr/local/openresty/nginx/logs/nginx.pid

NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/openresty ] && . /etc/sysconfig/openresty

lockfile=/var/lock/subsys/openresty

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    $nginx -q -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $nginx
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

三、openresty安装脚本:

# cat /usr/local/src/nginx_packages/install_nginx.sh

#!/bin/bash
nginx_packages_path=$(dirname $(readlink -f "$0"))
nginx_package_name='openresty-1.15.8.2.tar.gz'
nginx_dir_name=`echo $nginx_package_name |awk -F'.tar.gz' '{print $1}'`

install_nginx(){
    cd $nginx_packages_path
    rpm -Uvh ./*.rpm --nodeps --force
    tar xvf $nginx_package_name && cd $nginx_dir_name
    ./configure --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module && make && make install
}

nginx_system_service(){
    cd $nginx_packages_path
    cp ./openresty /etc/init.d/ && chmod +x /etc/init.d/openresty && chkconfig --add openresty && chkconfig openresty on && service openresty start
}
install_nginx
nginx_system_service

四、依赖包、服务脚本和安装脚本打包

# cd /usr/local/src && tar czvf nginx_packages.tar.gz nginx_packages
# docker cp centos6.8:/usr/local/src/nginx_packages.tar.gz ./

五、文件包拷贝及运行安装脚本

文件最好放到/usr/local/src目录下

# cd /usr/local/src && tar xvf nginx_packages.tar.gz 
# cd nginx_packages && bash install_nginx.sh