Question

caddy static site with root in user's home directory causes permission errors

On a recently started droplet running Ubuntu 24.10 I installed Caddy following the standard procedure given in the Caddy docs for Ubuntu. I did not follow the DigitalOcean tutorial for installing Caddy on Ubuntu 22.04.

I have created an user for day-to-day running of the server. I have the Caddyfile

xxxx.com {
  encode zstd gzip
  root * /home/username/blog
  file_server
}

This file is in /etc/caddy and is owned by root.

When I have debug at the top of Caddyfile, journalctl shows that there are permission errors when the root of the static site is accessed.

When I move the static site to /var/www/ and modify the Caddyfile accordingly everything works.

In contrast to the instructions given in the above mentioned tutorial /var/www and all sub directories in it are owned by root (and not caddy) and everything works.

All directories in my static site have a permission of 775 and all files a permission of 664.

Despite going through many tutorials and comments I have no idea why I have the permission errors when the static site is in the user’s home directory.

Any help or pointers would be appreciated. Thanks.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 16, 2024
Accepted Answer

Hey Jagadish!

The issue is related to permissions since Caddy runs as the caddy user by default. When you try to serve files from your home directory, the caddy user doesn’t have access.

This is typical for all webservers including Nginx and Apache as well.

The caddy user can access /var/www because it’s set up by default for system services. However, accessing files in /home/username needs additional permissions as the /home/username directories are owned by a different user and group.

To keep using your home directory, try this:

  1. Change ownership to the cady user:
    sudo chown -R caddy:caddy /home/username/blog
    
  2. Adjust permissions so that they are not too open:
    sudo chmod -R 755 /home/username/blog
    
  3. Reload Caddy:
    sudo systemctl reload caddy
    

If you prefer not changing permissions in your home folder, you could then change the user as which the cady service runs as:

  1. Edit the Caddy systemd file:
    sudo nano /lib/systemd/system/caddy.service
    
  2. Change:
    User=username
    Group=username
    
  3. Reload and restart:
    sudo systemctl daemon-reload
    sudo systemctl restart caddy
    

In most cases, sticking to /var/www is usually best.

Let me know if this helps!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.