Can't get PHP-FPM + Caddy to work

1. The problem I’m having:

I’ve been trying to setup PHP-FPM with Caddy, but Caddy refuses to send me php files. Instead, it throws and error and sends me 404. I’ve tried running php -S localhost:8000 to confirm - it works directly. I’ve tried multiple approaches but sadly non worked.

I didn’t change any of the configs, but if needed I can post php-fpm.conf.

The backend folder is a bunch of php files. For easier reproduction, here’s contents of index.php:

<?php
echo "Hello";

2. Error messages and/or full log output:

For when using a php-fpm.sock:

2024/04/23 20:54:45.785 ERROR   http.reverse_proxy.transport.fastcgi    stderr {"request": {"remote_ip": "::1", "remote_port": "51772", "client_ip": "::1", "proto": "HTTP/1.1", "method": "HEAD", "host": "localhost:7777", "uri": "/index.php", "headers": {"User-Agent": ["curl/8.7.1"], "Accept": ["*/*"], "X-Forwarded-For": ["::1"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Host": ["localhost:7777"]}}, "env": {"GATEWAY_INTERFACE": "CGI/1.1", "REQUEST_METHOD": "HEAD", "SERVER_SOFTWARE": "Caddy/v2.7.6", "AUTH_TYPE": "", "HTTP_HOST": "localhost:7777", "REQUEST_URI": "/index.php", "QUERY_STRING": "", "REMOTE_ADDR": "::1", "SERVER_NAME": "localhost", "CONTENT_LENGTH": "", "REMOTE_PORT": "51772", "SCRIPT_FILENAME": "/home/v1rtl/Coding/uni-kanban/backend/index.php", "REMOTE_HOST": "::1", "REMOTE_USER": "", "SERVER_PROTOCOL": "HTTP/1.1", "DOCUMENT_URI": "/index.php", "SERVER_PORT": "7777", "PATH_INFO": "", "HTTP_ACCEPT": "*/*", "HTTP_X_FORWARDED_FOR": "::1", "REQUEST_SCHEME": "http", "CONTENT_TYPE": "", "DOCUMENT_ROOT": "/home/v1rtl/Coding/uni-kanban/backend", "SCRIPT_NAME": "/index.php", "HTTP_USER_AGENT": "curl/8.7.1", "REMOTE_IDENT": "", "HTTP_X_FORWARDED_HOST": "localhost:7777", "HTTP_X_FORWARDED_PROTO": "http"}, "body": "Primary script unknown"}

For when using fast_cgi localhost:8000:

2024/04/23 20:56:09.570 ERROR   http.log.error  EOF     {"request": {"remote_ip": "::1", "remote_port": "46326", "client_ip": "::1", "proto": "HTTP/1.1", "method": "HEAD", "host": "localhost:7777", "uri": "/index.php", "headers": {"User-Agent": ["curl/8.7.1"], "Accept": ["*/*"]}}, "duration": 0.001889224, "status": 502, "err_id": "wxsmi2jtg", "err_trace": "reverseproxy.statusError (reverseproxy.go:1267)"}

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

a. System environment:

EndeavourOS Linux (kernel 6.8.7-arch1-1)

I have both php and php-fpm packages installed. The latter is running as a systemd service.

b. Command:

caddy run --config Caddyfile

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

The one with sockets:

http://localhost:7777 {
	root * /home/v1rtl/Coding/uni-kanban/backend
	php_fastcgi unix//run/php-fpm/php-fpm.sock {
		root /home/v1rtl/Coding/uni-kanban/backend
		capture_stderr
	}
}

The one with php -S localhost:8000

http://localhost:7777 {
	root * /home/v1rtl/Coding/uni-kanban/backend
	php_fastcgi localhost:8000 {
		root /home/v1rtl/Coding/uni-kanban/backend
		capture_stderr
	}
}

I temporarily solved it by moving all my php files to /srv/www/caddy. I’d still would like to be able to serve them from my homedir

I’d still would like to be able to serve them from my homedir

The caddy user has no permission to your home directory, you can change the user caddy is running under though

systemctl edit caddy.service
[Service]
User=
User=<YOUR USERNAME>
Group=
Group=<YOUR USERNAME>

I strongly recommend continuing to serve the files from /srv or /var/www. It’s not good to try to serve from /home. It’s best if your webserver doesn’t have permission to read user-owned files.

Also, you don’t need to repeat root inside your php_fastcgi config. Only do that if your PHP server has a different “view” of the filesystem (i.e. it’s useful in some Docker scenarios). Just use the root directive (the first one).