CentOS 7: Install WebDAV

This article will describe installing WebDAV.

1 Install WebDAV

  • This article uses default SSL/TLS certicication file for https. Please change your SSL/TLS certification file.
  • Use https or davs for SSL.
  • WEBDAV_USERNAME is username and WEBDAV_PASSWORD is password for digest authentication.
#!/bin/sh

set -e

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

sudo yum install -y httpd httpd-tools mod_ssl

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 yum 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 Access to WebDAV

Install davfs2 package for mounting davfs.

$ sudo yum install -y epel-release
$ sudo yum install -y 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:
/sbin/mount.davfs: the server certificate does not match the server name
/sbin/mount.davfs: the server certificate is not trusted
  issuer:      SomeOrganizationalUnit, SomeOrganization, SomeCity, SomeState, --
  subject:     SomeOrganizationalUnit, SomeOrganization, SomeCity, SomeState, --
  identity:    centos-7
  fingerprint: 59:a8:d0:88:aa:e5:dc:47:21:32:4a:63:00:ce:45:68:84:03:86:73
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
/sbin/mount.davfs: Warning: can't write entry into mtab, but will mount the file system anyway

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