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.

Recommended

Raspberry Pi 2 or 3

Micro SD Card

Power Supply

Ethernet Cord or Wifi dongle (The Pi 3 has WiFi inbuilt)

Optional

Raspberry Pi Case

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-get update
sudo apt-get 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-get remove apache2

3. Now we can run the following command in terminal to download and install Caddy. This command will grab and run the bash script that resides at Get Caddy.  The script detects what operating system you are running, downloads the correct version of Caddy and sets it up so it can run like a standard application.

curl https://getcaddy.com | bash -s personal

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 pimylifeup.com

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 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, also 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
    gzip
}

Once you’re finished doing that, you can save & 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 pimylifeup.com</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
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“.

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 need to make sure you are running Raspbian Buster or newer. You can learn how to upgrade Raspbian Stretch to Buster in our guide.

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

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

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.

Find

   root /var/www/localhost
    gzip

Add Below

    fastcgi / /var/run/php/php7.3-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, 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 https://raw.githubusercontent.com/mholt/caddy/master/dist/init/linux-systemd/caddy.service
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.

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.

Ultimate Books of Pi Bottom
Get projects delivered to your inbox weekly.