有无懂哥试过静态编译或者尽可能静态编译 openresty

2022-11-08 20:17:04 +08:00
 guanzhangzhang
FROM alpine AS build

ARG VERSION=openresty-1.21.4.1
ARG ADD_MODULE="\
    https://github.com/openresty/array-var-nginx-module/archive/refs/tags/v0.05.tar.gz \
    https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz \
    https://github.com/openresty/encrypted-session-nginx-module/archive/refs/tags/v0.09.tar.gz \
    https://github.com/calio/form-input-nginx-module/archive/refs/tags/v0.12.tar.gz \
    https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz \
    https://github.com/openresty/memc-nginx-module/archive/refs/tags/v0.19.tar.gz \
    https://github.com/FRiCKLE/ngx_coolkit/archive/refs/tags/0.2.tar.gz \
    https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.2.tar.gz \
    https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.22.tar.gz \
    https://github.com/openresty/lua-upstream-nginx-module/archive/refs/tags/v0.07.tar.gz \
    https://github.com/openresty/stream-lua-nginx-module/archive/refs/tags/v0.0.11.tar.gz \
    https://github.com/openresty/rds-csv-nginx-module/archive/refs/tags/v0.09.tar.gz \
    https://github.com/openresty/rds-json-nginx-module/archive/refs/tags/v0.15.tar.gz \
    https://github.com/openresty/redis2-nginx-module/archive/refs/tags/v0.15.tar.gz \
    https://github.com/openresty/set-misc-nginx-module/archive/refs/tags/v0.33.tar.gz \
    https://github.com/openresty/srcache-nginx-module/archive/refs/tags/v0.32.tar.gz \
    https://github.com/openresty/xss-nginx-module/archive/refs/tags/v0.06.tar.gz \
    "

WORKDIR /opt
RUN if [ -f /etc/apk/repositories ];then sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; fi && \
    if [ -f /etc/apt/sources.list ];then sed -ri 's/(deb|security).debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list; fi && \
    if [ ! -e /etc/nsswitch.conf ];then echo 'hosts: files dns myhostname' > /etc/nsswitch.conf; fi && \
    apk add --no-cache --virtual .build-deps \
                gcc \
                libc-dev \
                make \
                openssl-dev \
                pcre-dev \
                libxml2-dev libxslt-dev \
                libjpeg-turbo-static libpng-static libwebp-static
                zlib-dev \
              openssl-libs-static zlib-static  \
                linux-headers \
                libxslt-dev \
                gd-dev \
                geoip-dev \
                perl-dev \
                libedit-dev \
                bash \
                alpine-sdk \
                findutils
RUN    for m in ${ADD_MODULE};do \
        wget $m -O-| tar zxf -; \
    done && \
    wget https://openresty.org/download/${VERSION}.tar.gz && tar zxf ${VERSION}.tar.gz && \
    rm -f ${VERSION}.tar.gz
RUN cd ${VERSION} && \
    ./configure \
    $(ls -l ../ | awk -vN=${VERSION} '$NF!=N&&$1~/^d/&&$NF~/.+?\.[0-9]+$/{printf " --add-module=../%s ",$NF}') \
    --prefix=/usr/local/openresty/nginx \
    --with-compat \
    --with-file-aio \
    --with-threads \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_geoip_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
#加不上  --with-http_image_filter_module \
    --with-http_mp4_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
#加不上    --with-http_xslt_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-pcre \
    --with-pcre-jit \
    --with-stream \
    --with-stream_realip_module \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-cc-opt='-static -s' \
    --with-ld-opt=-static
RUN cd ${VERSION} && \
    mkdir /install_root && \
    make

上面是我的步骤,但是 make 的时候 ld 报错,最终需求是如果不是静态编译,那把整个目录拷贝到另一个机器上也可以运行,信创的适配要求大概这样。

nginx 静态编译没问题,但是 openresty module 挺多的,有没有老哥知道怎么尽可能的静态编译

2028 次点击
所在节点    Linux
6 条回复
alexsunxl
2022-11-09 10:46:26 +08:00
外面编译好了 ADD 进去呗
lixile
2022-11-09 11:04:45 +08:00
1 、静态编译需要看生成的 makefile configure 不一定原生写好了支持这中编译方式
2 、移动后可运行 需保持通操作系统 (主要是 libstdc++ ld libc ) 可以通过设置 RPATH 实现
注意不是 RUNPATH ! 不是 RUNPATH ! 不是 RUNPATH !
ysc3839
2022-11-09 11:40:57 +08:00
首先避免使用系统包管理安装依赖,比如你给的这段命令中 apk add *-dev 都不能用,需要自己编译对应库的静态版本。其次 glibc 等没法静态链接的库尽量用低版本的环境编译,glibc 是新版本库兼容旧版本的程序的。
boshi
2022-11-09 11:59:16 +08:00
openresty 编译出错大多是版本依赖问题吧。昨天刚编译一个。
```
/configure --prefix=/usr/local/openresty --with-ld-opt='-ljemalloc -Wl,-u,pcre_version' --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1o --with-pcre=../pcre-8.45 --with-pcre-jit --add-module=../ngx_http_geoip2_module-3.4 --add-module=../nginx-rtmp-module-1.2.2 --add-module=../ngx_http_substitutions_filter_module --with-stream_ssl_module --with-stream_ssl_preread_module -j4
```
lc1450
2022-11-09 12:55:47 +08:00
guanzhangzhang
2022-11-09 16:27:28 +08:00
@boshi 我这些模块列表是看官方镜像-V 查到的,所以加了这些模块,不过放弃了,打算扣官方的镜像试试

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/893674

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX