光猫折腾4:为光猫编译armel版本的nginx

in 日常 with 0 comment

其实早在上周我就尝试交叉编译nginx了,但是总是遇到各种问题,比如

直接在armhf环境下编译静态链接的nginx

/tmp $ ./nginx
Illegal instruction

在armhf环境下编译带软浮点的nginx

/usr/bin/ld: error: objs/autotest uses vfp register arguments, /tmp/ccauon4m.o does not

好不容易编译了可以用的nginx

~ $ /tmp/nginx
/tmp/nginx: /lib/libcrypt.so.1: version `XCRYPT_2.0' not found (required by /tmp/nginx)
/tmp/nginx: /lib/libc.so.6: version `GLIBC_2.28' not found (required by /tmp/nginx)
/tmp/nginx: /lib/libc.so.6: version `GLIBC_2.32' not found (required by /tmp/nginx)
/tmp/nginx: /lib/libc.so.6: version `GLIBC_2.27' not found (required by /tmp/nginx)
/tmp/nginx: /lib/libc.so.6: version `GLIBC_2.33' not found (required by /tmp/nginx)
/tmp/nginx: /lib/libc.so.6: version `GLIBC_2.34' not found (required by /tmp/nginx)

....

但是功夫不负有心人,终于我成功了

成功

安装交叉编译环境

# 仔细看,不要选择保持未安装状态的方案
aptitude install gcc-arm-linux-gnueabi

接下来,需要编辑apt配置

deb [arch=armel] https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

然后安装 libc6

dpkg --add-architecture
apt update
apt install libc6-dev-armel-cross libstdc++-10-dev-armel-cross aptitude -y

克隆nginx库和依赖

curl https://nginx.org/download/nginx-1.25.4.tar.gz | tar xz
cd nginx-1.25.4
sed -i 's|ngx_feature_run=yes|ngx_feature_run=no|g' auto/cc/name
sed -i 's|ngx_feature_run=yes|ngx_feature_run=no|g' auto/os/linux
sed -i 's|ngx_feature_run=yes|ngx_feature_run=no|g' auto/unix
sed -i 's|ngx_size=`$NGX_AUTOTEST`|ngx_size=4|g' auto/types/sizeof
sed -i '187 a ngx_feature_run=no' auto/cc/conf
sed -i 's|/opt/local|/data/app|g' auto/lib/geoip/conf
sed -i 's|/opt/local|/data/app|g' auto/lib/pcre/conf
sed -i 's|ngx_feature_path=|ngx_feature_path="/data/app/include"|g' auto/lib/zlib/conf
sed -i 's|ngx_feature_libs="-lz"|ngx_feature_libs="-R/data/app/lib -L/data/app/lib -lz"|g' auto/lib/zlib/conf
sed -i 's|/opt/local|/data/app|g' auto/lib/openssl/conf
curl https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.43/pcre2-10.43.tar.gz -L| tar xz
git clone https://github.com/madler/zlib.git

编译

./configure --prefix=/ \
--sbin-path=/usr/local/osgi/bin \
--modules-path=/usr/data/nginx/ \
--conf-path=/usr/data/nginx/nginx.conf \
--error-log-path=/tmp/nginx/error.log \
--pid-path=/tmp/nginx/.pid \
--lock-path=/tmp/nginx/.lock \
--user=www-data \
--group=www-data \
--build="for ARMRounger" \
--without-poll_module \
--without-http_ssi_module \
--without-http_userid_module \
--without-http_mirror_module  \
--without-http_geo_module \
--without-http_map_module \
--without-http_split_clients_module \
--without-http_uwsgi_module \
--without-http_grpc_module \
--without-http_memcached_module \
--without-http_empty_gif_module \
--without-http_browser_module \
--without-http_upstream_hash_module \
--without-http_upstream_ip_hash_module \
--without-http_upstream_least_conn_module \
--without-http_upstream_random_module \
--without-http_upstream_keepalive_module \
--without-http_upstream_zone_module \
--http-log-path=/tmp/nginx/log.log \
--without-pcre2 \
--http-client-body-temp-path=/tmp/nginx/body \
--http-proxy-temp-path=/tmp/nginx/proxy \
--http-fastcgi-temp-path=/tmp/nginx/fcgi \
--http-scgi-temp-path=/tmp/nginx/scgi \
--with-cc=/usr/bin/arm-linux-gnueabi-gcc \
--with-cpp=/usr/bin/arm-linux-gnueabi-g++ \
--with-cc-opt="-mfloat-abi=soft -mthumb -static" \
--with-ld-opt="-static" \
--with-zlib=zlib \
--with-pcre=pcre2-10.43

完成

armbian:objs:# file nginx
nginx: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=854541a4659c5a584993e620d233c2b8aa503a45, for GNU/Linux 3.2.0, with debug_info, not stripped

接下来使用ftp送上门

armbian:objs:# ftp 192.168.1.1
Connected to 192.168.1.1.
220 Welcome to virtual FTP service.
Name (192.168.1.1:root):
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put nginx /nginx
local: nginx remote: /nginx
229 Entering Extended Passive Mode (|||36099|).
150 Ok to send data.
100% |**************************************************************************|  4537 KiB    3.95 MiB/s    00:00 ETA
226 File receive OK.
4646092 bytes sent in 00:01 (3.43 MiB/s)
ftp>

不错哦,可以运行

~ $ cp /mnt/nginx /tmp
~ $ /tmp/nginx
2024/03/10 10:20:56 [emerg] 19363#0: open() "/usr/data/nginx/nginx.conf" failed (2: No such file or directory)

然后献上我编译的nginx,希望帮到不想编译的你

Responses