V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
1260475686
V2EX  ›  NGINX

请问有静态编译 nginx 的吗?

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

    默认编译出的 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   ·   我们的愿景   ·   实用小工具   ·   1024 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:40 · PVG 03:40 · LAX 12:40 · JFK 15:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.