准备
编译环境
参见《NDK交叉编译OPENSSL编译环境Linux x86_64》中的安装NDK
编译openssl
参见《NDK交叉编译OPENSSL编译环境Linux x86_64》
编译pcre
参见《NDK交叉编译PCRE编译环境Linux x86_64》
开始
下载nginx
cd /home
wget http://nginx.org/download/nginx-1.19.5.tar.gz
tar -xf nginx-1.19.5.tar.gz
cd nginx-1.19.5
编译nginx
由于要修改的地方太多了所以创建了myconfig
文件,内容如下:
#!/bin/sh
HOST=arm-linux-androideabi
CC=arm-linux-androideabi-gcc
ANDROID_API=21
ARCH=arm
NDK=/usr/local/android-ndk-r16b
PCRE=/system
OPENSSL=/system
INCLUDES=/system/include
LIBS="/system/lib/libcrypt.a /system/lib/libglob.a"
# auto/lib/pcre/conf
sed -i "s/\$PCRE\/pcre.h/\$PCRE\/include\/pcre.h/g" auto/lib/pcre/conf
sed -i "s/\$PCRE\/.libs/\$PCRE\/lib/g" auto/lib/pcre/conf
# auto/lib/openssl/conf
sed -i "s/\$OPENSSL\/.openssl/\$OPENSSL/g" auto/lib/openssl/conf
# auto/include
sed -i "s/ngx_test=\"\$CC -o \$NGX_AUTOTEST \$NGX_AUTOTEST.c\"/ngx_test=\"\$CC \$CFLAGS -o \$NGX_AUTOTEST \$NGX_AUTOTEST.c\"/g" auto/include
# auto/cc/name
sed -r -i "/ngx_feature_run=yes/ s/.*/\tngx_feature_run=no/g" auto/cc/name
sed -r -i "/exit 1/ s/.*//1" auto/cc/name
# auto/types/sizeof
sed -r -i "/ngx_size=`$NGX_AUTOTEST`/ s/.*/\tngx_size=4/g" auto/types/sizeof
./configure --prefix=/system \
--builddir=`pwd` \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module \
--without-http_upstream_zone_module \
--with-cc="$CC" \
--with-cc-opt="-D__ANDROID_API__=$ANDROID_API --sysroot=$NDK/platforms/android-$ANDROID_API/arch-$ARCH -isystem $NDK/sysroot/usr/include -isystem $NDK/sysroot/usr/include/$HOST -I$INCLUDES" \
--with-ld-opt="-pie --sysroot=$NDK/platforms/android-$ANDROID_API/arch-$ARCH $LIBS" \
--with-pcre=$PCRE \
--with-openssl=$OPENSSL
sed -i "s/-Werror//g" Makefile
echo "#ifndef NGX_SYS_NERR" >> ngx_auto_config.h
echo "#define NGX_SYS_NERR 233" >> ngx_auto_config.h
echo "#endif" >> ngx_auto_config.h
echo "" >> ngx_auto_config.h
echo "#ifndef NGX_HAVE_MAP_DEVZERO" >> ngx_auto_config.h
echo "#define NGX_HAVE_MAP_DEVZERO 1" >> ngx_auto_config.h
echo "#endif" >> ngx_auto_config.h
DEL_LINE=`sed -n "/build\:/=" Makefile | awk 'END {print}'`
sed -i "${DEL_LINE}d" Makefile
sed -i "${DEL_LINE}d" Makefile
DEL_LINE=`sed -n "/install\:/=" Makefile | awk 'END {print}'`
sed -i "${DEL_LINE}d" Makefile
sed -i "${DEL_LINE}d" Makefile
DEL_LINE=`sed -n "/modules\:/=" Makefile | awk 'END {print}'`
sed -i "${DEL_LINE}d" Makefile
sed -i "${DEL_LINE}d" Makefile
make&&make install
然后运行myconfig
就可以了
chmod +x myconfig
./myconfig
最后
在这里我引入了两个库libcrypt.a和libglob.a,因为这两个库ndk里没有,我是在别的地方找的自己编译的。
libcrypt.a我借鉴的文档为:《linux/android libcrypt移植与实现》
libglob.a我借鉴的资料为:《glob.h and glob.c for the Android NDK》
部分bug的处理借鉴自:《arm linux 移植 Nginx》