PHP 8.4 on Caddy on Debian 13… in Three Minutes

I just needed to spin up a new PHP webserver and I was amazed how fast and easy it was, nowadays. I mean: Caddy already makes it pretty easy, but I was delighted to see that, since the last time I did this, the default package repositories had 100% of what I needed!

Apart from setting the hostname, creating myself a user and adding them to the sudo group, and reconfiguring sshd to my preference, I’d done nothing on this new server. And then to set up a fully-functioning PHP-powered webserver, all I needed to run (for a domain “example.com”) was:

sudo apt update && sudo apt upgrade -y
sudo apt install -y caddy php8.4-fpm
sudo mkdir -p /var/www/example.com
printf "example.com {\n"                               | sudo tee    /etc/caddy/Caddyfile
printf "  root * /var/www/example.com\n"               | sudo tee -a /etc/caddy/Caddyfile
printf "  encode zstd gzip\n"                          | sudo tee -a /etc/caddy/Caddyfile
printf "  php_fastcgi unix//run/php/php8.4-fpm.sock\n" | sudo tee -a /etc/caddy/Caddyfile
printf "  file_server\n"                               | sudo tee -a /etc/caddy/Caddyfile
printf "}\n"                                           | sudo tee -a /etc/caddy/Caddyfile
sudo service caddy restart

After that, I was able to put an index.php file into /var/www/example.com and it just worked.

And when I say “just worked”, I mean with all the bells and whistles you ought to expect from Caddy. HTTPS came as standard (with a solid QualSys grade). HTTP/3 was supported with a 0-RTT handshake.

Mind blown.

Reactions

No time to comment? Send an emoji with just one click!

4 comments

  1. Spencer Spencer says:

    You could probably save at least a minute using an editor rather than printf+tee 😂

    1. Dan Q Dan Q says:

      Hah! You caught me!

      I did use an editor, of course, but I hate those tutorials that have a script that you copy-paste and edit… except there’s a bit in the middle where it says “oh yeah, and go edit this file a bit”. This is written the way it is mostly for my own convenience, next time: I can copy-paste that, and then go in with an editor to search-replace the ‘example.com’ out!

  2. HisVirusness HisVirusness says:

    That is very impressive. Can’t say I have any experience with Caddy, but I may need to play around with it for a bit.

    1. Dan Q Dan Q says:

      Caddy’s just great: I’ve been using it “properly” for a few years now, and it’s my preferred go-to webserver, all other things being equal (supplanting Nginx, which was my go-to for about a decade, supplanting Apache, which was my go-to before that).

      It assumes sensible defaults, which means that if you’re looking for example for a server hosting a single static website over HTTPS (with a redirect from HTTP)… that can be as little as two lines of configuration in Caddyfile format! Only for the exceptional cases do you need significantly more.

      It’s designed to proxy, so you can put PHP-FPM behind it or whatever else if you like… or if you really want an “all in one” solution, FrankenPHP is Caddy with PHP pre-compiled into it! I tried it for a few months and found it a bit crashy, but it’s possible my configuration was whack. Anyway: Caddy + PHP-FPM “just works” plenty well for me!

Reply here

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

Reply on your own site

Reply elsewhere

You can reply to this post on Mastodon (@blog@danq.me).

Reply by email

I'd love to hear what you think. Send an email to b27991@danq.me; be sure to let me know if you're happy for your comment to appear on the Web!