@JS's Notes

Site with notes from my work.

Caddy Web Server on Centos 8 - mini how-to

2020-07-16 System @JS

My test platform: CentOS Linux release 8.2.2004 (Core)

Requirements: user with root privileges or non-root user with sudo privileges.

$ sudo dnf update -y
$ sudo dnf install 'dnf-command(copr)'
$ sudo dnf copr enable @caddy/caddy
$ sudo dnf install caddy -y
$ sudo systemctl enable caddy.service
$ sudo systemctl start caddy.service; systemctl status caddy.service
## my test local domain: ctx08vm.local.lnxorg
$ mkdir /var/www/html/ctx08vm.local.lnxorg
$ sudo touch /var/www/html/ctx08vm.local.lnxorg/index.html
$ echo '<!doctype html><head><title>Caddy Test Page</title></head><body><h1>Hello, World!</h1></body></html>' | sudo tee /var/www/html/ctx08vm.local.lnxorg/index.html
$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload

If you’re using SELinux, you need to change the file security context for web content:

$ chcon -t httpd_sys_content_t /var/www/html/ctx08vm.local.lnxorg -R
$ chcon -t httpd_sys_rw_content_t /var/www/html/ctx08vm.local.lnxorg -R

Open and edit the caddy configuration file at /etc/caddy/Caddyfile as shown:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.
:80

# Set this path to your site's directory.
# root * /usr/share/caddy
root * /var/www/html/ctx08vm.local.lnxorg

# Enable the static file server.
file_server

# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

Restart the Caddy service:

$ sudo systemctl restart caddy.service; systemctl restart caddy.service
$ curl http://localhost

Reference: Caddy DOCS.