在安卓平板上搭建 webdav 服務

早上醒來,腦子裏又冒出來要搭建一個 webdav 服務以便嘗試各種非雲服務模式的筆記客戶端的念頭。於是任性的嘗試起來。

在自己的華爲matepad安卓平板上進行的。

搭建 Linux 模擬環境

從 f-droid 應用市場中安裝 termux app。

termux 帶 包管理,而且有非常多的應用可用,甚至 nodejs。

安裝好後,進入app,先開啓內存卡文件訪問權限

termux-setup-storage

開啓 sshd 服務

安裝 sshd ,設置當前用戶密碼,啓動服務。

pkg install openssh

whoami 查看當前用戶名

passwd 設置當前用戶密碼

sshd

ifconfig 查看ip


通過 ssh 客戶端 就可以登錄服務器了,默認端口是8022。如 ssh [email protected] -p8022

ssh 登錄後,就可以遠程繼續操作服務器,安裝和配置服務。

安裝 apache

因爲 apache 內置了 webdav 模塊,安裝這一個東東就能實現需求。

pkg install apache2

先輸入 httpd 先驗證一下 apache 能否正常啓動。默認出現 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message 這樣的提示,也是ok的。

配置文件中默認端口是 8080,因此可以用瀏覽器或者命令測試一下服務能否正常訪問,地址如 http://192.168.1.101:8080/ ,頁面輸出 It works!

配置 webdav 服務

termux 中命令行有個環境變量 $PREFIX ,代表路徑前綴 /data/data/com.termux/files/usr,可以降低敲命令時的負擔。

比如,cd $PREFIX/etc/apache2/,實際進入的目錄會是 /data/data/com.termux/files/usr/etc/apache2

需要操作的配置文件有兩個。

  • etc/apache2/httpd.conf
  • etc/apache2/extra/httpd-dav.conf

配置過程主要參考 httpd-dav.conf 中的說明即可,先按其中要求依賴的模塊修改 httpd.conf 文件,取消相應模塊的註釋並取消 httpd-dav.conf 這行的註釋,然後執行密碼生成命令,最後創建相應的文件存儲目錄。

各種 webdav 客戶端用到的認證類型,有 basic - mod_auth_basic 和 digest - mod_auth_digest 兩種, basic 居多,所以針對 httpd-dav.conf 默認的配置,複製了一段,用於實現 basic 模式的授權。

下例中,/uploads 路徑對應 默認的 digest 認證,/webdavb 對應 basic 認證。

兩種認證方式,用到的 密碼生成工具也是不同的,digest 方式的如配置文件中所示,htdigest -c "/data/data/com.termux/files/usr/user.passwd" DAV-upload admin , basic 的則爲 htpasswd -c "/data/data/com.termux/files/usr/webdavb.passwd" admin

最後還初始化對應的兩個目錄,mkdir。

#
# Distributed authoring and versioning (WebDAV)
#
# Required modules: mod_alias, mod_auth_digest, mod_authn_core, mod_authn_file,
#                   mod_authz_core, mod_authz_user, mod_dav, mod_dav_fs,
#                   mod_setenvif

# The following example gives DAV write access to a directory called
# "uploads" under the ServerRoot directory.
#
# The User/Group specified in httpd.conf needs to have write permissions
# on the directory where the DavLockDB is placed and on any directory where
# "Dav On" is specified.

DavLockDB "/data/data/com.termux/files/usr/var/DavLock"

Alias /uploads "/data/data/com.termux/files/usr/uploads"
Alias /webdavb "/data/data/com.termux/files/usr/webdavb"

<Directory "/data/data/com.termux/files/usr/uploads">
    Dav On

    AuthType Digest
    AuthName DAV-upload
    # You can use the htdigest program to create the password database:
    #   htdigest -c "/data/data/com.termux/files/usr/user.passwd" DAV-upload admin
    AuthUserFile "/data/data/com.termux/files/usr/user.passwd"
    AuthDigestProvider file

    # Allow universal read-access, but writes are restricted
    # to the admin user.
    <RequireAny>
        Require method GET POST OPTIONS
        Require user admin
    </RequireAny>
</Directory>


<Directory "/data/data/com.termux/files/usr/webdavb">
    Dav On

    AuthType Basic
    AuthName DAV-upload
    # You can use the htpasswd program to create the password database:
    #   htpasswd -c "/data/data/com.termux/files/usr/webdavb.passwd" admin
    AuthUserFile "/data/data/com.termux/files/usr/webdavb.passwd"

    # Allow universal read-access, but writes are restricted
    # to the admin user.
    <RequireAny>
        Require method GET POST OPTIONS
        Require user admin
    </RequireAny>
</Directory>

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