您的位置:首页 > 运维架构 > Nginx

cross compile nginx+openssl+curl

2017-07-04 20:52 357 查看
开发环境:

$uname -a
Linux ubuntu 3.2.0-29-generic-pae #46-Ubuntu SMP Fri Jul 27 17:25:43 UTC 2012 i686 i686 i386 GNU/Linux

$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.1 LTS
Release:        12.04
Codename:       precise
编译器:
$ arm-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabi/4.6/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/arm-linux-gnueabi/include/c++/4.6.3 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-float=softfp --with-fpu=vfpv3-d16 --with-mode=thumb --disable-werror --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --includedir=/usr/arm-linux-gnueabi/include --with-headers=/usr/arm-linux-gnueabi/include --with-libs=/usr/arm-linux-gnueabi/lib
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

Nginx: nginx-1.10.0
Openssl: openssl-1.0.1c(有漏洞: heartbleed) 换用openssl-1.0.1g


下载curl:curl-7.54.1

./configure --host=arm-linux CC=arm-linux-gnueabi-gcc CROSS_COMPILE=arm-linux-gnueabi- --without-zlib --disable-ldap --disable-ldaps
make
find ./ -name *.a
./lib/.libs/libcurl.a
cp  ./lib/.libs/libcurl.a /usr/arm-linux-gnueabi/lib/ (或sdk/lib/arm-gnueabi/)


下载openssl

编译安装 nginx 时,默认使用系统自带的 OpenSSL 库,但其一般很老,不支持如 SDPY 等新功能。–with-openssl 参数虽然可以指定 OpenSSL 路径,但只支持 OpenSSL 的源代码,不支持已编译好的 OpenSSL。每回更新 nginx 都要重新编译 OpenSSL 肯定很麻烦,网上找到一个方案,觉得很好,记录下来。

1.1 首先使用交叉编译Openssl:

https://www.openssl.org/source/openssl-1.0.1g.tar.gz
export CC=arm-linux-gnueabi-gcc
./config no-asm shared --prefix=/app/my_lib


1.2 修改nginx代码:

把
31#CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
32#CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
33#CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
34#CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
35#CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
改为:
37CORE_INCS="$CORE_INCS $OPENSSL/include"
38CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
39CORE_LIBS="$CORE_LIBS $OPENSSL/libssl.a"
40CORE_LIBS="$CORE_LIBS $OPENSSL/libcrypto.a"
41CORE_LIBS="$CORE_LIBS $NGX_LIBDL"


交叉编译nginx:

2.1 下载nginx代码并解压

2.2 修改nginx

2.2.1 vim auto/cc/name
去掉   exit 1
2.2.2 vim auto/types/sizeof
ngx_size=4


2.3 执行configure,生成Makefile

./configure
--prefix=/data/
--with-cc=/usr/bin/arm-linux-gnueabi-gcc
--with-ld-opt="-L/sdk/lib/arm-gnueabi/依赖的动态库"
--with-stream
--with-stream_ssl_module
--with-openssl=/home/openssl-1.0.1c


2.4 make

2.4.1 出现error: glob.h No such file or dir

./objs/Makefile
CFLAGS += -I /usr/include    (/usr/include/glob.h)


2.4.2

src/os/unix/ngx_errno.c: In function ‘ngx_strerror’:
src/os/unix/ngx_errno.c:37:31: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)
src/os/unix/ngx_errno.c:37:31: note: each undeclared identifier is reported only once for each function it appears in
src/os/unix/ngx_errno.c: In function ‘ngx_strerror_init’:
src/os/unix/ngx_errno.c:58:11: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)


方法:

$ vim objs/ngx_auto_config.h
8 #ifndef NGX_SYS_NERR
9 #define NGX_SYS_NERR  132
10 #endif


2.4.3

bjs/src/core/ngx_cycle.o: In function `ngx_init_cycle':
/src/core/ngx_cycle.c:472: undefined reference to `ngx_shm_free'
/src/core/ngx_cycle.c:477: undefined reference to `ngx_shm_alloc'
/src/core/ngx_cycle.c:674: undefined reference to `ngx_shm_free'
objs/src/event/ngx_event.o: In function `ngx_event_module_init':
/src/event/ngx_event.c:507: undefined reference to `ngx_shm_alloc'


方法:

$ vim objs/ngx_auto_config.h
12
13 #ifndef NGX_HAVE_SYSVSHM
14 #define NGX_HAVE_SYSVSHM  1
15 #endif


2.4.4

/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-timeval.o): In function `curlx_tvnow':
timeval.c:(.text+0xa): undefined reference to `clock_gettime'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-version.o): In function `curl_version':
version.c:(.text+0x22): undefined reference to `zlibVersion'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-version.o): In function `curl_version_info':
version.c:(.text+0x56): undefined reference to `zlibVersion'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-content_encoding.o): In function `inflate_stream':
content_encoding.c:(.text+0x8a): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x98): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0xb6): undefined reference to `inflate'
content_encoding.c:(.text+0x106): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x13a): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x14c): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x16e): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x184): undefined reference to `inflateEnd'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-content_encoding.o):content_encoding.c:(.text+0x190): more undefined references to `inflateEnd' follow
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-content_encoding.o): In function `Curl_unencode_deflate_write':
content_encoding.c:(.text+0x274): undefined reference to `inflateInit_'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-content_encoding.o): In function `Curl_unencode_gzip_write':
content_encoding.c:(.text+0x356): undefined reference to `zlibVersion'
content_encoding.c:(.text+0x36c): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x398): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x3be): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x436): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x486): undefined reference to `inflateEnd'
/ /sdk/lib/arm_telit/libcurl.a(libcurl_la-content_encoding.o): In function `Curl_unencode_cleanup':
content_encoding.c:(.text+0x4b6): undefined reference to `inflateEnd'
collect2: ld returned 1 exit status
make[1]: *** [objs/nginx] Error 1
make[1]: Leaving directory ` / '
make: *** [build] Error 2


办法:

$ vim objs/Makefile
-lidn -lrt -lcrypto -lssl


2.4.5

/usr/bin/ld: cannot find -lidn


方法:

下载:libidn-1.15
export CC=arm-linux-gnueabi-gcc
./configure --host=arm-linux
make
find ./ -name *.a
./lib/.libs/libidn.a
cp ./lib/.libs/libidn.a /usr/arm-linux-gnueabi/lib/ 或sdk/lib/arm-gnueabi/


由于在arm开发板上没有放置动态库的权限,因此编译nginx链接静态库。

所有需要的静态库:

sdk
└── lib
└── arm_telit
├── libcrypt.a
├── libcrypto.a
├── libcurl.a
├── libidn.a
└── libssl.a


更新:使libcurl支持openssl

openssl编译:
####@ubuntu:~/111/openssl-1.0.1g$ export CC=arm-linux-gnueabi-gcc
####@ubuntu:~/111/openssl-1.0.1g$ ./config no-asm shared --prefix=/app/lib/openssl
make
sudo make install

curl编译:
####@ubuntu:~/111/111-Client-test/curl-7.54.1$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app/lib/openssl/lib
####@ubuntu:~/111/111-Client-test/curl-7.54.1$ ./configure --prefix=/app/lib/curl --host=arm-linux CC=arm-linux-gnueabi-gcc CROSS_COMPILE=arm-linux-gnueabi-   --disable-ldap --disable-ldaps --with-ssl=/app/lib/openssl

发现原来不支持openssl的curl,开始支持openssl

make
make install

nginx编译:
./configure --prefix=/cache/111-Client --with-cc=/usr/bin/arm-linux-gnueabi-gcc  --with-cc-opt=-static --with-ld-opt="-lcurl -lidn -lcrypto -lssl -lrt -ldl -lz -static -L/home/####/111/111-Client-test/111-Client_BK/Trunk/sdk/lib/arm_telit_a/lib -L/home/####/111/111-Client-test/111-Client_BK/Trunk/sdk/lib/arm_telit_a/curl/lib" --with-stream --with-stream_ssl_module --with-openssl=/home/####/111/openssl-1.0.1g


更新过程中出现的问题:

1:src/os/unix/ngx_errno.c: In function ‘ngx_strerror’:
src/os/unix/ngx_errno.c:37:31: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)
src/os/unix/ngx_errno.c:37:31: note: each undeclared identifier is reported only once for each function it appears in
src/os/unix/ngx_errno.c: In function ‘ngx_strerror_init’:
src/os/unix/ngx_errno.c:58:11: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)

解决办法:
vim objs/ngx_auto_config.h
在文件开始处添加如下代码
#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR 132
#endif

#ifndef NGX_HAVE_SYSVSHM
#define NGX_HAVE_SYSVSHM 1
#endif


2:libcurl.a(libcurl_la-version.o): In function `curl_version':
version.c:(.text+0x4e): undefined reference to `zlibVersion'
libcurl.a(libcurl_la-version.o): In function `curl_version_info':
version.c:(.text+0x92): undefined reference to `zlibVersion'
libcurl.a(libcurl_la-content_encoding.o): In function `inflate_stream':
content_encoding.c:(.text+0x8a): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x98): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0xb6): undefined reference to `inflate'
content_encoding.c:(.text+0x106): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x13a): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x14c): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x16e): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x184): undefined reference to `inflateEnd'
libcurl.a(libcurl_la-content_encoding.o):content_encoding.c:(.text+0x190): more undefined references to `inflateEnd' follow
libcurl.a(libcurl_la-content_encoding.o): In function `Curl_unencode_deflate_write':
content_encoding.c:(.text+0x274): undefined reference to `inflateInit_'
libcurl.a(libcurl_la-content_encoding.o): In function `Curl_unencode_gzip_write':
content_encoding.c:(.text+0x356): undefined reference to `zlibVersion'
content_encoding.c:(.text+0x36c): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x398): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x3be): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x436): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x486): undefined reference to `inflateEnd'

adding: -lcurl -lidn -ldl -lz –static
nginx在编译时添加:
--with-ld-opt="-lcurl -lidn -lcrypto -lssl -lrt -ldl -lz -static -L/app/lib/openssl/lib -L/app/lib/curl/lib"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: