uhttpd移植----在樹莓派上移植openwrt中的默認web服務

在樹莓派上移植openwrt中的web服務

openwrt 中默認使用的web服務器是uhttpd,uhttp是一款功能全面又小巧的web服務器,非常適合學習web服務編程使用,此外,在嵌入式設備中使用該web服務器,也是一個不錯的選擇。
這裏就介紹一下關於 uhttpd 移植相關的內容,以樹莓派上的debian 系統爲例,一步一步的將uhttpd移植過去。

uhttp相關依賴

  • json-c
  • ubox
  • ubus
  • lua

ubus 簡介

ubus 是 openwrt 中實現進程間通信的一套通用框架,通過 ubus ,可以使得進程間的通信變得非常簡單,這套進程間通信機制也可以非常方便的移植到其他的linux系統中,在移植 uhttpd 的同時,也將ubus 移植到了樹莓派中,後續會另起一篇文章介紹一下 ubus 的使用。

移植步驟

uhttpd 是使用 cmake 進行編譯的,因此在編譯之前需要安裝一下 cmake,這個就不介紹了。

移植 json-c

$ git clone https://github.com/json-c/json-c.git
$ cd json-c
$ ./autogen.sh --configure
$ make
$ make install

注意事項

在進行 autogen.sh 時 如果提示 autoreconf 不存在,則需要安裝 autoreconf。

$ sudo apt-get install dh-autoreconf

移植 ubox

$ git clone git://git.openwrt.org/project/libubox.git
$ cd libubox
$ cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_LUA=off
$ make
$ make install

移植 ubus

$ git clone git://git.openwrt.org/project/ubus.git
$ cd ubus
$ cmake -DBUILD_LUA=off
$ make
$ make install

編譯完成後,會生成以下文件。

-rwxr-xr-x 1 root root 233K Sep 26 10:28 libubus.so
drwxr-xr-x 3 root root 4.0K Sep 26 10:27 lua
-rwxr-xr-x 1 root root 124K Sep 26 10:28 ubus
-rwxr-xr-x 1 root root 273K Sep 26 10:28 ubusd

注意事項

因爲在 make install 的時候,默認沒有將 libubus.so 安裝到系統的庫路徑下,如果要使用 ubus 的話,還需要將 libusus.so 拷貝到系統庫下面。

$ cp libubus.so /usr/local/lib/

移植 lua

$ git clone https://github.com/lua/lua.git
$ cd lua 
$ make linux
$ make install

注意事項

編譯過程中如果提示 缺少 readline.h 則需要安裝 libreadline-dev

$ sudo apt-get install libreadline-dev

移植 uhttpd

$ cd uhttpd/build
$ cmake ..
$ make
$ make install

注意事項

如果不需要 uhttpd 支持 TLS 則需要在 CMakeList.txt 文件中將 TLS_SUPPORT 選項設置爲 OFF。

# CMakeList.txt

OPTION(TLS_SUPPORT "TLS support" OFF)

測試

所有軟件移植完成後,uhttpd就可以正常運行了。

root@raspberrypi:/home/qiao/http/uhttpd/ubus/build# ubus
Usage: ubus [<options>] <command> [arguments...]
Options:
 -s <socket>:           Set the unix domain socket to connect to
 -t <timeout>:          Set the timeout (in seconds) for a command to complete
 -S:                    Use simplified output (for scripts)
 -v:                    More verbose output
 -m <type>:             (for monitor): include a specific message type
                        (can be used more than once)
 -M <r|t>               (for monitor): only capture received or transmitted traffic

Commands:
 - list [<path>]                        List objects
 - call <path> <method> [<message>]     Call an object method
 - listen [<path>...]                   Listen for events
 - send <type> [<message>]              Send an event
 - wait_for <object> [<object>...]      Wait for multiple objects to appear on ubus
 - monitor                              Monitor ubus traffic

root@raspberrypi:/home/qiao/http/uhttpd/ubus/build# uhttpd --help
uhttpd: invalid option -- '-'
Usage: uhttpd -p [addr:]port -h docroot
        -f              Do not fork to background
        -c file         Configuration file, default is '/etc/httpd.conf'
        -p [addr:]port  Bind to specified address and port, multiple allowed
        -h directory    Specify the document root, default is '.'
        -E string       Use given virtual URL as 404 error handler
        -I string       Use given filename as index for directories, multiple allowed
        -S              Do not follow symbolic links outside of the docroot
        -D              Do not allow directory listings, send 403 instead
        -R              Enable RFC1918 filter
        -n count        Maximum allowed number of concurrent script requests
        -N count        Maximum allowed number of concurrent connections
        -l string       URL prefix for Lua handler, default is '/lua'
        -L file         Lua handler script, omit to disable Lua
        -u string       URL prefix for UBUS via JSON-RPC handler
        -U file         Override ubus socket path
        -a              Do not authenticate JSON-RPC requests against UBUS session api
        -X              Enable CORS HTTP headers on JSON-RPC api
        -x string       URL prefix for CGI handler, default is '/cgi-bin'
        -y alias[=path] URL alias handle
        -i .ext=path    Use interpreter at path for files with the given extension
        -t seconds      CGI, Lua and UBUS script timeout in seconds, default is 60
        -T seconds      Network timeout in seconds, default is 30
        -k seconds      HTTP keepalive timeout
        -A seconds      TCP keepalive timeout, default is unset
        -d string       URL decode given string
        -r string       Specify basic auth realm
        -m string       MD5 crypt given string



root@raspberrypi:/home/qiao/http/uhttpd/ubus/build#uhttpd -f -h /www  -T 30 -k 20 -A 1 -n 3 -N 100 -R -p 0.0.0.0:80 -p [::]:80

結尾

以上就是整個移植過程,移植完成之後,下一步就開始在樹莓派上使用 uhttpd 了,此外,還能夠使用 ubus 框架在 樹莓派上實現進程間通信,請等待後續更新…

參考鏈接

uhttpd github地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章