{"id":164,"date":"2017-07-22T22:35:05","date_gmt":"2017-07-22T22:35:05","guid":{"rendered":"http:\/\/linuxresellerwebhosting.in\/blog\/?p=164"},"modified":"2017-07-22T23:58:44","modified_gmt":"2017-07-22T23:58:44","slug":"install-caddy-web-server-host-website-centos-7","status":"publish","type":"post","link":"https:\/\/linuxresellerwebhosting.in\/blog\/install-caddy-web-server-host-website-centos-7\/","title":{"rendered":"How to Install Caddy Web Server to Host a Website on CentOS 7"},"content":{"rendered":"<p>Caddy is one of the webserver. It is simple enough to be used as a quick development server and strong enough to be used in productions.<\/p>\n<p>Basic Features of Caddy :<br \/>\nAn easy, intuitive way to configure your site. It&#8217;s not scripting, and not hard to memorize.<br \/>\nCaddy can be extended with plugins. All server types, directives, DNS providers, and more features are plugins! They&#8217;re easy to write and get compiled in directly.<br \/>\nWhen the going gets tough, Caddy gets going on more CPUs. Go&#8217;s scheduler understands Go code, and goroutines are more lightweight than system threads. So yeah, it&#8217;s fast.<br \/>\nCaddy can be configured to run system commands at startup and shutdown. Useful when your site requires other processes running.<br \/>\nCaddy can write a log of all its significant events, especially errors. Log to a file, stdout\/stderr, or a local or remote system log!<\/p>\n<p>Step 1 -&gt; Installing the Caddy on CentOS :<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">$ wget -s https:\/\/getcaddy.com | bash<\/pre>\n<p>Type and execute the above command to install caddy server&#8217;s binary files.<\/p>\n<p>The command output will look like this:<\/p>\n<p>Downloading Caddy for linux\/amd64&#8230;<br \/>\nhttps:\/\/caddyserver.com\/download\/linux\/amd64?plugins=<br \/>\nExtracting&#8230;<br \/>\nPutting caddy in \/usr\/local\/bin (may require password)<br \/>\n[sudo] password for sammy:<br \/>\nCaddy 0.10.2<br \/>\nSuccessfully installed<\/p>\n<p>To verify the caddy binary files location, use the below command<\/p>\n<p>$ which caddy<\/p>\n<p>Steps 2 -&gt; Creating User and Group for Caddy<\/p>\n<p>To create user named caddy let&#8217;s type:<\/p>\n<p>$ sudo adduser -r -d \/var\/www -s \/sbin\/nologin caddy<\/p>\n<p>Here \/var\/www is the home directory for the user caddy.<\/p>\n<p>Step 3 -&gt; Configuring Directories for Caddy&#8217;s files<\/p>\n<p>First, create a directory that will house the main Caddyfile, which is a configuration file that tells Caddy what websites should it serve and how.<\/p>\n<p>First, create a directory for the main Caddyfile, which is a configuration file that tells Caddy what websites should it serve and how.<\/p>\n<p>sudo mkdir \/etc\/caddy<\/p>\n<p>Change the owner of this directory to the root user and its group to www-data so Caddy can read it.<\/p>\n<p>sudo chown -R root:caddy \/etc\/caddy<\/p>\n<p>In this directory, create an empty Caddyfile which we&#8217;ll edit later.<\/p>\n<p>sudo touch \/etc\/caddy\/Caddyfile<\/p>\n<p>Create another directory in \/etc\/ssl to store the SSL private keys and certificates that it automatically obtains from Let&#8217;s Encrypt.<\/p>\n<p>sudo mkdir \/etc\/ssl\/caddy<\/p>\n<p>Caddy needs to be able to write to this directory when it obtains the certificate, so make the owner the caddy user<\/p>\n<p>sudo chown -R caddy:root \/etc\/ssl\/caddy<\/p>\n<p>Then make sure no one else can read those files by removing all the access rights for others.<\/p>\n<p>sudo chmod 0770 \/etc\/ssl\/caddy<\/p>\n<p>Make a directory that should be completely owned by caddy.<\/p>\n<p>sudo mkdir \/var\/www<\/p>\n<p>sudo chown caddy:caddy \/var\/www<\/p>\n<p>Step 4 -&gt; Configuring Caddy as a System Service<\/p>\n<p>While Caddy does not install itself as a service, the project provides an official systemd unit file. This file does assume the directory structure we set up in the previous step, so make sure your configuration matches.<\/p>\n<p>Download the file from the official Caddy repository. The additional -o parameter to the curl command will save the file in the \/etc\/systemd\/system\/ directory and make it visible to systemd.<\/p>\n<p>&nbsp;<\/p>\n<p>$ sudo curl -s https:\/\/raw.githubusercontent.com\/mholt\/caddy\/master\/dist\/init\/linux-systemd\/caddy.service -o \/etc\/systemd\/system\/caddy.service<\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s open the file with vi or your favourite text editor to modify the file slightly to make it use our unprivileged caddy user to run the server.<\/p>\n<p>$ sudo vi \/etc\/systemd\/system\/caddy.service<\/p>\n<p>\/etc\/systemd\/system\/caddy.service<br \/>\n; User and group the process will run as.<br \/>\nUser=www-data<br \/>\nGroup=www-data<\/p>\n<p>Find the above lines and Change both values to caddy as follows:<\/p>\n<p>; User and group the process will run as.<br \/>\nUser=caddy<br \/>\nGroup=caddy<\/p>\n<p>Save and close the file to exit.<\/p>\n<p>To make systemd aware of the new service file.<\/p>\n<p>$ sudo systemctl daemon-reload<\/p>\n<p>Enable Caddy to run on boot.<\/p>\n<p>$ sudo systemctl enable caddy.service<\/p>\n<p>To verify that the service has been properly loaded and enabled to start on boot by checking its status.<\/p>\n<p>$ sudo systemctl status caddy.service<\/p>\n<p>The output should look as follows:<\/p>\n<p>\u25cf caddy.service &#8211; Caddy HTTP\/2 web server<br \/>\nLoaded: loaded (\/etc\/systemd\/system\/caddy.service; enabled; vendor preset: disabled)<br \/>\nActive: inactive (dead)<br \/>\nDocs: https:\/\/caddyserver.com\/docs<\/p>\n<p>You have now configured Caddy as a system service which will start automatically on boot without the need to run it manually.<\/p>\n<p>Step 5 -&gt; Host a Test Website<\/p>\n<p>This command will create an index.html file in the website directory we created earlier with just the one line of text, &lt;h1&gt;CADDY WEBSITE&lt;\/h1&gt;, inside.<\/p>\n<p>echo &#8216;&lt;h1&gt;CADDY WEBSITE&lt;\/h1&gt;&#8217; | sudo tee \/var\/www\/index.html<\/p>\n<p>Open the Caddyfile you created in Step 2 using vi or your favorite text editor.<\/p>\n<p>http:\/\/ {<br \/>\nroot \/var\/www<br \/>\ngzip<br \/>\n}<\/p>\n<p>Then save the file and exit. Let&#8217;s explain what this specific Caddyfile does.<\/p>\n<p>Once the configuration file is ready, start the Caddy service.<\/p>\n<p>$ sudo systemctl start caddy<\/p>\n<p>First, replace the address definition of http:\/\/ with your domain. This removes the insecure connection forced by HTTP and provides a domain name for the TLS certificate. Second, provide Caddy with an email address using the tls directive inside the server block.<\/p>\n<p>The modified Caddyfile should look as follows, with your domain and email address substituted in:<\/p>\n<p>example.com {<br \/>\nroot \/var\/www<br \/>\ngzip<br \/>\ntls anbupriyan@example.com<br \/>\n}<\/p>\n<p>Save the file and exit the editor. To apply the changes, restart Caddy.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">$ sudo systemctl restart caddy<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Caddy is one of the webserver. It is simple enough to be used as a quick development server and strong<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[1],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":5,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxresellerwebhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}