How To Install Caddy Web Server on Ubuntu 16.04 LTS

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 article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation Caddy Web Server on a Ubuntu 16.04 Xenial Xerus server.

Install Caddy Web Server on Ubuntu 16.04 LTS

Step 1. First make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

sudo apt-get update
sudo apt-get upgrade

Step 2. Installing Caddy Web Server.

Install Caddy is quick and easy with run the following command:

curl https://getcaddy.com | bash

Once the installation is completed, we need to add the cap_net_bind_servicecapability to the Caddy binary:

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

Step 3. Setting Up Necessary Directories.

Next, create the directories where we will store the Caddy configuration file Caddyfile and SSL certificates:

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

Step 4. Installing Caddy as a System Service.

We also need to create a new SystemD configuration script:

nano /lib/systemd/system/caddy.service

Add following line:

[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

Execute the following commands to enable Caddy to run on boot:

systemctl enable caddy.service
systemctl restart caddy.service

Step 5. Creating Test Web Page and a Caddyfile.

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

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

Next, add our domain to the Caddy configuration file:

nano /etc/caddy/Caddyfile

Add following line:

my-domain.com {
    root /var/www/wpcademy.com
}

Save the file and exit the editor. To apply the changes, restart Caddy:

systemctl restart caddy.service

Now, with a web browser, just go to https://wpcademy.com, and you will see our test page!

Congratulation’s! You have successfully installed Caddy. Thanks for using this tutorial for installing Caddy web server in Ubuntu 16.04 Xenial Xerus system. For additional help or useful information, we recommend you to check the official Caddy web server web site.

Leave a Reply