在安卓平板上搭建 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 u0_a226@192.168.1.101 -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>

posted @ 2023-10-22 10:42  x3d  阅读(290)  评论(0编辑  收藏  举报