Php_fastcgi not rewrting to index.php

Just getting my feet wet (setting up a dev environment) with a php:fpm container and using caddy fastcgi directive.

I’ve got it working but the directive is not rewriting a relative url and thus it’s not loading index.php. Looking at the docs it looks like that directive is supposed to do that so wondering what i am missing here?

https://php.test.net => 404 not found
https://php.test.net/index.php => loads page fine

1. Caddy version (caddy version):

2.5.x

2. How I run Caddy:

script run from systemd service

Caddyfile

https://php.test.net {
    import r53
    root * /app/data
    php_fastcgi localhost:9000 
    file_server
} 

docker compose for php:fpm

version: '3.3'
services:
  php-fpm:
    container_name: php-fpm
    image: php:fpm
    restart: unless-stopped
    ports:
      - "9000:9000"
    volumes:
      - $PWD/src:/app/data

Did you actually mount /app/data in the Caddy container as well?

Caddy needs access to the source so that it can know how to perform rewrites, and to serve any non-PHP static files.

Please do not remove any parts of the help template. The forum rules are that you must completely fill out template.

1 Like

caddy is not running in a container. It’s running directly on my dev box as a systemd service that runs a script as I said.

if I use root * /path/per/container/volume/pwd/src in caddy it can’t find anything
if I use root * /app/data the directory inside the fpm container then at least it “kinda” works.

ok on the template but sometimes filling out “everything” just clouds this issue. I have this script and service running on multiple machines with dozens of stanzas without issues.

# caddy@.service
# For using Caddy2 and a caddyfile.
# Using caddy repository at @d see @d/README.md

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
# base directory must be hard coded
User=caddy
Group=caddy
WorkingDirectory=@d
ExecStart=@d/scripts/caddy %i
ExecReload=@d/scripts/caddy %i reload
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
#!/bin/bash
SDIR=$(dirname "$(readlink -f "$0")") || exit
DIR=$(dirname "$SDIR") || exit
INSTANCE=${1:-default}
source $SDIR/library
echo $SDIR
get-caddy-bin # this sets $CADDY_BIN
CONF_DIR=$DIR/conf/$INSTANCE
CONF_SHARED_DIR=$DIR/conf/_shared
CONF=$CONF_DIR/caddy.conf
SCRIPT=$CONF_DIR/script
BIN=$DIR/bin/$CADDY_BIN
echo running caddy $BIN
[ ! -f "$BIN" ] && echo binary file $BIN does not exist && exit 1
[ ! -x "$BIN" ] && echo binary file $BIN is not executable && exit 1
[ ! -f "$CONF" ] && echo no configuration file $CONF && exit 1
if [ -f "$SCRIPT" ]; then
   source $SCRIPT
fi
if [ ! $INVOCATION_ID ]; then
  CAP="cap_net_bind_service+eip"
  ISSET=$(getcap $BIN | grep $CAP )
  if [ ! "$ISSET" ]; then
    echo binary $BIN not set for binding port 80 by non-root users, attempting to set
    sudo setcap $CAP $BIN
    ISSET=$(getcap $BIN | grep $CAP)
    echo after $ISSET
    [ ! "$ISSET" ] && echo unable to set port binding && exit 1
  fi
  echo running caddy from commandline, enter caddy user password
  su -c "$BIN run --config $CONF --adapter caddyfile" caddy
else
  echo running caddy via systemd service
  $BIN run --config $CONF --adapter caddyfile
fi

You can set a root for Caddy and a root for PHP.

root * /path/per/container/volume/pwd/src
php_fastcgi localhost:9000 {
	root /app/data
}
1 Like

That solved it. Didn’t realize that I could have two roots like that (and why). Now I know.

For anyone reading this (per other posts I read) inside the container and out I used www-data for owner and group for the /src directory and then added caddy user to www-data group on my dev box. I did set write permissions for the group on that directory. Don’t know if that is necessary or if read is sufficient.

Nice! now I have a quick way to spin up a php dev or production environment with a particular fpm version. Thx yet again @francislavoie and @matt

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.