In this project, we will be showing you how to set up a Raspberry Pi Caddy web server.

While Caddy is not nearly as performance-friendly as NGINX on the Raspberry Pi, it is packed with user-friendly features and is designed to utilize modern web technologies right out of the box.
Caddy utilizes modern web technologies like HTTP/2 and HTTP/3 from the get-go and automatically provisions SSL certificates for any domain name that you set up through your Caddyfile. It does this by automatically grabbing these certificates from Let’s Encrypt.
A bonus Caddy’s Caddyfile system is relatively easy to use once you get the hang of it, and luckily, they have solid documentation on most features.
Equipment List
Below are all the pieces that I used for this Raspberry Pi Caddy web server tutorial. I highly recommend using an Ethernet cable for the best performance with any server-related projects.
Recommended
Optional
- Raspberry Pi Case Amazon
This tutorial was last tested on a Raspberry Pi 5 running the latest version of Raspberry Pi OS Trixie.
Setting up Caddy on the Raspberry Pi
1. One thing we must do before we start installing and setting up Caddy on our Raspberry Pi is to ensure that we have everything up to date.
Run the following two commands to update your Raspberry Pi’s packages to their latest versions.
sudo apt update
sudo apt upgrade -yCopy
2. Before we get started with setting up our Raspberry Pi Caddy web server, we will first have to remove Apache2, which is installed by default on some copies of Raspberry Pi OS.
If you already know that you don’t have Apache2 installed on the Raspberry Pi, you can skip this command and move on to the next step.
sudo apt remove apache2 -yCopy
3. Now we can add Caddy’s official package repository and install Caddy by running the following commands. These are based on the commands in Caddy’s install guide.
sudo apt install -y curl gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install -y caddyCopy
The first command makes sure curl and gpg are installed. The next two download the key Caddy uses to sign its packages and add Caddy’s repository to your Raspberry Pi. Lastly, we update the package list and install Caddy from this new repository.
4. Once Caddy has finished installing, it will already be running as a service and will start automatically whenever your Raspberry Pi boots.
Caddy reads its configuration from the /etc/caddy/Caddyfile file, which currently holds an example configuration. Rename this file using the following command so we can write our own while keeping the example for future reference.
sudo mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.bakCopy
5. Next, we will create a folder to keep our first website in. We will name it localhost for now.
However, if you want to utilize this for a domain name, we recommend replacing the folder name localhost with your website domain name, such as pimylifeup.com
sudo mkdir -p /var/www/localhostCopy
6. With our website’s folder created, we can now get to the interesting part, and that is making our Caddyfile. This file holds all our configurations for the Raspberry Pi Caddy web server.
This file is stored in the /etc/caddy folder that Caddy created when we installed it.
Run the nano command below on our Raspberry Pi to start creating our Caddyfile.
sudo nano /etc/caddy/CaddyfileCopy
7. In this file, write the following lines. These lines define the address to listen on and where the files we want to serve are. They also tell Caddy to use GZIP compression and to serve the files in that folder
Remember to replace localhost with the name of the folder you created earlier.
If you are using a domain name, replace :80 localhost:80 with it, so, for example, the line would end up like pimylifeup.com {
:80 localhost:80 {
root * /var/www/localhost
encode gzip
file_server
}Copy
Once you’re finished doing that, you can save and exit out of the file by pressing CTRL + X and then Y and lastly ENTER.
8. With our Caddyfile now set up, we will move on to making a sample HTML file to ensure it’s still working. We’ll go back to editing our Caddyfile when we set up PHP.
For now, let’s make our test index file by running the following command. Again replace localhost with whatever you set earlier.
sudo nano /var/www/localhost/index.htmlCopy
9. In this file, write anything, as all we need it for is to make sure it is working correctly. For our tutorial, we will just be writing this small bit of HTML.
<h1>This is a test for pimylifeup.com</h1>Copy
Once you have done this, you can save and quit by pressing CTRL + X then Y and then pressing ENTER.
10. To get Caddy to load our new Caddyfile, reload its service by running the following command.
sudo systemctl reload caddyCopy
If your Caddyfile has a mistake, this command will fail, and Caddy will keep running its previous configuration. You can see the error by running sudo systemctl status caddy.
11. Now browse to your Raspberry Pi’s local IP address, or go to localhost on the Pi itself. If you are unsure of what your Raspberry Pi’s local IP address is, you can utilize the hostname -I command.
If you followed our tutorial, you should see the text “This is a test for pimylifeup.com“.
Installing PHP for the Caddy Web Server
We can now set up PHP for our Raspberry Pi Caddy web server. To do this, we will use PHP-FPM.
1. For this step, we will be installing PHP-FPM on our Raspberry Pi. We will also install additional PHP modules required by many PHP scripts.
Now run the following command to install php-fpm and several other handy PHP modules on your Raspberry Pi.
sudo apt install -y php-fpm php-mbstring php-mysql php-curl php-gd php-zip php-xmlCopy
This command installs the PHP version that comes with your operating system. PHP 8.4 on Raspberry Pi OS Trixie.
2. With PHP-FPM now installed onto our Raspberry Pi, we can get on with configuring our Caddy web server to use it, to do this we will have to crack open our Caddyfile again.
Run the following command to begin editing the file.
sudo nano /etc/caddy/CaddyfileCopy
3. To this file, we want to add the following line. It tells Caddy to pass any requests for PHP files to PHP-FPM. It also makes Caddy treat index.php as a folder’s index file, and send any request for a file that doesn’t exist to index.php, which many PHP apps rely on.
Find
encode gzipCopy
Add Below
php_fastcgi unix//run/php/php-fpm.sockCopy
The unix/ at the start tells Caddy that what follows is a socket file. /run/php/php-fpm.sock always points to the version of PHP-FPM that is installed, so you won’t need to change this line when your Raspberry Pi gets a newer version of PHP.
The file should look like the one below.
:80 localhost:80 {
root * /var/www/localhost
encode gzip
php_fastcgi unix//run/php/php-fpm.sock
file_server
}Copy
Once you’re done, save & exit by pressing CTRL + X and then Y, then lastly hitting ENTER.
4. Now, before we go on to starting up Caddy using our new service, let’s make a .php file for it to run. Remember to change out localhost if you are utilizing an actual domain name.
sudo nano /var/www/localhost/index.phpCopy
5. Now, in this file, add the following line.
<?php phpinfo(); ?> Copy
Once done, save and quit by pressing CTRL + X, then Y, followed by the ENTER key.
6. We can now reload Caddy so that it uses our updated Caddyfile by running the following command. You don’t need to set Caddy to start at boot, as its package already does this for us.
sudo systemctl reload caddyCopy
7. Now browse to your Raspberry Pi’s IP address again. You should now see PHP’s information page, which shows the PHP version you’re running and the modules loaded.
8. This page shows a lot of details about your Raspberry Pi, so once you have confirmed that PHP is working, delete it by using the following command.
sudo rm /var/www/localhost/index.phpCopy
9. You should now have a fully working HTTP server that is utilizing Caddy. If you want to modify your Caddy configuration, you can check out their official documentation by going to the Caddy Docs page.
Uninstalling Caddy
If you no longer want Caddy on your Raspberry Pi, you can remove it with the following steps.
1. First, remove Caddy by running the following command. This also deletes the /etc/caddy folder, including your Caddyfile, and any certificates Caddy has obtained.
sudo apt purge -y caddyCopy
2. Next, remove Caddy’s package repository and its key, then update the package list by using the following two commands.
sudo rm /etc/apt/sources.list.d/caddy-stable.list /usr/share/keyrings/caddy-stable-archive-keyring.gpg
sudo apt updateCopy
3. Finally, delete your website’s files by using the following command.
sudo rm -rf /var/www/localhostCopy
PHP remains installed after these steps, as other software on your Raspberry Pi may use it. If you don’t need it, you can remove it by running the following command.
sudo apt remove -y --autoremove php-fpm php-mbstring php-mysql php-curl php-gd php-zip php-xmlCopy
Conclusion
I hope you now have a Caddy web server running on your Raspberry Pi, ready for your own websites and PHP apps.
We have plenty more Raspberry Pi server projects that I highly recommend checking out.
If you ran into any issues or want to share feedback, feel free to drop a comment below.




Again another great walkthrough. Thank you for the time and effort you put into putting these things together.
I have a suggestion (don’t I always). Step 10, I think this is out of date. The command to run caddy now is `caddy run`. I don’t know if you are keeping things evergreen but if you are.
As per Caddy official documentation. https://caddyserver.com/docs/getting-started
Hi Declan,
Thank you for pointing out that, I have corrected the tutorial to use the updated way of running Caddy.
Kind regards,
Emmet
On a fresh stretch installing I had to run sudo apt-get install dirmngr to get to run, it was resulting in an error 2 getting the pgp key,