请问有静态编译 nginx 的吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
1260475686
V2EX    NGINX

请问有静态编译 nginx 的吗?

  •  
  •   1260475686 2021-03-03 10:49:28 +08:00 5275 次点击
    这是一个创建于 1690 天前的主题,其中的信息可能已经有所发展或是发生改变。

    默认编译出的 nginx 可执行文件会依赖编译环境的一些动态库,现在想着编译出来之后可以在不同的设备上部署,是否可以在编译时静态链接,本人尝试使用如下编译参数 ./configure --with-pcre --prefix=/home/nginx-1.16.1/output/ --with-http_ssl_module --with-openssl=/usr/src/openssl-1.0.1g --with-pcre=/home/source_pkt/pcre-8.44 --with-zlib=/home/source_pkt/zlib-1.2.11 --with-cc-opt=-static-libgcc --with-ld-opt='-static' 在编译时加入了-static 但是报错如下: checking for --with-ld-opt="-static" ... not found ./configure: error: the invalid value in --with-ld-opt="-static"

    有没有知道怎么静态编译 nginx 的,谢谢

    22 条回复    2022-05-12 22:01:56 +08:00
    eason1874
        1
    eason1874  
       2021-03-03 13:07:18 +08:00
    问这种问题得说下自己是什么系统环境吧,不然别人比你更加一头雾水。

    可能缺少 glibc,检查下,没有就安装之后再试。最好是用 Docker
    1260475686
        2
    1260475686  
    OP
       2021-03-03 13:10:54 +08:00
    @eason1874 我想问下目前 nginx 支持静态编译不,我的环境是标准的 Centos8
    eason1874
        3
    eason1874  
       2021-03-03 13:20:14 +08:00
    @1260475686 #2 支持啊,网上也挺多分享文章的,你可以搜下看看。

    CentOS 8 到今年 12 月就不维护了,早点弃吧。7 的维护还能支持到 2024 年 6 月
    Kasumi20
        4
    Kasumi20  
       2021-03-03 13:40:26 +08:00
    依赖实在是太多了,make 都难

    不知道 exe 是怎么编译出来的,cygwin 环境下搞过,失败告终。
    官方的 exe 就是静态的
    suconghou
        5
    suconghou  
       2021-03-03 13:52:04 +08:00
    Cooky
        6
    Cooky  
       2021-03-03 13:57:13 +08:00 via Android
    appimage ?
    warcraft1236
        7
    warcraft1236  
       2021-03-03 13:58:53 +08:00
    官方文档下,这个参数是这么用的


    --with-ld-opt=parameters
    sets additional parameters that will be used during linking. When using the system PCRE library under FreeBSD, --with-ld-opt="-L /usr/local/lib" should be specified.
    msg7086
        8
    msg7086  
       2021-03-03 15:03:08 +08:00
    @eason1874 CentOS 8 到期可以换 RHEL 8,OL8,alma,rocky,等等,为什么要弃呢……

    @1260475686 随便搜到了别人的脚本,你可以对照着试验: https://trac.nginx.org/nginx/ticket/903
    1260475686
        9
    1260475686  
    OP
       2021-03-03 16:16:54 +08:00
    @suconghou 这个编译出来的 nginx 不依赖动态库吗?
    1260475686
        10
    1260475686  
    OP
       2021-03-03 16:17:44 +08:00
    @msg7086 https://trac.nginx.org/nginx/ticket/903 这个我看了,加上-static 参数,报错
    suconghou
        11
    suconghou  
       2021-03-03 17:20:40 +08:00
    @1260475686 不依赖, 你可以把镜像里的 nginx 文件复制出来,都能运行
    liuhan907
        12
    liuhan907  
       2021-03-04 01:21:44 +08:00 via Android
    虽然有点文不对题,但你这个需求不就是典型的 docker 场景么?
    1260475686
        13
    1260475686  
    OP
       2021-03-04 10:25:13 +08:00
    @liuhan907 可是我们现在不是为了部署到容器,不过跟 docker 场景还真一个情况,你们有 docker 部署方法吗
    liuhan907
        14
    liuhan907  
       2021-03-04 14:31:46 +08:00 via Android
    @1260475686 部署到容器不是目的是手段啊
    1260475686
        15
    1260475686  
    OP
       2021-03-04 15:22:26 +08:00
    @liuhan907 。。。。。。。。。。。。。
    liuhan907
        16
    liuhan907  
       2021-03-04 20:12:40 +08:00 via Android
    @1260475686
    你这个场景下,直接把程序打包成镜像然后用容器跑不就无关乎系统依赖了?
    szpunk
        17
    szpunk  
       2021-10-27 06:00:42 +08:00
    #Alpine: nginx 1.21.3 + openssl 3.0 static
    ./configure --prefix=/usr/local --conf-path=/root/cfg/nginx.conf --error-log-path=/var/log/nginx.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-openssl-opt="enable-ec_nistp_64_gcc_128 threads zlib sctp" --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_preread_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-pcre --with-libatomic --with-compat --with-file-aio --with-threads --with-poll_module --with-select_module --with-openssl=../openssl-3.0.0 --with-cc-opt="-static -s" --with-ld-opt="-static"

    #Result
    /usr/local/nginx/sbin/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

    #nginx -V
    nginx version: nginx/1.21.3
    built by gcc 10.3.1 20210625 (Alpine 10.3.1_git20210625)
    built with OpenSSL 3.0.0 7 sep 2021
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --error-log-path=/var/log/nginx.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-openssl-opt='enable-ec_nistp_64_gcc_128 threads zlib sctp' --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_preread_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-pcre --with-libatomic --with-compat --with-file-aio --with-threads --with-poll_module --with-select_module --with-openssl=../openssl-3.0.0 --with-cc-opt='-static -s' --with-ld-opt=-static
    guanzhangzhang
        18
    guanzhangzhang  
       2021-12-17 17:44:08 +08:00
    @szpunk 我编译报错
    ```
    checking for --with-ld-opt="-static" ... not found
    ./configure: error: the invalid value in --with-ld-opt="-static"
    ```
    szpunk
        19
    szpunk  
       2021-12-17 23:35:09 +08:00
    @guanzhangzhang #18 确定是在 Alpine 下编译?

    `Linux alpine 5.15.5-0-lts #1-Alpine SMP Fri, 26 Nov 2021 08:35:12 +0000 x86_64 Linux`
    guanzhangzhang
        20
    guanzhangzhang  
       2021-12-18 17:58:11 +08:00
    @szpunk 我是下载
    git clone https://github.com/nginxinc/docker-nginx.git
    ```
    ARG CFLAGS
    ARG LDFLAGS
    ```
    看了下 makefile 的逻辑,传递这俩参数的,然后改 Dockerfile 里的逻辑,不走那个包管理的 case ,走非 x86_64 和非 arm64 的源码编译
    ```
    docker buildx build -t zhangguanzhang/nginx:arm64-static . \
    --platform linux/arm64 --build-arg="CFLAGS='-static -s'" --build-arg=LDFLAGS=-static

    ```
    然后编译就报错上面的
    guanzhangzhang
        21
    guanzhangzhang  
       2022-01-26 16:53:05 +08:00
    1260475686
        22
    1260475686  
    OP
       2022-05-12 22:01:56 +08:00
    @guanzhangzhang 大佬,厉害
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2840 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 08:38 PVG 16:38 LAX 01:38 JFK 04:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86