install caddy on ubuntu

How to install Caddy on Ubuntu 16.04

In this tutorial, we will show you how to install Caddy on a Ubuntu 16.04 VPS. Caddy is a modern, general-purpose, multi-platform web server which supports Virtual hosting, HTTP/2, IPv6, Markdown, WebSockets, FastCGI, automatic HTTPS via Let’s Encrypt, templates and more. This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 16.04 VPS.

Log in to your VPS via SSH

ssh user@vps_IP

Update the system and install all necessary packages

sudo apt-get update && apt-get -y upgrade
sudo apt-get install curl

Install Caddy

Installing Caddy is quick and easy:

curl https://getcaddy.com | bash

If you want to install Caddy with some extra features, you can use the -s switch with a comma-separated list of directives, like in the following example:

curl https://getcaddy.com | bash -s realip,expires,upload

Once the installation is completed, we need to add the cap_net_bind_servicecapability to the Caddy binary. This capability will allow the Caddy executable to bind to a port less than 1024.

sudo setcap cap_net_bind_service=+ep /usr/local/bin/caddy

Next, create the directories where we will store the Caddy configuration file Caddyfile and SSL certificates. The term “Caddyfile” is a plaintext configuration file used to configure how Caddy works. This configuration file is very similar in purpose to httpd.conf in Apache or nginx.conf in Nginx.

sudo mkdir /etc/caddy
sudo chown -R root:www-data /etc/caddy
sudo mkdir /etc/ssl/caddy
sudo chown -R www-data:root /etc/ssl/caddy
sudo chmod 0770 /etc/ssl/caddy
sudo touch /etc/caddy/Caddyfile
sudo mkdir /var/www
sudo chown www-data: /var/www

SystemD Configuration

We also need to create a new SystemD configuration script:

sudo nano /lib/systemd/system/caddy.service
[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target

[Service]
Restart=on-failure
StartLimitInterval=86400
StartLimitBurst=5

User=www-data
Group=www-data
; Letsencrypt-issued certificates will be written to this directory.
Environment=CADDYPATH=/etc/ssl/caddy

ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp
ExecReload=/bin/kill -USR1 $MAINPID

LimitNOFILE=1048576
LimitNPROC=64

PrivateTmp=true
PrivateDevices=true
ProtectHome=true
ProtectSystem=full
ReadWriteDirectories=/etc/ssl/caddy

; The following additional security directives only work with systemd v229 or later.
; They further retrict privileges that can be gained by caddy. Uncomment if you like.
; Note that you may have to add capabilities required by any plugins in use.
;CapabilityBoundingSet=CAP_NET_BIND_SERVICE
;AmbientCapabilities=CAP_NET_BIND_SERVICE
;NoNewPrivileges=true
[Install]
WantedBy=multi-user.target

Enable Caddy to start on boot:

sudo systemctl enable caddy.service

Testing Caddy

For testing purposes, we will create a test HTML file:

sudo mkdir -p /var/www/my-domain.com
sudo echo "Caddy" > /var/www/my-domain.com/index.html
sudo chown -R www-data: /var/www/my-domain.com

and add our domain to the Caddy configuration file.

sudo nano /etc/caddy/Caddyfile
my-domain.com {
    root /var/www/my-domain.com
}

Start Caddy with the following command:

sudo systemctl start caddy.service

You can now access your domain at https://my-domain.com.

For more information about how to manage your Caddy web server, please refer to the Caddy documentation.


Of course, you don’t have to do any of this if you use one of our Server Setup and Optimization Services, in which case you can simply ask our expert Linux admins to install Caddy on your server for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *