In recent articles, I walked through how to setup a home webserver with Apache on Linux and how to configure home DNS server using bind on Linux, complete with custom in-home domain for local name resolution. This article revisits the webserver and creates a second virtual host to handle WebDav.
WebDAV is a file sharing protocol built on top of HTTP. Many operating systems can attach to WebDAV folders to upload and download files, including Linux, Windows, Mac, IOS, and Android. I have a password database that I want to keep sync’d between different computers and phones and I’m not comfortable hosting that “in the cloud”, so this allows me to self-host.
I’m using Ubuntu Server 21.04 for this exercise.
My forward lookup zone file includes an A record for the server and a CNAME for the dav share, similar to the output below.
www IN A 192.168.26.53
dav IN CNAME www.stewart.lan
Once the zone file is updated and named restarted, this can be tested by pinging “www.stewart.lan” and “dav.stewart.lan”.
If you haven’t already done so, the first thing to do is to install apache2. Next, enable the webdav Apache modules. Apache using a2enmod and a2dismod for handling modules. Finally, create a folder to handle the WebDAV files and set the permissions up. When complete, restart Apache to load the modules.
sudo apt install apache2
sudo a2enmod dav
sudo a2enmod dav_fs
sudo mkdir /var/www/dav
sudo chown -R wwwroot:wwwroot /var/www/dav
sudo systemctl restart apache2.service
Apache is now ready to host a WebDAV site, but needs a configuration. For this, create a text file under /etc/apache2/sites-available (I named mine dav.conf). The serveralis parameter tells it to respond to requests to dav.stewart.lan and the alias directive tells Apache the root location is the dav folder.
brent@webnamer:/etc/apache2/sites-available$ cat dav.conf
DavLockDB /var/www/DavLock #database file Apache uses to lock files
<VirtualHost *:80>
ServerName stewart.lan
ServerAlias dav.stewart.lan
alias / /var/www/dav
ServerAdmin brent@stewart.tc
DocumentRoot /var/www/dav/
<Directory /var/www/dav/>
Options Indexes MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
<Location /dav>
DAV On
AuthType Basic
AuthName "webdav"
AuthUserFile /var/www/passwd.dav
Require valid-user
</Location>
</VirtualHost>
Enable the site and reload Apache
a2ensite dav.conf
sudo systemctl restart apache2.service
Go to the directory referenced by DavLockDB and create an empty file named users.password. Set the file ownership to www-data. Finally, add users to this file using htdigest (you’ll be prompted for passwords).
sudo touch /var/www/users.password
sudo chown www-data:www-data /var/www/passwd.dav
sudo htdigest /var/www/passwd.dav webdav newuser
Adding user newuser in realm webdav
New password:
There are a lot of ways to test. You can browse to that URL, use an application, or attach to it from your file manager using the url webdav://dav.myserver. Confirm that you are prompted for a password as expected!