# 1.21.4.3

# 0. 资源准备

包名/模块 作用
openresty (opens new window) openresty 本体
nginx-sticky-module-ng (opens new window) 根据 cookie 转发到同一 upstream
nginx-module-vts (opens new window) 查看 nginx 状态
nginx_upstream_check_module (opens new window) 检查 upstream
pcre (opens new window) 正则匹配
openssl (opens new window) 加解密套件
zlib (opens new window) 传输内容 gzip 压缩
# 本次编译使用的资源和版本
# nginx_upstream_check_module-0.4.0.tar.gz
# openresty-1.21.4.3.tar.gz
# openssl-1.1.1w.tar.gz
# zlib-1.3.tar.gz
# nginx-module-vts-master.zip
# pcre-8.45.zip
# nginx-sticky-module-ng-master.zip
# 解压所有资源
find ./ -name "*.tar.gz" | xargs -I {} tar xzvf {}
find ./ -name "*.zip" | xargs -I {} unzip {}
  • openresty 的编译顺序为 模块 -> nginx -> 链接

  • 为了静态编译 openresty,重点要注意 --prefix=.指定 pcre opensll zlib 模块

# 1. 编译 LuaJIT

OPENRESTY_VERSION=1.21.4.3
NGINX_VERSION=1.21.4
cd openresty-${OPENRESTY_VERSION}
cd bundle/LuaJIT-* && make install -j$(grep -c ^processor /proc/cpuinfo) PREFIX=`pwd` && LUAROOT=`pwd` && rm -rf lib/*.so* && cd ../..

# 2. nginx-module-vts patch

nginx-module-vts 和 nginx_upstream_check_module 有冲突,需要打补丁

cd bundle/nginx-${NGINX_VERSION}
patch -p1 < ../../../nginx_upstream_check_module-0.4.0/check_1.20.1+.patch
cd ../../

# 3. 编译 openresty

./configure \
    -j$(grep -c ^processor /proc/cpuinfo)\
    --prefix=.\
    --with-cc-opt="-O2"\
    --with-luajit=${LUAROOT}\
    --with-http_v2_module\
    --with-pcre-jit\
    --with-http_stub_status_module\
    --with-http_ssl_module\
    --with-http_sub_module\
    --with-stream\
    --with-stream_ssl_module\
    --with-stream_ssl_preread_module\
    --add-module=../nginx-module-vts-master\
    --add-module=../nginx_upstream_check_module-0.4.0\
    --add-module=../nginx-sticky-module-ng-master\
    --with-zlib=../zlib-1.3\
    --with-pcre=../pcre-8.45\
    --with-openssl=../openssl-1.1.1w\
    --with-http_iconv_module


gmake -j$(grep -c ^processor /proc/cpuinfo)
gmake install
mkdir -p openresty_${OPENRESTY_VERSION}
cp nginx/ lualib/ openresty_${OPENRESTY_VERSION}/ -r
tar czvf openresty_${OPENRESTY_VERSION}.tar.gz openresty_${OPENRESTY_VERSION}

注意:

  • 运行时需要注意 工作目录从 `pwd` 到 nginx 的路径一定要为 ./nginx/sbin/nginx,nginx 可执行文件依靠工作目录寻找 LuaJIT 运行库。

  • 此方法编译出的 openresty 依赖编译机器使用的 glibc,需要确保使用 openresty 的主机有同样版本的 glibc

# 4. 安装

将 openresty_${OPENRESTY_VERSION}.tar.gz 复制到目标主机任意目录并解压

INSTALL_DIR="/opt"
OPENRESTY_VERSION=1.21.4.3

# 解压本体
tar xzvf openresty_${OPENRESTY_VERSION}.tar.gz

# 配置 logrotate
cat << EOF > /etc/logrotate.d/openresty_${OPENRESTY_VERSION}
${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/logs/access.log
${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/logs/error.log
{
    daily
    rotate 15
    copytruncate
    missingok
    dateext
    compress
    notifempty
}
EOF

# 配置 systemd
cat << EOF > /etc/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
WorkingDirectory=${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}
PIDFile=${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/logs/nginx.pid
ExecStartPre=${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/sbin/nginx -t
ExecStart=${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/sbin/nginx
ExecReload=${INSTALL_DIR}/openresty_${OPENRESTY_VERSION}/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

参考:

  1. 编译 可移动、静态链接的 OpenResty (opens new window)
  2. nginx-module-vts 和 nginx_upstream_check_module 冲突解决 (opens new window)
最后编辑于: 2/18/2024, 3:44:15 PM