Cannot get PHP working in Caddy v2

1. Caddy version (caddy version):

v2.4.1 h1:kAJ0JB5Xk5gPdTH/27S5cyoMGqD5lBAe9yZ8zTjVJa0=

2. How I run Caddy:

a. System environment:

Arch Linux running caddy binary downloaded from Download Caddy in CLI

This is my directory structure:

Caddyfile  README.md  phpinfo.php

phpinfo.php:

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

?>

b. Command:

sudo caddy start

c. Service/unit/compose file:


d. My complete Caddyfile or JSON config:

{
	debug
}

http://localhost:8888 {
	log {
		output file /var/log/caddy/access.log
		# format single_field common_log
	}

	root * /home/leesei/wip/learn-php

	php_fastcgi unix//run/php-fpm7/php-fpm.sock {
		index phpinfo.php
		resolve_root_symlink
	}

	encode gzip
	file_server
}

3. The problem I’m having:

PHP page cannot be viewed: curl http://localhost:8888/phpinfo.php returns “File not found.” from PHP.

4. Error messages and/or full log output:

2021/05/31 02:46:21.906	DEBUG	http.reverse_proxy.transport.fastcgi	roundtrip	{"request": {"remote_addr": "[::1]:48172", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8888", "uri": "/phpinfo.php", "headers": {"X-Forwarded-For": ["::1"], "User-Agent": ["curl/7.76.1"], "Accept": ["*/*"], "X-Forwarded-Proto": ["http"]}}, "dial": "/run/php-fpm7/php-fpm.sock", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/home/leesei/wip/learn-php","DOCUMENT_URI":"/phpinfo.php","GATEWAY_INTERFACE":"CGI/1.1","HTTP_ACCEPT":"*/*","HTTP_HOST":"localhost:8888","HTTP_USER_AGENT":"curl/7.76.1","HTTP_X_FORWARDED_FOR":"::1","HTTP_X_FORWARDED_PROTO":"http","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"::1","REMOTE_HOST":"::1","REMOTE_IDENT":"","REMOTE_PORT":"48172","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"http","REQUEST_URI":"/phpinfo.php","SCRIPT_FILENAME":"/home/leesei/wip/learn-php/phpinfo.php","SCRIPT_NAME":"/phpinfo.php","SERVER_NAME":"localhost","SERVER_PORT":"8888","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"Caddy/v2.4.1"}}
2021/05/31 02:46:21.910	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "unix//run/php-fpm7/php-fpm.sock", "request": {"remote_addr": "[::1]:48172", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8888", "uri": "/phpinfo.php", "headers": {"X-Forwarded-For": ["::1"], "User-Agent": ["curl/7.76.1"], "Accept": ["*/*"], "X-Forwarded-Proto": ["http"]}}, "headers": {"Status": ["404 Not Found"], "X-Powered-By": ["PHP/7.4.18"], "Content-Type": ["text/html; charset=UTF-8"]}, "status": 404}
2021/05/31 02:46:21.910	ERROR	http.log.access.log0	handled request	{"request": {"remote_addr": "[::1]:48172", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8888", "uri": "/phpinfo.php", "headers": {"User-Agent": ["curl/7.76.1"], "Accept": ["*/*"]}}, "common_log": "::1 - - [31/May/2021:10:46:21 +0800] \"GET /phpinfo.php HTTP/1.1\" 404 16", "duration": 0.004308066, "size": 16, "status": 404, "resp_headers": {"Server": ["Caddy"], "Status": ["404 Not Found"], "X-Powered-By": ["PHP/7.4.18"], "Content-Type": ["text/html; charset=UTF-8"]}}

5. What I already tried:

I added this to /etc/php7/php-fpm.d/www.conf

access.format = "%R - %u %t [%f] \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"

where %f should be the script filename

Here’s what I got from php-fpm7/www.access.log:

- -  31/May/2021:10:46:21 +0800 [-] "GET /phpinfo.php" 404 - 2.911 2048 0.00%

6. Links to relevant resources:

Are you sure that both your user that php-fpm runs as, and the caddy user have permissions to read those files?

php-fpm is started with systemd as root
and my folder and php file have permission 755

Anything else I can do to validate what the path php-fpm gets?
I seems correct from Caddy’s side.

The php-fpm service probably starts as root, but runs with a different user. Check your php-fpm config for the configured user.

1 Like

Following the user direction, I’ve solved the issue.

  1. php-fpm is started as user http
    the socket is also owned by http and has 0600 permission
    so sudo -u http caddy start is enough
  2. php-fpm requires access of all folders all the way to www root
    I probably enabled error log in PHP to see this log and I saw it failed as the http user tries to lstat /home/leesei/wip/
    this is where I got stuck with, I tried chmod -r http:http /home/leesei/wip/learn-php already

Solution:

  • moved my www root to /srv/http/learn-php (/srv/http is owned by http user)
  • update Caddyfile
  • sudo -u http caddy start
  • have fun with PHP
2 Likes

I found the cgi-fcgi tool to test PHP-FPM independent of Caddy.
Maybe we can add these (or write up something) to php_fastcgi (Caddyfile directive) — Caddy Documentation to point user to debug first?

inanzzz | Testing PHP-FPM without having a web server
Directly connect to PHP-FPM

The Caddy docs assume that you know how to use PHP-FPM. It’s not our job to document that. Other resources can better answer those questions.

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