RK平台之nginx移植

1 下载源码并解压

wget http://nginx.org/download/nginx-1.12.2.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1h.tar.gz
wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.bz2
wget https://www.zlib.net/fossils/zlib-1.2.11.tar.gz

2 步骤

2.1 设置交叉编译环境
$ vim ~/.bashrc
...
export PATH=/opt/zlg/m1808-sdk-v1.6.0-ga/host/bin:$PATH
export CC=aarch64-linux-gnu-gcc

$ source ~/.bashrc
$ aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/zlg/m1808-sdk-v1.6.0-ga_2021.07.16/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/lto-wrapper
Target: aarch64-linux-gnu
Configured with: '/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/snapshots/gcc.git~linaro-6.3-2017.05/configure' SHELL=/bin/bash --with-mpc=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-mpfr=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gmp=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gnu-as --with-gnu-ld --disable-libmudflap --enable-lto --enable-shared --without-included-gettext --enable-nls --disable-sjlj-exceptions --enable-gnu-unique-object --enable-linker-build-id --disable-libstdcxx-pch --enable-c99 --enable-clocale=gnu --enable-libstdcxx-debug --enable-long-long --with-cloog=no --with-ppl=no --with-isl=no --disable-multilib --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419 --with-arch=armv8-a --enable-threads=posix --enable-multiarch --enable-libstdcxx-time=yes --enable-gnu-indirect-function --with-build-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/sysroots/aarch64-linux-gnu --with-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu/aarch64-linux-gnu/libc --enable-checking=release --disable-bootstrap --enable-languages=c,c++,fortran,lto --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=aarch64-linux-gnu --prefix=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu
Thread model: posix
gcc version 6.3.1 20170404 (Linaro GCC 6.3-2017.05) 

2.2 修改nginx编译配置

nginx本身并不支持交叉编译,因此需要对其进行一定的修改。(具体修改看m1808-nginx-with-rtmp.patch里面的内容)

将m1808-nginx-with-rtmp.patch文件拷贝到解压后的nginx-1.12.2目录,并进入nginx-1.12.2源码目录,将m1808-nginx-with-rtmp.patch补丁打上

$ cd nginx-1.12.2/
$ patch -p1 <m1808-nginx-with-rtmp.patch

file: m1808-nginx-with-rtmp.patch

yanz@yanz-virtual-machine:~/opt/nginx-rtmp-build/nginx-1.12.2$ cat m1808-nginx-with-rtmp.patch 
diff --git a/auto/feature b/auto/feature
index 3561f59..bc43b6f 100644
--- a/auto/feature
+++ b/auto/feature
@@ -53,7 +53,7 @@ if [ -x $NGX_AUTOTEST ]; then
 
         yes)
             # /bin/sh is used to intercept "Killed" or "Abort trap" messages
-            if /bin/sh -c $NGX_AUTOTEST >> $NGX_AUTOCONF_ERR 2>&1; then
+#            if /bin/sh -c $NGX_AUTOTEST >> $NGX_AUTOCONF_ERR 2>&1; then
                 echo " found"
                 ngx_found=yes
 
@@ -61,9 +61,9 @@ if [ -x $NGX_AUTOTEST ]; then
                     have=$ngx_have_feature . auto/have
                 fi
 
-            else
-                echo " found but is not working"
-            fi
+#            else
+#                echo " found but is not working"
+#            fi
         ;;
 
         value)
diff --git a/auto/types/sizeof b/auto/types/sizeof
index 480d8cf..61dcd2d 100644
--- a/auto/types/sizeof
+++ b/auto/types/sizeof
@@ -14,7 +14,7 @@ END
 
 ngx_size=
 
-cat << END > $NGX_AUTOTEST.c
+cat << _EOF > $NGX_AUTOTEST.c
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -25,22 +25,41 @@ $NGX_INCLUDE_UNISTD_H
 $NGX_INCLUDE_INTTYPES_H
 $NGX_INCLUDE_AUTO_CONFIG_H
 
-int main(void) {
-    printf("%d", (int) sizeof($ngx_type));
+#if !defined( PASTE)
+#define PASTE2( x, y) x##y
+#define PASTE( x, y)  PASTE2( x, y)
+#endif /* PASTE */
+
+#define SAY_IF_SIZEOF( typename, type, size)   \\
+    static char PASTE( PASTE( PASTE( sizeof_, typename), _is_), size) \\
+    [(sizeof(type) == (size)) ? 1 : -1]
+
+SAY_IF_SIZEOF(TEST_TYPENAME, TEST_TYPE, TEST_SIZE);
+
+int main(void)
+{
     return 0;
 }
 
-END
+_EOF
 
 
-ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
-          -o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs"
+_ngx_typename=`echo "$ngx_type" | sed 's/ /_/g;s/\*/p/'`
+ngx_size="-1"
+ngx_size=`for i in 1 2 4 8 16 ; do \
+    $CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
+    -DTEST_TYPENAME="$_ngx_typename" -DTEST_TYPE="$ngx_type" -DTEST_SIZE="$i" \
+    $NGX_AUTOTEST.c -o $NGX_AUTOTEST \
+    $NGX_LD_OPT $ngx_feature_libs >/dev/null 2>&1 || continue ;\
+    echo $i ; break ; done`
 
-eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
+rm -rf $NGX_AUTOTEST*
 
+if test -z $ngx_size ; then
+    ngx_size=-1
+fi
 
-if [ -x $NGX_AUTOTEST ]; then
-    ngx_size=`$NGX_AUTOTEST`
+if [ $ngx_size -gt 0 ]; then
     echo " $ngx_size bytes"
 fi
 
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
index 7756370..d086f37 100644
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -4,7 +4,7 @@
  * Copyright (C) Nginx, Inc.
  */
 
-
+#define _GNU_SOURCE
 #include <ngx_config.h>
 #include <ngx_core.h>
 #include <ngx_event.h>
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
index 7d6ca76..06f1cb4 100644
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -54,6 +54,7 @@ typedef int               ngx_err_t;
 #define NGX_ENOMOREFILES  0
 #define NGX_ELOOP         ELOOP
 #define NGX_EBADF         EBADF
+#define NGX_SYS_NERR			1
 
 #if (NGX_HAVE_OPENAT)
 #define NGX_EMLINK        EMLINK
diff --git a/src/os/unix/ngx_time.h b/src/os/unix/ngx_time.h
index c128c9a..cb947da 100644
--- a/src/os/unix/ngx_time.h
+++ b/src/os/unix/ngx_time.h
@@ -8,7 +8,7 @@
 #ifndef _NGX_TIME_H_INCLUDED_
 #define _NGX_TIME_H_INCLUDED_
 
-
+#include <time.h>
 #include <ngx_config.h>
 #include <ngx_core.h>

2.3 新建config.sh脚本,添加configure配置项

$ vim config.sh
./configure \                                                                             
    --crossbuild=Linux::aarch64 \
    --with-ld-opt= \
    --with-ipv6 \
    --prefix=../nginx-install/ \
    --conf-path=/etc/nginx/nginx.conf \
    --sbin-path=/usr/sbin/nginx \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/lock/nginx.lock \
    --user=www-data \
    --group=www-data \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --http-client-body-temp-path=/var/tmp/nginx/client-body \
    --http-proxy-temp-path=/var/tmp/nginx/proxy \
    --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
    --http-scgi-temp-path=/var/tmp/nginx/scgi \
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    --with-threads \
    --with-pcre=../pcre-8.41 \
    --with-openssl=../openssl-1.1.1h \
    --with-openssl-opt="no-shared linux-armv4 --cross-comile-prefix=aarch64-linux-gnu- " \
    --with-zlib=../zlib-1.2.11 \
    --without-select_module \
    --without-poll_module \
    --without-http-cache \
    --with-http_ssl_module \
    --with-http_sub_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_auth_request_module \
    --add-module=../nginx-rtmp-module-master \
    --with-debug \
    --with-cc-opt='-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -DUSE_UPDATEENGINE=ON -DSUCCESSFUL_BOOT=ON'

2.4 对objs/Makefile的修改

执行./config.sh脚本后生成用于编译的objs/Makefile文件,由于在configure配置项上无法配置pcre的交叉编译环境,因此在执行make编译前,还需要对objs/Makefile做修改。

打开objs/Makefile

1 添加pcre的交叉编译环境,如下:
image.png
2 由于编译链接libcrypto.a库时需要加在-ldl -lpthread的前面,否则会报错,因此修改objs/Makefile如下:
image.png
3 由于通过objs/Makefile直接编译openssl,在编译过程中会出现交叉编译环境不正确的情况。最终采用以下方式解决openssl交叉编译问题,如下:
在执行make编译前,先进入到…/openssl-1.1.1h/的目录,执行如下Configure配置:

$ ./Configure \                                                                             
    linux-armv4 \
    --prefix=/home/yanz/opt/nginx-rtmp-build/openssl-1.1.1h/.openssl \
    no-shared no-asm no-ssl2 \
    -DOPENSSL_USE_IPV6 \
    -DOPENSSL_NO_DYNAMIC_ENGINE

这里暂不对openssl的配置命令做详述,有兴趣可以查阅相关资料。

然后回到nginx-1.12.2源码目录下,修改objs/Makefile,将一下两行配置删除:
image.png
删除后,如下:
image.png

2.5 编译

通过以上修改后,执行make进行编译,此时可以看到编译时使用正确的交叉编译工具aarch64-linux-gnu-gcc进行编译即可。
image.png
编译完成后在objs/目录下生成可执行文件nginx,使用file命令查看nginx运行平台:
image.png

2.6 测试

在板子上执行如下命令,停止nginx服务:

killall nginx

将生成的nginx可执行程序拷贝到板子上/usr/sbin/目录下,并启动nginx服务器:

//启动nginx
$ /usr/sbin/nginx

修改/etc/nginx/nginx.conf配置文件,添加rtmp配置:
image.png
执行如下命令重新加载配置:

# nginx -s reload

配置板子ip地址与电脑处于同一网口,此处测试配置板子ip: 192.168.1.136
在板子上执行ffmpeg推流命令:

# ffmpeg -re -i ./test.mp4 -c copy -f flv rtmp://192.168.1.136:1935/myapp/test

在电脑上用vlc播放rtmp://192.168.1.136:1935/myapp/test即可。
image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值