
Caddy is an open-source web server built with Go, designed for serving static and dynamic web applications with automatic HTTPS for all linked domains. Its simple configuration makes it easy to use as a web server, reverse proxy, or load balancer.
In this article, you are to install the Caddy web server on Ubuntu 22.04 and securely serve web applications on the server.
Before you begin:
Have an Ubuntu 22.04 server.
Create a domain name A record pointing to the server IP address.
Access the server using SSH as a non-root user with sudo privileges.
Follow the steps in this section to download the latest Caddy repository information and install the application on your server.
Add the latest Caddy GPG Key to your server.
Add the Caddy repository to your APT sources.
Update the server package index.
Install Caddy.
View the installed Caddy version to verify that the installation is successful.
Output:
Allow incoming connections to the HTTP port 80 through the firewall.
Restart the firewall to apply changes.
Access your server IP to test access to the Caddy web server.
Confirm that the default Caddy webpage displays in your browser.
Enable the service to start at boot time.
Start the Caddy web server.
View the Caddy system service status to verify that the application is running.
Output:
Caddy stores its configuration files in /etc/caddy by default but can use Caddyfile configurations from any location. Follow the steps in this section to create a new Caddy virtual host to serve web files from the /var/www/example.com directory.
Create the /var/www/example.com web application files directory.
Create a new HTML application file index.html.
Add the following code to the file.
Save and close the file.
Switch to the Caddy configuration files directory.
Back up the default Caddyfile configuration.
Create a new Caddyfile configuration.
Add the following configurations to the file.
Save and exit the file.
The above Caddy configuration creates a new virtual host using your domain example.com. Within the configuration:
example.com: Defines a new virtual host using your domain or IP address.tls: Associates an email address with Let's Encrypt SSL certificate requests.root: Specifies the directory for web application files.file_server: Enables the file server, with index specifying the default file to serve when accessed.log: Enables logging of access and errors to a file, such as /var/log/caddy/example.log.Test the Caddy configuration for errors.
Reload the Caddy web server to apply your configuration changes.
Caddy automatically enables HTTPS to secure connections with SSL certificates for virtual hosts with valid domains. Follow the steps in this section to secure the Caddy web server by restricting access to the Caddyfile configurations from unauthorized changes.
Grant the Caddy user full privileges to the /etc/caddy directory.
Grant the Caddy user read and write permissions to the Caddyfile while disabling access for other system users.
Long list the /etc/caddy directory to verify the permission changes.
Output:
Caddy uses HTTP port 80 and HTTPS port 443 based on your Caddyfile configurations to serve files. Follow the steps in this section to allow both Caddy ports through the firewall and enable network connections to the web server.
View the UFW status and verify that it's active.
If the status is inactive, allow the SSH port 22 and enable UFW.
Allow incoming connections to the HTTPS port 443.
Reload the firewall to apply changes
Access your domain using a browser such as Chrome to verify that Caddy serves your virtual host web application fils.
If you receive a connection error, view the Caddy configuration logs to verify the error details.
In this article, you installed the Caddy web server on an Ubuntu 22.04 server and set up a virtual host profile to serve web application files on the server. Visit the Caddy documentation for more information and configuration options.
0 Comments
Be the first to comment and share your perspective with the community.