
Caddy is an open-source modern web server application that enables the delivery of static sites, services or web applications with automatic SSL termination. Caddy can function as a web server, load balancer, or reverse proxy to deliver web applications or services using site blocks. A Caddyfile configuration contains directives with specific site blocks that use ports, IP addresses, or domains.
This article explains how to install the Caddy web server on FreeBSD 14.0 and create virtual hosts using site blocks to securely deliver web applications.
Before you begin:
app.example.com.Caddy is available in the default FreeBSD 14.0 package repositories with the latest version information. You can also install a specific version using the FreeBSD ports collection or build and compile the application's source code. Follow the steps below to install Caddy using the default pkg package manager on your server.
Update the server's package index.
Install Caddy.
View the installed Caddy version on your server.
Your output should be similar to the one below:
The Caddy system service does not automatically start on your FreeBSD server after installation. Follow the steps below to enable Caddy to start at system boot and manage the web server processes.
Enable the Caddy service to automatically start at system boot.
Start the Caddy service.
View the Caddy service status to verify that the application is running.
Output:
Stop the Caddy service.
Restart the Caddy service.
Caddy uses a Caddyfile configuration to store virtual hosts as site blocks on your server. The default /usr/local/etc/caddy/Caddyfile configuration contains virtual host directives that serve web applications using the default web root directory /usr/local/www/Caddy. Follow the steps below to create a new Caddy virtual host to deliver web application files on your server.
Back up the default Caddyfile configuration on your server.
Create a new Caddyfile configuration using a text editor such as vi.
Add the following configurations to the file. Replace app.example.com with your actual domain.
Save and close the file.
The above Caddy site block configuration creates a new virtual host that listens for connection requests using your domain app.example.com to deliver web application files from the /usr/local/www/app.example.com directory. Within the configuration:
app.example.com: Creates a new site block using your virtual host domain app.example.com.root: Sets the web root directory to deliver web application files.file_server: Enables the built-in file server to deliver static files from the web root directory.Validate the Caddy configuration to test for syntax errors.
Your output should be similar to the one below when successful:
Create the virtual host's web root directory /usr/local/www/app.example.com to store your web application files.
Create a new sample HTML application file index.html.
Add the following contents to the file.
Save and close the file.
The above HTML application displays a Greetings from Vultr message when accessed in a web browser.
Reload the Caddy service to apply your configuration changes.
Access your domain using a web browser such as Chrome and verify that your HTML application displays.
Caddy automatically enables HTTPS using trusted Let's Encrypt SSL certificates for all valid domain site blocks in your Caddyfile configuration. Follow the steps below to use the FreeBSD ipfw firewall utility to enable connections to the Caddy HTTP port 80, and HTTPS port 443 on your server.
Enable the ipfw firewall service to automatically start at system boot.
Output:
Set the ipfw firewall debug messages mode to quiet.
Output:
Set the firewall mode to workstation to use stateful rules.
Output:
Allow connections to the SSH, HTTP, and HTTPS ports through the firewall configuration.
Output:
Allow connection requests from any IP address.
Output
Start the ipfw firewall service.
Output:
You have installed the Caddy web server on FreeBSD 14.0 and created a virtual host to deliver web applications on your server. Caddy automatically enables automatic HTTPS connections using valid SSL certificates that auto-renew every 90 days. You can host multiple web applications, set up a reverse proxy, or load balance between multiple applications using Caddy on your server. For more information and configuration options, visit the Caddy documentation.
0 Comments
Be the first to comment and share your perspective with the community.