Windows Server - Caddy2 as a service

I’m trying to find documentation on setting up Caddy2 (beta 18) as a service on a Windows Server. I’ve tried using sc create "caddy" binPath= "C:\Caddy\caddy2.exe start -config C:\Caddy\Caddyfile" DisplayName= "caddy" and the service will not start up.

The error is “Error 1053: the service did nto respond to the start or control request in a timely fashion”. If I run the command from the prompt it works fine. I have gone as far as changing the logon as in the services from local service, to a user and to a local admin account with no success.

Also tried putting the command into a batch file and using sc to configure the service that way; same result. Does anyone have any knowledge of getting this to work?

It requires that the main thread (I think) does some special Windows API handling: https://github.com/kardianos/service/blob/master/service_windows.go

Any service integrations would definitely be implemented as separate plugins, like with v1: https://github.com/hacdias/caddy-service

Thanks for the response. I think I found an alternative solution by just using Windows Scheduled task to manage things. The task can be set to run every few minutes and running the following PowerShell script which it will either start caddy back up or reload the config if the config file has been modified in less than 5 minutes.

try {
    $Process = Get-Process caddy2 -ErrorAction Stop
    If($Process) {
        If($($(Get-Date) - $(Get-Item C:\Caddy\Caddyfile).LastWriteTime).TotalMinutes -le 5) {
            Start-Process -FilePath "C:\Caddy\caddy2.exe" -ArgumentList "reload -config C:\Caddy\Caddyfile"
            Write-Host "reloaded config"
        }
    }
}
catch {
    Start-Process -FilePath C:\Caddy\caddy2.exe -ArgumentList "start -config C:\Caddy\Caddyfile"
}
2 Likes

Glad you found a solution!

Just wanted to point out that a popular solution has been to use https://nssm.cc/ to set up and configure a service on windows.

Your powershell script sounds like a better solution for your use-case though. :+1:

1 Like

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