# Caddy To setup Caddy you must be root ( `sudo -s` ). Add the repo... ``` apt install -y curl debian-keyring debian-archive-keyring apt-transport-https curl 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' -o /etc/apt/trusted.gpg.d/caddy_repo_signing.asc curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list ``` --- Now update apt & install it... ``` apt update apt install caddy ``` --- Once installed we need to make a backup of the default Caddyfile and replace it with our own... ``` mv -iv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.old cp -v ./Caddyfile /etc/caddy/Caddyfile ``` --- We need somewhere to serve sites... ``` mkdir -v /var/www ``` ## Site setup Create the site's base directory but don't include `www.` and change to it... ``` mkdir -v /var/www/example.com cd /var/www/example.com ``` **Make sure you're in the right directory before continuing.** You can use a tilde `~` in your terminal to see your current directory. --- The site needs some folders... ``` mkdir -v htdocs mkdir data tmp sessions ``` `htdocs` is where the site's public-accessible files are kept, `data` is for private site files, `tmp` is for temporary site files - such as uploads, and `sessions` is for site vistor session data. --- Everyone on the system can access the site's files and we don't want that, change the folder(s) permissions... **Take note of the `.` in the command below do not just enter `/` !** ``` chmod -Rv 750 ./ ``` --- Drat, only root can access the folders now, but Caddy and others need to be able to read the htdocs folder too... ``` chmod -Rv 755 htdocs ``` --- If you want another user on the system to own the files, say we have user `fred` and they're in group `fred`... **Take note of the `.` in the command below do not just enter `/` !** ``` chown -Rv fred:fred ./* ``` If `fred` is in a different user group and you don't know which, you can run `groups fred` to find out! --- ## Things to know The `Caddyfile` included here will (in this order)... * Check if the requested host (without `www.`) is served here, if not return 404. * If the requested file exists serve it. The files index.html index.php take precedence and will always be served if no path is given. Requests where the requested path/file doesn't exist will be passed on to the other handlers (described below). * Reverse proxy the request if a socket matching the hostname (without `www.`) exists in `/run/`. This can be any service that understands how to handle HTTP requests. It just needs to be setup to listen via a socket matching the hostname in `/run/`, e.g. `/run/myawesomesite.com.sock`. * If the above socket does not exist and/or a php file is requested, attempt to pass along the request to php-fpm (setup to listen via a socket matching the hostname in `/run/php`, e.g. `/run/php/myawesomesite.com.sock`). * Return 404 if the request cannot be handled by any of the above.