Nginx的交叉编译

Nginx的交叉编译

听说PHP与Nginx是绝配,因此也将Nginx交叉编译到arm平台上来(ubuntu-ports armhf架构)。

1.环境

Host环境:Ubuntu 18.04.5 (PC)

编译工具链:arm-himix200-linux(解包自arm-himix200-linux.tgz,据说来自Hi3516dv300SDK),海思提供的arm编译工具链

环境变量:执行命令:export PATH=/opt/hisi-linux/x86-arm/arm-himix200-linux/bin:$PATH

Nginx目标安装目录:/data/app

编译器arm-himix200-linux默认输出的ELF格式为private flags = 5000200: [Version5 EABI] [soft-float ABI],与ubuntu-armhf的格式private flags = 5000400: [Version5 EABI] [hard-float ABI]不兼容(soft-float ABI与hard-float ABI的传参规则不一样,因此会不兼容)。

通过gcc的print-multi-lib查询

xxx@xxx-HP-ProDesk-480-G5-MT:~/BigEye$ arm-himix200-linux-gcc --print-multi-lib
.;
armv5te_arm9;@mcpu=arm926ej-s
a9;@mcpu=cortex-a9
a7;@mcpu=cortex-a7
a17;@mcpu=cortex-a17
a17_a7;@mcpu=cortex-a17.cortex-a7
a53;@mcpu=cortex-a53
a73;@mcpu=cortex-a73
a73_a53;@mcpu=cortex-a73.cortex-a53
armv5te_arm9_soft;@mcpu=arm926ej-s@mfloat-abi=soft
armv5te_arm9_vfp;@mcpu=arm926ej-s@mfloat-abi=softfp@mfpu=vfp
a9_soft;@mcpu=cortex-a9@mfloat-abi=soft
a9_softfp_vfp;@mcpu=cortex-a9@mfloat-abi=softfp@mfpu=vfp
a9_softfp_vfpv3-d16;@mcpu=cortex-a9@mfloat-abi=softfp@mfpu=vfpv3-d16
a9_vfpv3;@mcpu=cortex-a9@mfloat-abi=softfp@mfpu=vfpv3
a9_vfpv3_neon;@mcpu=cortex-a9@mfloat-abi=softfp@mfpu=neon
a9_hard_neon;@mcpu=cortex-a9@mfloat-abi=hard@mfpu=neon
a7_soft;@mcpu=cortex-a7@mfloat-abi=soft
a7_softfp_vfpv4;@mcpu=cortex-a7@mfloat-abi=softfp@mfpu=vfpv4
a7_softfp_neon-vfpv4;@mcpu=cortex-a7@mfloat-abi=softfp@mfpu=neon-vfpv4
a7_hard_neon-vfpv4;@mcpu=cortex-a7@mfloat-abi=hard@mfpu=neon-vfpv4
a17_soft;@mcpu=cortex-a17@mfloat-abi=soft
a17_softfp_vfpv4;@mcpu=cortex-a17@mfloat-abi=softfp@mfpu=vfpv4
a17_softfp_neon-vfpv4;@mcpu=cortex-a17@mfloat-abi=softfp@mfpu=neon-vfpv4
a17_hard_neon-vfpv4;@mcpu=cortex-a17@mfloat-abi=hard@mfpu=neon-vfpv4
a17_a7_soft;@mcpu=cortex-a17.cortex-a7@mfloat-abi=soft
a17_a7_softfp_vfpv4;@mcpu=cortex-a17.cortex-a7@mfloat-abi=softfp@mfpu=vfpv4
a17_a7_softfp_neon-vfpv4;@mcpu=cortex-a17.cortex-a7@mfloat-abi=softfp@mfpu=neon-vfpv4
a17_a7_hard_neon-vfpv4;@mcpu=cortex-a17.cortex-a7@mfloat-abi=hard@mfpu=neon-vfpv4
a53_soft;@mcpu=cortex-a53@mfloat-abi=soft
a53_softfp_vfpv4;@mcpu=cortex-a53@mfloat-abi=softfp@mfpu=vfpv4
a53_softfp_neon-vfpv4;@mcpu=cortex-a53@mfloat-abi=softfp@mfpu=neon-vfpv4
a53_hard_neon-vfpv4;@mcpu=cortex-a53@mfloat-abi=hard@mfpu=neon-vfpv4
a73_soft;@mcpu=cortex-a73@mfloat-abi=soft
a73_softfp_vfpv4;@mcpu=cortex-a73@mfloat-abi=softfp@mfpu=vfpv4
a73_softfp_neon-vfpv4;@mcpu=cortex-a73@mfloat-abi=softfp@mfpu=neon-vfpv4
a73_hard_neon-vfpv4;@mcpu=cortex-a73@mfloat-abi=hard@mfpu=neon-vfpv4
a73_a53_soft;@mcpu=cortex-a73.cortex-a53@mfloat-abi=soft
a73_a53_softfp_vfpv4;@mcpu=cortex-a73.cortex-a53@mfloat-abi=softfp@mfpu=vfpv4
a73_a53_softfp_neon-vfpv4;@mcpu=cortex-a73.cortex-a53@mfloat-abi=softfp@mfpu=neon-vfpv4
a73_a53_hard_neon-vfpv4;@mcpu=cortex-a73.cortex-a53@mfloat-abi=hard@mfpu=neon-vfpv4

所以,所有库的编译都需要修改默认CFLAGS,添加上:

CFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4"

默认nginx不支持交叉编译,需要对编译脚本进行某些调整,需要去除检测编译器、字节大小时运行临时编译出的程序的步骤,在arm v7上,相应int/long等类型数据大小均为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

2.依赖

Nginx 8.0.12编译时,默认选项,会依赖于如下一些包:zlib、openssl、pcre、libxcrypt、geoip、mailcap等。

下载链接:https://nginx.org/download/nginx-1.20.2.tar.gz

env PKG_CONFIG_LIBDIR=/data/app/lib/pkgconfig/ ./configure --prefix=/data/app --conf-path=/data/app/etc/nginx/nginx.conf --sbin-path=/data/app/bin/nginx --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --user=http --group=http --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --http-client-body-temp-path=/var/lib/nginx/client-body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_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-mail=dynamic --with-mail_ssl_module --with-pcre-jit --with-stream=dynamic --with-stream_geoip_module=dynamic --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc=/opt/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-gcc --with-cpp=/opt/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-cpp --with-cc-opt="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4" --with-ld-opt="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 -Wl,--rpath=/data/app/lib"

make V=1
make DESTDIR=/XXX install

sed -e 's|\<user\s\+\w\+;|user http;|g' -e '44s|html|/data/app/html|' -e '54s|html|/data/app/html|' -i /XXX/data/app/etc/nginx/nginx.conf
rm  /XXX/data/app/etc/nginx/*.default
rm  /XXX/data/app/etc/nginx/mime.types  # in mailcap
install -d /XXX/var/lib/nginx
install -dm700 /XXX/var/lib/nginx/proxy
chmod 755 /XXX/var/log/nginx
chown root:root /XXX/var/log/nginx

执行命令后,将安装Nginx相应的程序、共享库、头文件等至/XXX目录下。

同时需要添加一个服务文件(复制到/usr/lib/systemd/system/目录下):

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target network-online.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
PrivateDevices=yes
SyslogLevel=err

ExecStart=/data/app/bin/nginx -g 'pid /run/nginx.pid; error_log stderr;'
ExecReload=/data/app/bin/nginx -s reload
KillMode=mixed

[Install]
WantedBy=multi-user.target

另外需要添加一个logrotate文件(复制到/etc/logrotate.d/nginx目录下):

/var/log/nginx/*log {
	missingok
	notifempty
	create 640 http log
	sharedscripts
	compress
	postrotate
		test ! -r /run/nginx.pid || kill -USR1 `cat /run/nginx.pid`
	endscript
}

2.1 zlib

下载链接:http://www.zlib.net/zlib-1.2.11.tar.gz

编译命令如下(默认会是O3,建议用O2):

//export CC=arm-himix200-linux-gcc
env CC=arm-himix200-linux-gcc CFLAGS="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 " ./configure --prefix=/data/app
make
make DESTDIR=/XXX install

执行命令后,将安装zlib相应的共享库、头文件等至/XXX目录下。

2.2 openssl

下载链接:https://www.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz

编译命令如下(生成的Makefile不太正确,需要用sed命令修正):

./Configure --prefix=/data/app --openssldir=/data/app/etc/ssl --libdir=lib--with-zlib-include=/data/app/include --with-zlib-lib=/data/app/lib/ no-asm no-async shared zlib-dynamic no-ssl3-method linux-armv4 --cross-compile-prefix=arm-himix200-linux-
sed -i 's|arm-himix200-linux-gcc|gcc|g' Makefile
sed -i 's|-O3|-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4|g' Makefile
sed -i 's|./demoCA|/data/app/etc/ssl|g' apps/CA.pl.in
sed -i 's|./demoCA|/data/app/etc/ssl|g' apps/openssl.cnf
make
make DESTDIR=/XXX install

执行命令后,将安装openssl相应的共享库、头文件等至/XXX目录下。

2.3 pcre

下载链接:https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz

编译命令如下:

env  CFLAGS="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 -I/data/app/include " CXXFLAGS="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 -I/data/app/include " LDFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 -L/data/app/lib" PKG_CONFIG_LIBDIR=/data/app/lib/pkgconfig/ ./configure --prefix=/data/app --disable-static --enable-unicode-properties --enable-pcre16 --enable-pcre32 --enable-jit --enable-pcregrep-libz --enable-pcregrep-libbz2 --host=arm-himix200-linux --target=arm-himix200-linux 
make V=1
make DESTDIR=/XXX install

执行命令后,将安装libpcre2相应的共享库、头文件等至/XXX目录下。

2.4 libxcrypt

下载链接:https://github.com/besser82/libxcrypt/releases/download/v4.4.26/libxcrypt-4.4.26.tar.xz

编译命令如下:

env PKG_CONFIG_LIBDIR=/data/app/lib/pkgconfig/  CFLAGS="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 " ./configure --prefix=/data/app --disable-static --enable-hashes=strong,glibc --enable-obsolete-api=no --disable-failure-tokens --host=arm-himix200-linux --target=arm-himix200-linux 
make V=1
make DESTDIR=/XXX install

执行命令后,将安装libxcrypt相应的共享库、头文件等至/XXX目录下。

2.5 geoip

下载链接:https://github.com/maxmind/geoip-api-c/archive/refs/tags/v1.6.12.tar.gz

编译命令如下:

sed -i 's|AC_FUNC_MALLOC|AC_CHECK_FUNC(malloc, AC_DEFINE(HAVE_MALLOC))|g'    ./configure.ac
sed -i 's|AC_FUNC_REALLOC|AC_CHECK_FUNC(realloc, AC_DEFINE(HAVE_REALLOC))|g' ./configure.ac
./bootstrap
env PKG_CONFIG_LIBDIR=/data/app/lib/pkgconfig/  CFLAGS="-O2 -mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4 " ./configure --prefix=/data/app --disable-static --host=arm-himix200-linux --target=arm-himix200-linux 
make V=1
make DESTDIR=/XXX install

执行命令后,将安装libxcrypt相应的共享库、头文件等至/XXX目录下。

posted @ 2021-11-25 16:16  日月王  阅读(1519)  评论(0编辑  收藏  举报