Caddy is a cross-platform web server built in the Go and uses HTTPS by default. It stands out for its ease of use and simple configuration. It is known for being easy to configure, especially for users who do not have much experience with web server administration.
Unlike other web servers, Caddy is designed to work with HTTPS out of the box and has integration with Let's Encrypt, allowing you to automatically receive and renew certificates.
Below, we will explain how to install Caddy on Ubuntu 22.04 and how to configure it.
Why do you need Caddy and what does it offer compared to Apache or Nginx? As we said above: simplicity and security. You do not need to configure encryption parameters and protocol usage, Caddy will do everything out of the box, and in the best possible way, using the most modern technologies. It has the latest features such as HTTP/2, IPv6, Markdown, WebSockets, CreateCGI, templates, and other standard features.
The configuration itself is extremely simple, you need to set a minimum of options to get a working server, but at the same time you can manage it quite flexibly, redefining the necessary parameters. The only downside is compatibility with old systems, as Caddy automatically disables outdated protocols and ciphers.
There are four different methods to install Caddy. We can do it by simply downloading the executable binary, by compiling the source code, using docker image, or installing it from the repository. In this article, we will do the latter.
Before we start installing the Caddy web server, it is recommended that we first update the Ubuntu host system and at the same time update the package sources. We always want to benefit from the most recent releases and prefer to avoid outdated software packages.
Perform updates and upgrades:
sudo apt update && sudo apt upgrade -y
Installing Caddy requires appropriate sudo permissions on the host system. To install Caddy on Linux Ubuntu 22.04, we first start by setting up the necessary dependencies:
apt install gnupg curl apt-transport-https debian-keyring debian-archive-keyring -y
Once the installation of all dependencies for the web server has been successfully completed, we need to add the GPG key using the following command:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
Next we have to add the Caddy repository to the APT sources list, allowing Caddy to be installed from this repository:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
Update the package sources:
sudo apt update -y
Finally, the Caddy can be installed via the command line. At this point, all prerequisites, and preparations for the installation have been successfully completed. Run the command below to install.
sudo apt install caddy -y
Now that the web server has been installed on the Linux host system, the service just needs to be activated. We do this with the following command.
sudo systemctl enable --now caddy
The version you just installed can be validated with the following command:
caddy version
Is a version number displayed? Then Caddy has been successfully installed on the system.
The internal format of the caddy configuration is stored in JSON format and can be managed online via REST API, a more classic format of setting via a configuration file is also available, for this purpose the configuration file /etc/caddy/Caddyfile
is used.
It already contains an example of the configuration, and we only need to correct it. Please note that the indents in the file are formed strictly using tabulation and two, four, six, etc. indents should be used, otherwise you will receive a warning about incorrect formatting of the configuration file.
If we want to set a website with Caddy over the local network or over the Internet, we have to save the files and subdirectories associated with the website in the www
directory.
To do this, we first create a new directory for the output of a web page:
sudo mkdir -p /var/www/html
cd /var/www/html
Create a website index with the editor:
sudo nano index.html
Copy the following content and paste this example page into the index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello, I'm Caddy, your reliable web server!</title>
</head>
<body>
<p>Great projects can be created here! All that is needed is HTML and CSS knowledge, rounded off with a little JavaScript.</p>
</body>
</html>
Now we need to edit the Caddy configuration file so that the web page can be displayed. To do this, execute the following commands from your system.
sudo nano /etc/caddy/Caddyfile
Edit:
:80 {
# Path of the website
root * /var/www/html/
The website should now be accessible via the IP address from the local network.
If you want to make the website accessible via the Internet, the web ports (80/443) must be released for the server IP address within the NAT settings in the router/firewall.
To set up a domain, you first need to specify the domain's A/AAAA DNS records on this server in the DNS control panel. Then create a document root directory for the website in the /var/www/html
folder:
mkdir /var/www/html/example.com
Replace example.com
with your domain.
When using SELinux, we will change the file security context for web content:
# chcon -t httpd_sys_content_t /var/www/html/example.com -R
# chcon -t httpd_sys_rw_content_t /var/www/html/example.com -R
To configure a domain in this configuration, we will only have to replace <name> :80
with our domain. Also, if we want to change the path of our website, we will have to modify the parameter root
.
example.com {
root * /var/www/html/example
file_server
}
To reload the configuration, we have to restart the service:
systemctl reload caddy
Also, if we want, we can configure the logs for access:
example.com {
root * /var/www/html/example
file_server
log {
output file /var/log/caddy/access.log
format console
}
}
To work with dynamic content, we will need support for PHP, the scripting language in which most CMS are written. Caddy does not have its own process manager, so we will use PHP-FPM for this purpose:
sudo apt install php-fpm
Then we go to /etc/php/8.3/fpm/php.ini
and adjust some parameters. First, we find, uncomment and change the option to the following:
cgi.fix_pathinfo=0
Then we set the maximum size of the request being sent:
post_max_size = 32M
And the maximum size of the uploaded file, it must be less than or equal to the size of the request sent:
upload_max_filesize = 30M
Save the changes and restart the fpm
service:
sudo systemctl restart php8.3-fpm
Now, after adding the fpm directive, configuration will look like this:
example.com {
root * /var/www/html/example
file_server
encode zstd gzip
php_fastcgi unix//run/php/php8.3-fpm.sock # Uses PHP-FPM to serve PHP files (through a Unix socket)
log {
output file /var/log/caddy/access.log
format console
}
}
To set up a reverse proxy, add a new site block with the following structure:
example.com {
encode zstd gzip
handle_path /static/* {
root * /var/www/html/example
file_server
}
reverse_proxy localhost:3000
log {
output file /var/log/caddy/access.log
format console
}
}
This Caddyfile sets up a reverse proxy where requests to hostman.com are forwarded to localhost:3000, except for requests starting with /static/
, which are served directly from /var/www/html/example
. The reverse_proxy
directive ensures all non-static requests are proxied to the backend server at localhost:3000
.
If you are a beginner and want to set up a web server without the hassle of long configuration, Caddy is perfect for you. Even if you are an experienced user who needs an instant and simple web server, then you should pay attention to Caddy.
If you require a more sophisticated server with advanced features, then with minimal configurations you can set folder permissions, manage authentication, error pages, archiving, HTTP request redirection, and other settings.