How to Setup a Raspberry Pi Caddy Web Server

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

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 HTTP2 from the get-go and automatically provisions SSL certificates for any domain name that you set up through your Caddy files. It does this by automatically grabbing these certificates from Lets 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.



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

2. Before we get started with setting up our Raspberry Pi Caddy web server, we will first have to remove apache2 that is installed by default on some copies of Raspbian.

If you already know that you don’t already have apache2 installed on the Raspberry Pi, you can skip this command and move on to the next step.

sudo apt remove apache2

3. Now we can run the following commands in terminal to download and install Caddy. These commands are also available at the Caddy website.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf '' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf '' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

4. Once our Caddy web server has finished being downloaded and installed onto our Raspberry Pi, we can begin on the more complex task of setting it up.

Let’s first make a folder where we will keep our Caddy configuration file in the /etc/ folder by running the following command on our Raspberry Pi.

sudo mkdir /etc/caddy

5. While we are busy making folders, we will also make one to keep our first website. 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

sudo mkdir -p /var/www/localhost

6. With both our folders created on our Raspberry Pi, we can now get onto the interesting part, and that is making our caddyfile. This file is where we will keep all our configurations for our Raspberry Pi Caddy web server.

We will be storing this file in the /etc/caddy folder that we created previously.

Run the nano command below on our Raspberry Pi to begin the process of creating our caddyfile.

sudo nano /etc/caddy/Caddyfile

7. In this file write the following lines, basically what these lines of text do is define the address to listen on, where the files we want to serve are and that we want to utilize GZIP compression on Caddy’s HTML output.

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 {

:80 localhost:80 {
    root /var/www/localhost

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 onto making a sample HTML file to ensure its still working. We will be going back to editing our Caddyfile some more when we setup 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.html

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</h1>

Once you have done this, you can save and quit by pressing CTRL + X then Y and then pressing ENTER.

10. To start up Caddy web server on our Raspberry Pi, all we need to do is type in the following commands into the terminal.

cd /etc/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“.

12. Once you have confirmed that it is working press CTRL + C to terminate the program.

Installing PHP for the Caddy Web Server

We can now move onto setting up PHP for our Raspberry Pi Caddy web server. To do this, we will be making use of PHP-FPM.

13. For this step, we will be installing PHP-FPM to our Raspberry Pi. We will also be installing some additional PHP modules that are required by numerous PHP scripts.

Before proceeding with this tutorial, you should be running Raspberry Pi OS Bullseye or newer. You can learn how to upgrade Raspberry Pi OS Buster to Bullseye in our guide.

Now run the following command to install “php7.4-fpm” and several other handy PHP modules to your Raspberry Pi.

sudo apt install php7.4-fpm php7.4-mbstring php7.4-mysql php7.4-curl php7.4-gd php7.4-curl php7.4-zip php7.4-xml -y

If you are running a version of Raspberry Pi OS that doesn’t contain PHP 7.4, please follow our guide on adding a third-party PHP repository. This repository includes new and old versions of PHP.

14. 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/Caddyfile

15. To this file we want to add the following lines, these lines will tell our Caddy web server to utilize PHP-FPM as its fastcgi handler.

It also tells it to read index.php files as the index, and we also add a few lines that rewrite URL requests to work fine with PHP.


   root /var/www/localhost

Add Below

    fastcgi / /var/run/php/php7.4-fpm.sock {
        index index.php
    rewrite {
        r .*
        ext /
        to /index.php?{query}

Once you’re done, save & exit by pressing CTRL + X and then Y then lastly hitting ENTER.

16. With that now saved, we need to change a few files permissions so that PHP-FPM can interpret our “index.php” file, and we also need to do this to set up our service file.

Run the following Linux permission commands on your Raspberry Pi to give the www-data user and group control over our web related files and assign relevant read/write/execute permissions.

sudo chown -R root:www-data /etc/caddy
sudo chown www-data:www-data /etc/caddy/Caddyfile
sudo chmod 444 /etc/caddy/Caddyfile
sudo chown -R www-data:www-data /var/www
sudo chmod -R 555 /var/www

17. Now that we have corrected the read/write permissions and file ownership for our Caddy files we can finally create our Caddy service as well. Run the following commands to download and set up the Caddy service.

sudo wget
sudo cp caddy.service /etc/systemd/system/
sudo chown root:root /etc/systemd/system/caddy.service
sudo chmod 644 /etc/systemd/system/caddy.service
sudo systemctl daemon-reload

18. Now before we go 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.php

19. Now in this file, add the following line.

 <?php phpinfo(); ?> 

Once you’re all done with that, you can save & exit out of the file by pressing CTRL + X and then pressing Y, and then the ENTER key.

20. Now we can finally start up our Raspberry Pi Caddy web server by using our new service and set it to automatically start on boot by running the following two commands. If you don’t want it starting on boot, don’t run the second command.

sudo systemctl start caddy.service
sudo systemctl enable caddy.service

21. You should now have a fully working HTTP server that is utilizing Caddy if you want to modify further your Caddy configuration you can check out their official documentation by going to the Caddy Docs page.

By now we hope you have successfully set up your Raspberry Pi Caddy web server if you have enjoyed this DIY Pi tutorial or ran into any issues feel free to drop a comment in the comments section below.

One Comment

  1. Avatar for Will
    Will on

    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,

Leave a Reply

Your email address will not be published. Required fields are marked *