How to Install & Configure the Caddy web server on an Ubuntu 18.04 VPS

Eliran Ouzan
Written by:
Jul. 26, 2018

Caddy web server is a free open source web server written in go language. It is mostly known for its HTTP functionality and enabling HTPPS by default. It can run on most common systems including Windows, OS X and Linux.

In this how-to article, we cover the steps of how to install Caddy web server on Ubuntu 18.04 VPS.

Overview

It means that you can move your site from windows to Linux without worrying about compatibility issues.

Some of its features include;

  • It is self-contained hence no compilation is needed – since it’s written in the go language, it has no external dependencies.
  • Easy to use – it avoids using regular expressions since they are a bit tricky.
  • Secure by default
  • Use of plugins can extend the features offered by caddy.
  • It writes all its errors in a log file, thus enabling you to troubleshoot quickly.
  • Caddy can be used as a reverse proxy or a library in your Go program.
  • Caddy is fast; it can utilise more CPUs.

Prerequisite

  • VPS server running Ubuntu 18.04
  • SSH client

How to Install & Configure the Caddy web server on an Ubuntu 18.04 VPS

SSH to VPS server

Log in to your VPS via SSH by using the following.

$ ssh user@vps_IP

Update packages

Update the packages by running the following commands in the terminal.

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

Install Caddy web server

Then to install the caddy web server run the following command;

$ curl https://getcaddy.com | bash -s personal

You can also install caddy with some additional features, use the –s switch with a comma

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

Then we can now add cap_net_bind_servicecapability for caddy to bind to ports less than 1024

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

Configure Caddy

We now have to set up the directories that will be used to store the caddy configuration files and SSL certificates. Therefore, run the following commands.

$ 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

After that, we need to create a systemD configuration script, therefore, run the following;

$ sudo nano /lib/systemd/system/caddy.service

Then add the following to the caddy service file;

[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 here in 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 restrict privileges that can be gained by caddy. Uncomment if you want.
; You may need to add capabilities required by the plugins in use.
;CapabilityBoundingSet=CAP_NET_BIND_SERVICE
;AmbientCapabilities=CAP_NET_BIND_SERVICE
;NoNewPrivileges=true
[Install]
WantedBy=multi-user.target

Save the file by pressing control key + X then overwriting the original caddy file. Then, to enable caddy to run on boot, execute the following commands;

sudo systemctl enable caddy.service

Testing the installation

We are going to create a small webpage and a caddyfile. Therefore, type the following in your shell.

$ 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

Then to add our domain to the caddy file, type the following;

$ sudo nano /etc/caddy/Caddyfile

Then add the following;

my-domain.com {
    root /var/www/ ateamagencies.000webhostapp.com
}

Then save the file, close the editor and restart caddy.

$ sudo systemctl restart caddy.service

Now, using your browser, go to the https:// ateamagencies.000webhostapp.com the test page we just created.

Conclusion

While caddy isn’t truly unique, it does offer significant advantages over other web servers.

These among other features make caddy an excellent alternative to common web servers. Now that we have installed caddy on our system, we hope you enjoy these features.

 

Check out these top 3 Linux hosting services

9.7
9.6
9.7
9.8
9.7
8.9
8.9
9.1
8.9
8.7
9.8
9.4
9.7
9.8
9.7

Was this article helpful?