{"id":2136,"date":"2016-11-18T16:37:50","date_gmt":"2016-11-18T15:37:50","guid":{"rendered":"https:\/\/denbeke.be\/blog\/?p=2136"},"modified":"2016-11-18T17:18:28","modified_gmt":"2016-11-18T16:18:28","slug":"running-caddy-as-a-service-on-macos-os-x-server","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/","title":{"rendered":"Running Caddy as a service on macOS X server"},"content":{"rendered":"<h3 id=\"installing-caddy\">Installing Caddy<a href=\"https:\/\/denbeke.be\/blog\/servers\/two-months-ago-i-started-to-serve-the-web-like-its-2016-and-im-still-loving-caddy-migrating-from-nginx-to-caddy-server\/attachment\/caddy-logo-2\/\" rel=\"attachment wp-att-2088\"><img loading=\"lazy\" class=\"alignright size-medium wp-image-2088\" src=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo-300x65.png\" alt=\"Caddy logo\" width=\"300\" height=\"65\" srcset=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo-300x65.png 300w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo-768x166.png 768w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo-1024x222.png 1024w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo.png 1200w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/h3>\n<p>Download <a href=\"https:\/\/caddyserver.com\" target=\"_blank\">Caddy<\/a> from the official website.<\/p>\n<p>Give the executable root permissions<sup>1<\/sup>:<\/p>\n<pre><code>$ chown root .\/caddy\r\n$ chmod +s .\/caddy<\/code><\/pre>\n<h3 id=\"caddyfile\">Caddyfile<\/h3>\n<p>Caddy reads it configuration for the <em>Caddyfile<\/em>. More information can be found in the <a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\" target=\"_blank\">official documentation<\/a>.<\/p>\n<h3 id=\"launchd-config\">Launchd config<\/h3>\n<p>Create a <em>launchd<\/em> plist file: <code>~\/Library\/LaunchAgents\/com.caddyserver.web.plist<\/code>. This file should look like this:<\/p>\n<pre><code>&lt;?xml version=\u201d1.0\" encoding=\u201dUTF-8\"?&gt;\r\n&lt;!DOCTYPE plist PUBLIC \u201c-\/\/Apple Computer\/\/DTD PLIST 1.0\/\/EN\u201d \u201chttp:\/\/www.apple.com\/DTDs\/PropertyList-1.0.dtd\"&gt;\r\n&lt;plist version=\u201d1.0\"&gt;\r\n    &lt;dict&gt;\r\n        &lt;key&gt;Label&lt;\/key&gt;\r\n        &lt;string&gt;com.caddyserver.web&lt;\/string&gt;\r\n        &lt;key&gt;EnvironmentVariables&lt;\/key&gt;\r\n        &lt;dict&gt;\r\n            &lt;key&gt;HOME&lt;\/key&gt;\r\n            &lt;string&gt;\/Users\/mathias&lt;\/string&gt;\r\n        &lt;\/dict&gt;\r\n        &lt;key&gt;ProgramArguments&lt;\/key&gt;\r\n        &lt;array&gt;\r\n            &lt;string&gt;sh&lt;\/string&gt;\r\n            &lt;string&gt;-c&lt;\/string&gt;\r\n            &lt;string&gt;launchctl unload -w \/Applications\/Server.app\/Contents\/ServerRoot\/System\/Library\/LaunchDaemons\/com.apple.serviceproxy.plist; ulimit -n 8192; cd \/Users\/mathias\/Sites; .\/caddy -agree -email my_email@domain.com -conf=\/Users\/mathias\/Sites\/Caddyfile&lt;\/string&gt;\r\n        &lt;\/array&gt;\r\n        &lt;key&gt;UserName&lt;\/key&gt;\r\n        &lt;string&gt;www&lt;\/string&gt;\r\n        &lt;key&gt;RunAtLoad&lt;\/key&gt;\r\n        &lt;true\/&gt;\r\n        &lt;key&gt;KeepAlive&lt;\/key&gt;\r\n        &lt;true\/&gt;\r\n        &lt;key&gt;WorkingDirectory&lt;\/key&gt;\r\n        &lt;string&gt;\/Users\/mathias\/Sites&lt;\/string&gt;\r\n        &lt;key&gt;StandardOutPath&lt;\/key&gt;\r\n        &lt;string&gt;\/Users\/mathias\/Sites\/caddy.log&lt;\/string&gt;\r\n        &lt;key&gt;StandardErrorPath&lt;\/key&gt;\r\n        &lt;string&gt;\/Users\/mathias\/Sites\/caddy_error.log&lt;\/string&gt;\r\n    &lt;\/dict&gt;\r\n&lt;\/plist&gt;<\/code><\/pre>\n<p>The launchd script will stop the built-in Apache server on macOS X, set the <code>ulimit<\/code> and execute Caddy. The Caddy command line arguments are quite trivial:<\/p>\n<ol style=\"list-style-type: decimal;\">\n<li><code>-agree<\/code>: agree to the CA&#8217;s (Let&#8217;s Encrypt) Subscriber Agreement.<\/li>\n<li><code>-email<\/code>: your email address used for the certificates authority.<\/li>\n<li><code>-conf<\/code>: full path to your Caddyfile.<\/li>\n<\/ol>\n<p>The launchd file will make sure that Caddy is started at boot and kept alive.<\/p>\n<p>Start and Stop Caddy launchd service using the following commands:<\/p>\n<pre><code>$ launchctl load ~\/Library\/LaunchAgents\/com.caddyserver.web.plist\r\n$ launchctl unload ~\/Library\/LaunchAgents\/com.caddyserver.web.plist<\/code><\/pre>\n<hr \/>\n<ol style=\"list-style-type: decimal;\">\n<li>Yes, I know, it&#8217;s not good practice to run a service as root. But there is no other decent way on OS X. <code>setcap<\/code> doesn&#8217;t exist on macOS. Since Caddy won&#8217;t be touching any files, and Caddy doesn&#8217;t execute shell script, there is not a lot that can go wrong in this case. If you feel uncomfortable running Caddy as root, you could use portforwarding in your firewall and listen on a port &gt;= 1024, for which you won&#8217;t need root privileges. This can be done using this command:\n<pre><code>echo \"\r\nrdr pass inet proto tcp from any to any port 80 -&gt; 127.0.0.1 port 8080\r\n\" | sudo pfctl -ef -<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Installing Caddy Download Caddy from the official website. Give the executable root permissions1: $ chown root .\/caddy $ chmod +s .\/caddy Caddyfile Caddy reads it configuration for the Caddyfile. More information can be found in the official documentation. Launchd config Create a launchd plist file: ~\/Library\/LaunchAgents\/com.caddyserver.web.plist. This file should look like this: &lt;?xml version=\u201d1.0&#8243; encoding=\u201dUTF-8&#8243;?&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[227,161],"tags":[260,232,151,149,186,150,110],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Running Caddy as a service on macOS X server &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\/software\/running-caddy-as-a-service-on-macos-os-x-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running Caddy as a service on macOS X server &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"Installing Caddy Download Caddy from the official website. Give the executable root permissions1: $ chown root .\/caddy $ chmod +s .\/caddy Caddyfile Caddy reads it configuration for the Caddyfile. More information can be found in the official documentation. Launchd config Create a launchd plist file: ~\/Library\/LaunchAgents\/com.caddyserver.web.plist. This file should look like this: &lt;?xml version=\u201d1.0&quot; encoding=\u201dUTF-8&quot;?&gt; [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-18T15:37:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-11-18T16:18:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo-300x65.png\" \/>\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\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2016\/03\/Caddy-logo.png\",\"width\":1200,\"height\":260},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/\",\"name\":\"Running Caddy as a service on macOS X server &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/#primaryimage\"},\"datePublished\":\"2016-11-18T15:37:50+00:00\",\"dateModified\":\"2016-11-18T16:18:28+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/software\/running-caddy-as-a-service-on-macos-os-x-server\/\"]}]},{\"@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\/2136"}],"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=2136"}],"version-history":[{"count":4,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2136\/revisions"}],"predecessor-version":[{"id":2191,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2136\/revisions\/2191"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=2136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=2136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=2136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}