{"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":{"footnotes":""},"categories":[227],"tags":[260,231,232,151,186,264,212],"class_list":["post-1989","post","type-post","status-publish","format-standard","hentry","category-servers","tag-caddy","tag-go","tag-golang","tag-open-source","tag-server","tag-systemd","tag-ubuntu","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Running Caddy Server as a service with systemd &#8211; 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 &#8211; 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=\"author\" content=\"Mathias Beke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mathias Beke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/\"},\"author\":{\"name\":\"Mathias Beke\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"headline\":\"Running Caddy Server as a service with systemd\",\"datePublished\":\"2015-12-19T17:17:11+00:00\",\"dateModified\":\"2016-01-20T16:10:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/\"},\"wordCount\":342,\"commentCount\":17,\"keywords\":[\"caddy\",\"go\",\"golang\",\"open source\",\"server\",\"systemd\",\"ubuntu\"],\"articleSection\":[\"Servers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/\",\"name\":\"Running Caddy Server as a service with systemd &#8211; 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\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/servers\\\/running-caddy-server-as-a-service-with-systemd\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Running Caddy Server as a service with systemd\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Running Caddy Server as a service with systemd &#8211; DenBeke","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/","og_locale":"en_US","og_type":"article","og_title":"Running Caddy Server as a service with systemd &#8211; DenBeke","og_description":"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;]","og_url":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/","og_site_name":"DenBeke","article_published_time":"2015-12-19T17:17:11+00:00","article_modified_time":"2016-01-20T16:10:41+00:00","author":"Mathias Beke","twitter_card":"summary_large_image","twitter_creator":"@MthsBk","twitter_site":"@MthsBk","twitter_misc":{"Written by":"Mathias Beke","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/#article","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/"},"author":{"name":"Mathias Beke","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"headline":"Running Caddy Server as a service with systemd","datePublished":"2015-12-19T17:17:11+00:00","dateModified":"2016-01-20T16:10:41+00:00","mainEntityOfPage":{"@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/"},"wordCount":342,"commentCount":17,"keywords":["caddy","go","golang","open source","server","systemd","ubuntu"],"articleSection":["Servers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/","url":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/","name":"Running Caddy Server as a service with systemd &#8211; 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"},"breadcrumb":{"@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/denbeke.be\/blog\/"},{"@type":"ListItem","position":2,"name":"Running Caddy Server as a service with systemd"}]},{"@type":"WebSite","@id":"https:\/\/denbeke.be\/blog\/#website","url":"https:\/\/denbeke.be\/blog\/","name":"DenBeke","description":"Mathias Beke","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/denbeke.be\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc","name":"Mathias Beke","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","caption":"Mathias Beke"}}]}},"_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1989","targetHints":{"allow":["GET"]}}],"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}]}}