Caddy on Mac OS as homebrew service autostart on system startup

1. Output of caddy version:

v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=

2. How I run Caddy:

only locally for development on M1 Mac

a. System environment:

Mac OS Monterey, installed with homebrew

3. The problem I’m having:

Everything works, I installed via homebrew and now I can run caddy. Nice. But I want it to run in the background, even after mac has restarted. I thought this is what caddy run is for. But it’s not.

So what do I have to do to make caddy restart the service on start up. With my other homebrew installed services, like postgres or mongodb, they automatically restart when my system restarts.

And with that, if i have to do anything globally, where would I put my global Caddy file? Or could I provide it somehow with caddy start -config ~/my/custom/path?

Thanks for any help guys!

PS: Yes I looked at this old post, but that does not seem to work anymore, I tried it: Run caddy on macOS startup

1 Like

(Interesting; I thought the homebrew formula did this for you. Guess not.)

The launchd file from Caddy v1 can be found in our repo on the v1 branch here:

You’ll need to update that to work for Caddy v2, but the overall launchd steps should be similar (unless Apple has changed how it works in recent Mac versions – I don’t dev on a Mac anymore so I dunno).

Once someone figures it out, would be a good addition to our community wiki here!

You should be able to just run:
brew service start caddy

The Caddyfile is probably at /usr/local/etc/Caddyfile

1 Like

Indeed brew services start caddy (note the s), starts caddy. But regarding the Caddyfile, there is nothing in /usr/local/etc/Caddyfile. I created the etc/ folder and added a Caddyfile. But it is not being picked up automatically. When I do sth like this instead: brew services start caddy --file ~/my/custom/Caddyfile I get this ouput:

Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/phil/Library/LaunchAgents/homebrew.mxcl.caddy.plist` exited with 5.

Dunno, that’s not an exit code Caddy uses. Try running as root I guess to see more detailed errors, or look at your system Console for more info.

(Sorry I can’t be more help on this one. Maybe someone else has more experience with it!)

I wonder if it’s file permission issue. Check the permissions on /usr/local/etc/Caddyfile.

1 Like

I had to create the Caddyfile using sudo, it’s from the link @matt posted. Permissions are:

-rw-r--r-- 1 root wheel 160B 23 Aug 18:57 /usr/local/etc/Caddyfile

1 Like

/opt/homebrew/etc/Caddyfile

that’s where it lives. so far looking good, will report.

2 Likes

Runs super smooth. Maybe this should be documented in the docs…

1 Like

Good idea :+1:

This topic was automatically closed after 30 days. New replies are no longer allowed.