{"id":1885,"date":"2015-08-08T13:42:07","date_gmt":"2015-08-08T12:42:07","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1885"},"modified":"2016-01-28T15:49:46","modified_gmt":"2016-01-28T14:49:46","slug":"running-caddy-server-as-a-service","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service\/","title":{"rendered":"Running Caddy Server as a service with Upstart"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\">Update<\/span>: Need to run Caddy as service with systemd? Checkout <a href=\"http:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service-with-systemd\/\">this blogpost<\/a>!<\/p>\n<p>Recently I discovered a very promising webserver (written in Go) called <a href=\"https:\/\/caddyserver.com\" target=\"_blank\">Caddy<\/a>.<\/p>\n<p>Now, Caddy is just an executable, which I wanted to turn into a service on my Ubuntu server.<\/p>\n<h3>Upstart<\/h3>\n<p>Running any executable as a service can be done using <a href=\"http:\/\/upstart.ubuntu.com\/cookbook\/\" target=\"_blank\">Upstart<\/a>. Once you&#8217;ve created a config file in <code>\/etc\/init\/my_service.conf<\/code>, you can use some simple commands like <code>service my_service start<\/code>, <code>service my_service stop<\/code>, &#8230; and the service will run at boot time.<\/p>\n<p>The output of the program will be logged to\u00a0<code>\/var\/log\/upstart\/my_service.log<\/code>.<\/p>\n<h3>Caddy executable<\/h3>\n<p>To allow a non-root user to listen on port 80, you must allow the executable to do so:<\/p>\n<pre><code>$ sudo setcap cap_net_bind_service=+ep .\/caddy<\/code><\/pre>\n<h3>Config file<\/h3>\n<p>My configuration file looks like this:<\/p>\n<pre><code>description \"Caddy Server startup script\"\r\nauthor \"Mathias Beke\"\r\n\r\nstart on runlevel [2345]\r\nstop on runlevel [016]\r\n\r\n\r\nsetuid some-caddy-user\r\nsetgid some-caddy-group\r\n\r\nrespawn\r\nrespawn limit 10 5\r\n\r\nlimit nofile 4096 4096\r\n\r\nscript\r\n    cd \/home\/mathias\/\r\n    exec .\/caddy\r\nend script<\/code><\/pre>\n<p>Explanation of the commands:<\/p>\n<ul>\n<li><code>start on runlevel [2245]<\/code> specifies the job needs to be started after the basic facilities are up.<\/li>\n<li><code>stop on runlevel [016]<\/code> is used for a normal shutdown.<\/li>\n<li><code>setuid<\/code> and <code>setgid<\/code> 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&#8217;t want to run a webserver with root access!<\/li>\n<li><code>respawn<\/code> will automatically restart (well&#8230; &#8216;respawn&#8217;) the process when it stops for any reason.<\/li>\n<li><code>respawn limit COUNT INTERVAL | unlimited<\/code> specifies a limit for the automatic respawn of the process.<br \/>\n<blockquote><p><em>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.<\/em><\/p>\n<p><em>To have the job respawn indefinitely, specify an argument of &#8220;unlimited&#8221;. However, care should be taken using this option: does your service really stop that frequently? Should it?<\/em><\/p><\/blockquote>\n<p style=\"text-align: right;\">\u00a0<a href=\"http:\/\/upstart.ubuntu.com\/cookbook\/\" target=\"_blank\">Upstart Cookbook<\/a><\/p>\n<\/li>\n<li><code>exec<\/code>\u00a0executes the actual command.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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,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 Upstart &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\/\" \/>\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 Upstart &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-08T12:42:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-01-28T14:49:46+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\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/servers\/running-caddy-server-as-a-service\/\",\"name\":\"Running Caddy Server as a service with Upstart &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2015-08-08T12:42:07+00:00\",\"dateModified\":\"2016-01-28T14:49:46+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\/\"]}]},{\"@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\/1885"}],"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=1885"}],"version-history":[{"count":12,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1885\/revisions"}],"predecessor-version":[{"id":2024,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1885\/revisions\/2024"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}