请问有静态编译 nginx 的吗?

2021-03-03 10:49:28 +08:00
 1260475686

默认编译出的 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 的,谢谢

4304 次点击
所在节点    NGINX
22 条回复
eason1874
2021-03-03 13:07:18 +08:00
问这种问题得说下自己是什么系统环境吧,不然别人比你更加一头雾水。

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

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

不知道 exe 是怎么编译出来的,cygwin 环境下搞过,失败告终。
官方的 exe 就是静态的
suconghou
2021-03-03 13:52:04 +08:00
Cooky
2021-03-03 13:57:13 +08:00
appimage ?
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
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
2021-03-03 16:16:54 +08:00
@suconghou 这个编译出来的 nginx 不依赖动态库吗?
1260475686
2021-03-03 16:17:44 +08:00
@msg7086 https://trac.nginx.org/nginx/ticket/903 这个我看了,加上-static 参数,报错
suconghou
2021-03-03 17:20:40 +08:00
@1260475686 不依赖, 你可以把镜像里的 nginx 文件复制出来,都能运行
liuhan907
2021-03-04 01:21:44 +08:00
虽然有点文不对题,但你这个需求不就是典型的 docker 场景么?
1260475686
2021-03-04 10:25:13 +08:00
@liuhan907 可是我们现在不是为了部署到容器,不过跟 docker 场景还真一个情况,你们有 docker 部署方法吗
liuhan907
2021-03-04 14:31:46 +08:00
@1260475686 部署到容器不是目的是手段啊
1260475686
2021-03-04 15:22:26 +08:00
@liuhan907 。。。。。。。。。。。。。
liuhan907
2021-03-04 20:12:40 +08:00
@1260475686
你这个场景下,直接把程序打包成镜像然后用容器跑不就无关乎系统依赖了?
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
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
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
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

```
然后编译就报错上面的

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

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

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

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

© 2021 V2EX