ArchLinux 2017.10.01: Install WebDAV for file server

This article will describe installing WebDAV.

1 Install WebDAV

  • Use https or davs for SSL.
  • WEBDAV_USERNAME is username and WEBDAV_PASSWORD is password for digest authentication.
  • Change WEBDAV_SERVER_FQDN to your server's FQDN.
#!/bin/sh

set -e

[ -z "${WEBDAV_USERNAME}" ] && \
  WEBDAV_USERNAME=webdav
[ -z "${WEBDAV_PASSWORD}" ] && \
  WEBDAV_PASSWORD=webdav
[ -z "${WEBDAV_SERVER_FQDN}" ] && \
  WEBDAV_SERVER_FQDN=webdav-server.hiroom2.com

sudo pacman -Sy --noconfirm apache
sudo systemctl enable httpd

# ssl configuration.
# Country Name (2 letter code) [AU]:
# State or Province Name (full name) [Some-State]:
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:
# Organizational Unit Name (eg, section) []:
# Common Name (e.g. server FQDN or YOUR name) []:
# Email Address []:
cat <<EOF | sudo openssl req -new -x509 -nodes -newkey rsa:4096 -days 1095 \
                 -keyout /etc/httpd/conf/server.key \
                 -out /etc/httpd/conf/server.crt
AU
Some-State
city
company
section
${WEBDAV_SERVER_FQDN}

EOF
sudo sed -i /etc/httpd/conf/httpd.conf \
     -e 's/^#LoadModule ssl_module/LoadModule ssl_module/g' \
     -e 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/g'
cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/httpd-ssl.conf
EOF

# digest configuration.
sudo sed -i /etc/httpd/conf/httpd.conf \
     -e 's/#LoadModule auth_digest_module/LoadModule auth_digest_module/g'

# dav configuration.
sudo sed -i /etc/httpd/conf/httpd.conf \
     -e 's/^#LoadModule dav_module/LoadModule dav_module/g' \
     -e 's/^#LoadModule dav_fs_module/LoadModule dav_fs_module/g' \
     -e 's/^#LoadModule dav_lock_module/LoadModule dav_lock_module/g'
cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/webdav.conf
EOF
cat <<EOF | sudo tee /etc/httpd/conf/extra/webdav.conf
Alias /webdav /srv/http/webdav
DavLockDB "/srv/http/lock/DavLock"

<Location /webdav>
  DAV On
  SSLRequireSSL
  AuthType Digest
  AuthName webdav
  AuthUserFile /etc/httpd/.webdav
  Require valid-user
</Location>
EOF
sudo mkdir /srv/http/lock
sudo mkdir /srv/http/webdav
sudo chown http:http /srv/http/lock
sudo chown http:http /srv/http/webdav

sudo systemctl restart httpd

# Create digest password file with expect command.
sudo pacman -Sy --noconfirm 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 Access to WebDAV

Install davfs2 package for mounting davfs.

$ sudo pacman -Sy --noconfirm davfs2

Run "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>
Accept certificate for this session? [y,N] y

Write file via WebDAV.

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

For updating written file immediately, unmount /mnt.

$ sudo umount /mnt

Written file can be read in /var/www/webdav.

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