Skip to content

Setting Up Caddy with PHP 7 on Ubuntu 16.04

Posted on:April 14, 2017 at 01:00 PM

Caddy seems like a Web Server that’s going places, but unfortuantely there is still a need for more tutorials, and discussion. While there was nothing overly complicated about getting it up and running, it did take a few head scratching moments to get there! So, here’s how to do it:

Update and upgrade your existing packages:

sudo apt-get update && sudo apt-get upgrade

Next you want to install the packages you can get from the Ubuntu repositories:

 sudo apt-get install language-pack-en php7.0-fpm php7.0-cli curl

Install Caddy:

curl https://getcaddy.com | bash

Now, it’s time to start to configure Caddy, and the website. For this example, we’ll use caddy.thomasredstone.com.

Setup the directories:

sudo -p /opt/www/caddy.thomasredstone.com /etc/caddy /etc/ssl/caddy sudo chown -R www-data:www-data /etc/caddy /var/www /etc/ssl/caddy sudo chmod 700 /etc/ssl/caddy

Next, we want to setup a caddy service, by putting the following stuff into /etc/systemd/system/caddy.service:

[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
Restart=on-failure

User=www-data
Group=www-data

Environment=HOME=/etc/ssl/caddy

ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp
ExecReload=/bin/kill -USR1 $MAINPID

LimitNOFILE=4096
LimitNPROC=64

PrivateTmp=true
PrivateDevices=true
ProtectHome=true
ProtectSystem=full
ReadWriteDirectories=/etc/ssl/caddy

CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

And we need to make the Caddyfile, in /etc/caddy/Caddyfile look like this (make sure you update the domain, on the first and second line, and the email address on the third line:

caddy.thomasredstone.com {
root /var/www/caddy.thomasredstone.com
tls youremail@example.com
    fastcgi / /var/run/php/php7.0-fpm.sock {
        ext     .php
        split   .php
        index   index.php
    }
}

Now we enable the service, and start it with:

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

If this doesn’t work right away, you can run caddy from the command line:

caddy --conf /etc/caddy/Caddyfile

This can be useful for debugging any issues