Fedora 29: ファイルサーバのWebDAVをインストールする

WebDAVのインストール方法について記載します。

1 WebDAVをインストールする

  • この記事ではhttpsの為にデフォルトのSSL/TLS証明書を使っています。別にSSL/TLS証明書を用意している場合はそちらをお使いください。
  • SSLを利用するのでhttpsやdavsで接続してください。
  • Digest認証を使用します。ユーザ名はWEBDAV_USERNAMEの値で、パスワードはWEBDAV_PASSWORDの値です。
#!/bin/sh -e

[ -z "${WEBDAV_USERNAME}" ] && \
  WEBDAV_USERNAME=webdav
[ -z "${WEBDAV_PASSWORD}" ] && \
  WEBDAV_PASSWORD=webdav

sudo dnf install -y httpd httpd-tools mod_ssl apr-util-bdb

cat <<EOF | sudo tee /etc/httpd/conf.d/webdav.conf
Alias /webdav /var/www/webdav
DAVLockDB /var/lib/webdav/DAVLock

<VirtualHost _default_:443>
  SSLEngine on
  SSLCertificateFile /etc/pki/tls/certs/localhost.crt
  SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
</VirtualHost>

<Location /webdav>
  DAV On
  SSLRequireSSL
  AuthType Digest
  AuthName webdav
  AuthUserFile /etc/httpd/.webdav
  Require valid-user
</Location>
EOF

sudo mkdir /var/www/webdav
sudo chown apache:apache /var/www/webdav

sudo mkdir /var/lib/webdav
sudo chown apache:apache /var/lib/webdav

sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
sudo systemctl enable httpd
sudo systemctl restart httpd

sudo setsebool -P httpd_unified 1

# Create digest password file with expect command.
sudo dnf install -y expect
expect -c "
set timeout -1
spawn sudo htdigest -c /etc/httpd/.webdav webdav ${WEBDAV_USERNAME}
expect \"New password: \"
send \"${WEBDAV_PASSWORD}\n\"
expect \"Re-type new password: \"
send \"${WEBDAV_PASSWORD}\n\"
expect eof
"

2 WebDAVへアクセスする

davfsのマウントを可能にするdavfs2パッケージをインストールします。

$ sudo dnf install -y davfs2

mount -t davfsでマウントします。証明書を組み込んでいないので警告がでます。

$ sudo mount -t davfs https://localhost/webdav /mnt
Please enter the username to authenticate with server
https://localhost/webdav or hit enter for none.
  Username: webdav
Please enter the password to authenticate user webdav with server
https://localhost/webdav or hit enter for none.
  Password:
<snip>
You only should accept this certificate, if you can
verify the fingerprint! The server might be faked
or there might be a man-in-the-middle-attack.
Accept certificate for this session? [y,N] y

/mnt/hello.txtを書き込みます。

$ echo hello | sudo tee /mnt/hello.txt

即座にファイルを反映させる為にアンマウントします。

$ sudo umount /mnt

WebDAVのディレクトリにファイルが反映されています。

$ sudo cat /var/www/webdav/hello.txt
hello