{"id":1989,"date":"2015-12-19T18:17:11","date_gmt":"2015-12-19T17:17:11","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1989"},"modified":"2016-01-20T17:10:41","modified_gmt":"2016-01-20T16:10:41","slug":"running-caddy-server-as-a-service-with-systemd","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/","title":{"rendered":"Running Caddy Server as a service with systemd"},"content":{"rendered":"<p>In a\u00a0<a href=\"http:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service\/\" target=\"_blank\">previous blogpost<\/a> I explained how to run <a href=\"https:\/\/caddyserver.com\" target=\"_blank\">Caddy<\/a> (a brilliant and simple web server) as a service using Upstart. <a href=\"http:\/\/freedesktop.org\/wiki\/Software\/systemd\/\" target=\"_blank\">Systemd<\/a>, however, replaced Upstart on most of the operating systems, so it makes more sense to have a guide for systemd.<\/p>\n<h3><\/h3>\n<h3>Caddy executable<\/h3>\n<p>We don&#8217;t run Caddy as root to keep things as secure as possible. To allow a non-root user to listen on port 80 or 443, we\u00a0must allow the executable to do so:<\/p>\n<pre><code>$ sudo setcap cap_net_bind_service=+ep .\/caddy\r\n<\/code><\/pre>\n<h3>systemd unit file<\/h3>\n<p>Now we create a systemd configuration file <code>\/etc\/systemd\/system\/caddy.service<\/code>, which looks like this on my server:<\/p>\n<pre><code>[Unit]\r\nDescription=Caddy webserver\r\nDocumentation=https:\/\/caddyserver.com\/\r\nAfter=network.target\r\n\r\n[Service]\r\nUser=mathias\r\n#Group=some_group\r\nWorkingDirectory=\/home\/mathias\r\nLimitNOFILE=4096\r\nPIDFile=\/var\/run\/caddy\/caddy.pid\r\nExecStart=\/home\/mathias\/caddy -agree -email my_lets_encrypt@mail.com -pidfile=\/var\/run\/caddy\/caddy.pid\r\nRestart=on-failure\r\nStartLimitInterval=600\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/code><\/pre>\n<p>A brief explanation of the directives:<\/p>\n<ul>\n<li><code>After=network.target<\/code> will automatically start Caddy after all networking stuff is loaded at boot.<\/li>\n<li><code>User<\/code>, <code>Group<\/code> and <code>WorkingDirectory<\/code> speak for themselves&#8230; Make sure that your Caddyfile is in the working directory. You can always manually specify the location of the Caddyfile:\u00a0<code>$ caddy -conf=\"\/path\/to\/Caddyfile\"<\/code>.<\/li>\n<li><code>LimitNOFILE=4096<\/code> sets max file descriptor (to a\u00a0good production value), like you would do with\u00a0<code>ulimit -n 4096<\/code> in your terminal.<\/li>\n<li><code>ExecStart<\/code> is the actual script that will be executed. In this case it runs the Caddy binary which is in my home directory, agrees the <a href=\"https:\/\/letsencrypt.org\" target=\"_blank\">Let&#8217;s Encrypt<\/a> user agreement, and sets a default email for your Let&#8217;s Encrypt account. If you don&#8217;t add those two flags, Caddy will prompt for user input, which will naturally not work when you start it as a daemon.<\/li>\n<li><code>Restart=on-failure<\/code> will automatically respawn the Caddy process if Caddy crashes (i.e. stopping with non-zero exit code).<\/li>\n<li><code>StartLimitInterval=600<\/code> makes sure that if Caddy crashes too many times (being 5 by systemd&#8217;s default value) it will wait 10 min before trying again.<\/li>\n<li><code>WantedBy<\/code> specifies a target we want this service to be included with.<\/li>\n<\/ul>\n<p>Make sure too take a peak at the <a href=\"http:\/\/www.freedesktop.org\/software\/systemd\/man\/systemd.service.html\" target=\"_blank\">systemd documentation<\/a>, since it&#8217;s very powerful \u00a0and supports a lot of features!<\/p>\n<h3>Running the service<\/h3>\n<p>After you have created the service file, you must enable it:\u00a0<code>sudo systemctl enable caddy<\/code>. Don&#8217;t forget to reload the service file if you make changes to it:\u00a0<code>systemctl daemon-reload<\/code>.<\/p>\n<p>Now you can start Caddy using <code>sudo service caddy start<\/code>. And of course <code>stop<\/code>, <code>restart<\/code>, &#8230;<\/p>\n<p>You can view the status of your service using\u00a0<code>sudo service caddy status<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a\u00a0previous blogpost I explained how to run Caddy (a brilliant and simple web server) as a service using Upstart. Systemd, however, replaced Upstart on most of the operating systems, so it makes more sense to have a guide for systemd. Caddy executable We don&#8217;t run Caddy as root to keep things as secure as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[227],"tags":[260,231,232,151,186,264,212],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Running Caddy Server as a service with systemd &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running Caddy Server as a service with systemd &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"In a\u00a0previous blogpost I explained how to run Caddy (a brilliant and simple web server) as a service using Upstart. Systemd, however, replaced Upstart on most of the operating systems, so it makes more sense to have a guide for systemd. Caddy executable We don&#8217;t run Caddy as root to keep things as secure as [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-19T17:17:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-01-20T16:10:41+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/\",\"name\":\"Running Caddy Server as a service with systemd &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2015-12-19T17:17:11+00:00\",\"dateModified\":\"2016-01-20T16:10:41+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1989"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1989"}],"version-history":[{"count":5,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":2022,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions\/2022"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}