Update: Need to run Caddy as service with systemd? Checkout this blogpost!
Recently I discovered a very promising webserver (written in Go) called Caddy.
Now, Caddy is just an executable, which I wanted to turn into a service on my Ubuntu server.
Upstart
Running any executable as a service can be done using Upstart. Once you’ve created a config file in /etc/init/my_service.conf, you can use some simple commands like service my_service start, service my_service stop, … and the service will run at boot time.
The output of the program will be logged to /var/log/upstart/my_service.log.
Caddy executable
To allow a non-root user to listen on port 80, you must allow the executable to do so:
$ sudo setcap cap_net_bind_service=+ep ./caddy
Config file
My configuration file looks like this:
description "Caddy Server startup script"
author "Mathias Beke"
start on runlevel [2345]
stop on runlevel [016]
setuid some-caddy-user
setgid some-caddy-group
respawn
respawn limit 10 5
limit nofile 4096 4096
script
cd /home/mathias/
exec ./caddy
end script
Explanation of the commands:
start on runlevel [2245]specifies the job needs to be started after the basic facilities are up.stop on runlevel [016]is used for a normal shutdown.setuidandsetgidcan be used to run the job under a specific user account. Set this to an account that is less privileged than the root user. You don’t want to run a webserver with root access!respawnwill automatically restart (well… ‘respawn’) the process when it stops for any reason.respawn limit COUNT INTERVAL | unlimitedspecifies a limit for the automatic respawn of the process.
Respawning is subject to a limit. If the job is respawned more than COUNT times in INTERVAL seconds, it will be considered to be having deeper problems and will be stopped. Default COUNT is 10. Default INTERVAL is 5 seconds.
To have the job respawn indefinitely, specify an argument of “unlimited”. However, care should be taken using this option: does your service really stop that frequently? Should it?
execexecutes the actual command.
I managed easily to host files in the same directory as /caddy/ localhost:2015
Actually I want to host 2 sites with caddyserver and tried it with caddyfile.
But whatever is written in Caddyfile (same directory /caddy/)
seems to have no impact at all. I even renamed the file Caddyfile.config >
localhost:2015 works fine, but localhost:2020 (or localhost:8080) for the other site
does not. I edited it a dozen times, but it has NO effect at all !
Any ideas ?
Your Caddyfile is apparently not found. You’re sure they are in the same directory?
Else you can specify the full path when executing Caddy:
$ caddy -conf="/path/to/Caddyfile".Thanks for posting this. I was able to get Caddy up and running as a service without any trouble.
Thanks much. It was very easy to follow this and get up and running.
Thanks!
Do I have to serve the email adress here? like in with systemd. Because I get a permission denied response in the log file.
Do I have to make something with the non root user? Because when I set “setuid user” then it doesn’t work. It works only without setuid and setgid
As mentioned in the post, you need to allow Caddy to bind to port 80 and 443. So you either run it as root, or you give the executable the permission to bind to ports below 1024:
$ sudo setcap cap_net_bind_service=+ep ./caddyYes of course I run this command. But still when I have “setuid myusername” in the my_service.conf file Caddy is not running. When I delete setuid then it’s fine and my wordpress website starts. But still I have some wordpress ftp problems.(I can’t upload a plugin). I think I have some permission problems. But don’t know how to figure that out at the moment.
Probably something to do with certificates folder. I’ll look to my ‘current’ upstart config file today…
FTP in WordPress has nothing to do with Caddy’s permissions, since WordPress has the permissions of the FTP user at the time of installing the plugin.
ok thank you. Ok yes my wordpress problems were different. I had to change the owner and group of wordpress folder to the user who runs php-fpm.