Operating System CentOS

How to Set Up a WebDAV Server with Apache on AlmaLinux 9 / Rocky Linux 9

Share a directory over WebDAV with Apache on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9: users, SELinux label, firewalld, HTTPS and testing with curl and cadaver.

WebDAV extends HTTP so that clients can upload, rename, delete and lock files on a web server, which makes it a simple way to share a directory without running FTP or Samba. This guide sets up a password-protected WebDAV share with Apache (httpd) on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, including the SELinux and firewalld changes those systems need.

CentOS 7 reached end of life in June 2024. The Apache configuration is much the same on its successors, but the package manager is dnf, SELinux needs a writable label on the share, and the lock database no longer has to be configured by hand.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 and a user with sudo rights.
  • A hostname pointing to the server, for example dav.example.com. You can test with the IP address, but you need a hostname to add HTTPS, and you should add HTTPS before real use.

Step 1: Install Apache

sudo dnf install httpd

The httpd package already contains the three WebDAV modules and loads them from /etc/httpd/conf.modules.d/00-dav.conf. Confirm it:

sudo httpd -M | grep dav

The output should list dav_module, dav_fs_module and dav_lock_module.

Step 2: Create the shared directory

Keep the share outside the normal document root so that it is only reachable through the WebDAV configuration. Apache runs as the apache user and must be able to write to the directory:

sudo mkdir -p /var/www/webdav
sudo chown apache:apache /var/www/webdav
sudo chmod 750 /var/www/webdav

Files uploaded over WebDAV are owned by apache. Treat the directory as private to the web server and manage its content through WebDAV rather than by editing files on disk.

Allow writes under SELinux

SELinux is enforcing by default, and content under /var/www is labelled read-only for the web server. Without a writable label, uploads fail with a 403 error even though the file permissions are right. Install the management tools, add a rule for the directory and apply it:

sudo dnf install policycoreutils-python-utils
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/webdav(/.*)?"
sudo restorecon -Rv /var/www/webdav

Step 3: Create a WebDAV user

The htpasswd tool comes from httpd-tools, which is installed together with httpd. Create the password file with a first user (the -c option creates the file, so use it only once):

sudo htpasswd -c /etc/httpd/webdav.passwd deploy

Add further users without -c:

sudo htpasswd /etc/httpd/webdav.passwd anotheruser

Make the file readable by Apache only:

sudo chown root:apache /etc/httpd/webdav.passwd
sudo chmod 640 /etc/httpd/webdav.passwd

Step 4: Configure the WebDAV location

Create a configuration file:

sudo vi /etc/httpd/conf.d/webdav.conf

Add a virtual host that maps /webdav to the directory and requires a login:

<VirtualHost *:80>
    ServerName dav.example.com
    DocumentRoot /var/www/html

    Alias /webdav /var/www/webdav

    <Directory /var/www/webdav>
        Dav On
        AuthType Basic
        AuthName "WebDAV"
        AuthUserFile /etc/httpd/webdav.passwd
        Require valid-user
    </Directory>
</VirtualHost>

Dav On enables the filesystem provider from mod_dav_fs. Once WebDAV is on for a directory it cannot be switched off again for a subdirectory, so point it only at the share itself.

Older tutorials add a DavLockDB line. The httpd build in the RHEL 9 family already keeps its lock database in Apache's own state directory, so you can leave the directive out. If you do set DavLockDB, it belongs outside the virtual host and its directory must be writable by the apache user.

Check the syntax, then start Apache and enable it at boot:

sudo apachectl configtest
sudo systemctl enable --now httpd

If Apache was already running, reload it instead with sudo systemctl reload httpd.

Step 5: Open the firewall

sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload

Step 6: Add HTTPS

Basic authentication sends the password with every request in a form that is trivial to decode, so do not use the share over plain HTTP on the internet. The Apache documentation recommends Basic authentication for WebDAV only over TLS. Install the TLS module:

sudo dnf install mod_ssl
sudo systemctl restart httpd

Then obtain a certificate for the hostname. If you use Certbot with its Apache plugin, one command requests a Let's Encrypt certificate, creates the HTTPS copy of the virtual host and redirects HTTP to it:

sudo certbot --apache -d dav.example.com

Certbot is not part of the base repositories; install it first as described on the Certbot instructions page.

Step 7: Test the share

You can exercise the basic WebDAV methods with curl from any machine. Each command prompts for the password. Upload a file:

echo "hello" > test.txt
curl -u deploy -T test.txt https://dav.example.com/webdav/

Create a directory and list the share:

curl -u deploy -X MKCOL https://dav.example.com/webdav/dir1/
curl -u deploy -X PROPFIND -H "Depth: 1" https://dav.example.com/webdav/

The upload and MKCOL should answer 201 Created, and PROPFIND returns an XML listing. A request without credentials must be rejected with 401 Unauthorized:

curl -I https://dav.example.com/webdav/

For an interactive command-line client on a RHEL-family machine, cadaver is available from EPEL:

sudo dnf install epel-release
sudo dnf install cadaver
cadaver https://dav.example.com/webdav/

Inside its prompt, put, get, mkdir, ls and quit do what their names say. Desktop file managers on Linux, macOS and Windows can connect to the same URL.

Troubleshooting

  • 403 Forbidden on upload while reading works: the SELinux label or the ownership is wrong. Check with ls -dZ /var/www/webdav; the type should be httpd_sys_rw_content_t and the owner apache. Recent denials are shown by sudo ausearch -m AVC -ts recent.
  • 401 with the correct password: the password file is not readable by Apache, or the path in AuthUserFile is wrong. Look at /var/log/httpd/error_log.
  • 405 Method Not Allowed: the request reached a path where Dav On does not apply. Check the Alias and the URL you are using.
  • Locking errors: if you set DavLockDB yourself, make sure its directory exists and is writable by apache.

Frequently asked questions

Can I make the share readable by everyone but writable only by one user?

Yes. Replace Require valid-user with a <RequireAny> block containing Require method GET POST OPTIONS and Require user deploy. Read-only methods are then open, while every other method needs the named account. The mod_dav documentation has the full example.

Is Digest authentication better than Basic?

Digest authentication (mod_auth_digest, loaded by default) avoids sending the password itself, and the Apache documentation names it as the minimum for WebDAV when TLS is not used. With HTTPS in place, Basic authentication is acceptable and is the simplest to manage.

How do I protect the server from abusive requests?

Leave DavDepthInfinity at its default of off so clients cannot request a recursive listing of the whole share, and set LimitXMLRequestBody to cap the size of XML request bodies. Apache cannot stop an authenticated user from filling the disk, so give accounts only to people you trust, or put the share on its own filesystem.

Why do documents opened from the share lose their lock?

Some clients request short lock timeouts. Setting DavMinTimeout 600 inside the <Directory> block makes the server grant locks of at least ten minutes, which reduces lost locks on slow connections.

Share story:

Continue reading other guides on How VPS.