Running Caddy Server as a service with Upstart

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.
  • setuid and setgid can 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!
  • respawn will automatically restart (well… ‘respawn’) the process when it stops for any reason.
  • respawn limit COUNT INTERVAL | unlimited specifies 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?

     Upstart Cookbook

  • exec executes the actual command.

Join the Conversation

12 Comments

  1. 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 ?

    1. 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".

  2. 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

    1. 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 ./caddy

      1. Yes 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.

        1. 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.

          1. 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.

Leave a comment

Your email address will not be published. Required fields are marked *