raspberry pi web server

How to Setup a Raspberry Pi Caddy Web Server

Equipment List

Below are all the pieces that I used for this Raspberry Pi Caddy web server tutorial. I highly recommend using a ethernet cord for the best performance with any web related projects.


Raspberry Pi 2 or 3

Micro SD Card

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

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 completely 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 will grab and run the bash script that resides at Get Caddy basically this script detects what operating system you are running, downloads the correct version of Caddy and sets it up so it can be ran like a normal application.

curl https://getcaddy.com | bash

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 just name ours 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 basically 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 does 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 likepimylifeup.com {

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

Once you’re finished doing that you can save & exit out of the file by pressing CTRL +X and then Yand lastly Enter.

8. With our Caddyfile now setup, we will move onto making a sample html file just 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 just 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 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 on what your Raspberry Pi’s local IP address is you can utilize the hostname -Icommand. 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. 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. You will have the choice between the PHP 5 or PHP 7, we recommend utilizing PHP 7 as it is more modern, faster, and more memory efficient.


13a. Luckily for PHP 5 we don’t have to do anything fancy to get it working on all versions of Raspbian, we can just enter the following command into terminal to install it.

sudo apt-get install php5-fpm


13b. Unfortunately, PHP 7 is not included on Raspbian Jessie and earlier so we have to do some trickery to download the package from the newer repositories. We personally recommend peopleupgrade to Raspbian Stretch if they intend on using PHP 7.0 as it stops having to use a very hacky method of getting PHP on older versions. Follow the following steps to install PHP 7.0 onto your Raspberry Pi.

Raspbian Stretch and later

sudo apt-get install php7.0-fpm

Raspbian Jessie and earlier

Sadly as we mentioned before, Raspbian Jessie and earlier does not have the PHP 7.0 packages. Luckily for us we can do some apt trickery so we can specifically grab the new versions of php from the Raspbian Stretch repository.

a. Begin by editing the apt sources list file by running the following command.

sudo nano /etc/apt/sources.list

b. Add the following line to the bottom of this file.

deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi

Now we can save & exit, simply enter CTRL +X and then press y.

c. Now let’s modify our preferences file to make sure that Jessie packages will be chosen over Stretch. Run the following command to begin editing the file.

sudo nano /etc/apt/preferences

d. To this, add the following lines, this basically sets the packages to effect and the priority.

Package: *
Pin: release n=jessie
Pin-Priority: 600

Once done, we can save & exit by pressing CTRL +X and then pressing y.

e. With the preference changes and the new repository added to our sources list, we need to run an apt-get update command to start grabbing packages from the new source.

sudo apt-get update

f. Finally we can install PHP7.0-FPM by using the following command.

sudo apt-get install -t stretch php7.0-fpm

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 basically tell our Caddy web server to utilize PHP-FPM as its fast cgi handler. It also tells it to read index.php files as the index and we also add a few lines that rewrites URL requests to work fine with php. Replace/var/run/php/php7.0-fpm.sock with /var/run/php5-fpm.sock


   root /var/www/localhost

Add Below

    fastcgi / /var/run/php/php7.0-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, we also need to do this to setup 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 ownerships for our Caddy files we can finally create our Caddy service as well. Run the following commands to download and setup 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 further modify your Caddy configuration you can check out their official documentation by going to theCaddy Docs page.

Leave a Reply

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