Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrajo/c1959f76610ecadf851792ba05f2a2bd to your computer and use it in GitHub Desktop.
Save mrajo/c1959f76610ecadf851792ba05f2a2bd to your computer and use it in GitHub Desktop.
Caddy setup on Ubuntu/Digital Ocean

Create droplet

  1. Use DO dashboard
  2. Add SSH key for login
    • This does not create a root password, and login must be through SSH only
  3. Create DO firewall
    • Add HTTP and HTTPS to inbound rules
  4. After created, login as root through SSH
    • $ ssh root@<ip_of_droplet>
  5. Create new user and add to sudo and www-data group group
    • $ adduser <username>
    • $ usermod -aG sudo,www-data <username>
  6. Setup SSH for new user
    • $ su - <username>
    • $ mkdir ~/.ssh
    • $ chmod 700 ~/.ssh
    • $ vim ~/.ssh/authorized_keys
      • put ssh pub key here
    • $ chmod 600 ~/.ssh/authorized_keys
  7. Make sure SSH login to new user works
  8. Disable password authentication
    • $ sudo vim /etc/ssh/sshd_config
    • This step appears unnecessary if you created the droplet with SSH key; it's already disabled

DNS

  1. Add domain name in DO dashboard in Networking -> Domains
  2. Set nameservers in registrar:
    • ns(1-3).digitalocean.com
  3. Add "A" record to point to droplet

Transferring Webfaction sites

  1. Get list of sites in ~/webapps
    • SSH into Webfaction
      • $ ls ~/webapps > sites.txt
    • Exit SSH
      • $ scp <user>@<webfaction>:/home/<user>/sites.txt .
    • Download site folders as needed with "scp -r" or "rsync -arvz"

Create web document root

  1. $ sudo mkdir /var/www
  2. $ sudo chown www-data:www-data /var/www
  3. $ sudo chmod 775 /var/www
  4. Create folders for each site
    • $ mkdir /var/www/example.com
    • $ touch /var/www/example.com/index.html
  5. Edit index.html
<!DOCTYPE html>
<html>
    <body>
        <h1>Hello</h1>
    </body>
</html>

Install Caddy

  1. Setup installer at https://caddyserver.com/download with plugins:
    • http.cache
    • http.cors
    • http.expires
    • http.filter
    • http.git
    • http.ratelimit
    • http.supervisor
    • tls.dns.digitalocean
  2. Run installer
  3. Verify caddy runs (caddy -version)
  4. Set permissions on caddy binary
    • $ sudo chown root:root /usr/local/bin/caddy
    • $ sudo chmod 755 /usr/local/bin/caddy
  5. Allow caddy to use privileged ports (80,443) without root
    • $ sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy
  6. Create caddy config dir
    • $ sudo mkdir /etc/caddy
    • $ sudo chown root:www-data /etc/caddy
  7. Create SSL dir
    • $ sudo mkdir /etc/ssl/caddy
    • $ sudo chown root:www-data /etc/ssl/caddy
    • $ sudo chmod 770 /etc/ssl/caddy

Setup Caddyfile

  1. Create config file
    • $ sudo touch /etc/caddy/Caddyfile
  2. Edit Caddyfile with contents:
    example.com {
        root /var/www/example.com
        gzip
        tls {
            dns digitalocean
        }
    }
    

Create systemd service

Create service from caddy service definition in github

$ curl -L https://github.com/mholt/caddy/raw/master/dist/init/linux-systemd/caddy.service | sed "s/;CapabilityBoundingSet/CapabilityBoundingSet/" | sed "s/;AmbientCapabilities/AmbientCapabilities/" | sed "s/;NoNewPrivileges/NoNewPrivileges/" | sudo tee /etc/systemd/system/caddy.service
$ sudo chown root:root /etc/systemd/system/caddy.service
$ sudo chmod 744 /etc/systemd/system/caddy.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable caddy.service
$ sudo systemctl status caddy

Configure automatic HTTPS cert challenge

  1. Go to Digital Ocean API dashboard (https://cloud.digitalocean.com/settings/api)
  2. Create token named "caddy-dns" with Write scope
  3. Copy token somewhere
  4. Edit service
    • $ sudo vim /etc/systemd/systemd/caddy.service
    • Edit line starting
      • Environment=CADDYPATH=/etc/ssl/caddy
    • Change to
      • Environment=CADDYPATH=/etc/ssl/caddy DO_AUTH_TOKEN=<token generated from dashboard>
    • Restart systemd
      • $ sudo systemctl daemon-reload

Start/enable caddy service

  1. $ sudo systemctl start caddy
  2. $ sudo systemctl status caddy (should show successfully running and DNS challenge succeeded)

Done, check site in browser

@melzworld
Copy link

This is really great and all, but just an FYI: on the Configure automatic HTTPS cert challenge, when you edit the service (step 4) it's going to be $ sudo nano /etc/systemd/system/caddy.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment