Unable to serve basic PHP file via Caddy

1. The problem I’m having:

Unable to serve PHP apps / files via Caddy

This is my 3rd time attempting to setup and configure Caddy so I can start using it as a local development tool. I need to serve PHP applications with Caddy. I am on MacOS Ventura 13.5.1. However, not once have I been successful in serving a PHP application, let a lone a stand-alone PHP file with Caddy. This is unbelievably frustrating

I successfully followed the docs to get the “Hello world” caddy webserver up and running.

I then followed multiple instructions to attempt to serve PHP files without any success. (Only linking the official docs despite trying non-official blogs as well).

https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#examples

I wasn’t exactly sure what I should use for my php_fastcgi coniguration directive in my Caddyfile so I tried a few options.

First

  1. I double checked php-fpm was running on my system: pgrep -fa php-fpm shows three process IDs.
  2. Looking up the first PID returned using ps -p <PID> shows:
    44870 ?? 0:00.10 php-fpm: master process (/usr/local/etc/php/8.2/php-fpm.conf)
    so it appears to me php-fpm is running?
  3. Looking at the php-fpm.conf file from above I don’t see any settings related to php-fpm listening on a particular socket or a particular port.
  4. The conf file does have this line however include=/usr/local/etc/php/8.2/php-fpm.d/*.conf
  5. So I decided to look in the /usr/local/etc/php/8.2/php-fpm.d/ directory and find a www.conf file in it.
  6. This file has a setting in it: listen = 127.0.0.1:9000 so I use that for my Caddyfile setting.
So my Caddyfile looks like this:
 inv.local {
     root * /Users/nateschreiner/projects/php-project/
     php_fastcgi 127.0.0.1:9000
     file_server
 }

Just as it does on the example from the official docs.

However, when I browse to inv.local, I simply get a We can’t connect to the server at inv.local error message and not a single log message is being logged in the terminal where Caddy is started.

Second

I saw some blogs stating they directed caddy to use php-fpm via a unix socket. I did some grepping around my system and could not find a php-fpm.sock file anywhere. Nor could I find a directive in any of the php-fpm configuration files. So I figured configuring caddy to use php-fpm via unix socket was off the table.

2. Error messages and/or full log output:

After starting Caddy via caddy run --config Caddyfile and requesting inv.local via the browser here is the full log output from the terminal:

2023/11/21 23:39:56.664 INFO    using provided configuration    {"config_file": "Caddyfile", "config_adapter": ""}
2023/11/21 23:39:56.666 WARN    Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies    {"adapter": "caddyfile", "file": "Caddyfile", "line": 2}
2023/11/21 23:39:56.668 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/11/21 23:39:56.668 INFO    tls.cache.maintenance   started background certificate maintenance     {"cache": "0xc00056c680"}
2023/11/21 23:39:56.668 INFO    http.auto_https server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2023/11/21 23:39:56.671 INFO    http.auto_https enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2023/11/21 23:39:56.673 INFO    tls     cleaning storage unit   {"description": "FileStorage:/Users/nateschreiner/Library/Application Support/Caddy"}
2023/11/21 23:39:56.674 INFO    tls     finished cleaning storage units
2023/11/21 23:39:56.684 INFO    pki.ca.local    root certificate is already trusted by system   {"path": "storage:pki/authorities/local/root.crt"}
2023/11/21 23:39:56.684 INFO    http    enabling HTTP/3 listener        {"addr": ":443"}
2023/11/21 23:39:56.684 INFO    http.log        server running  {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/11/21 23:39:56.684 INFO    http.log        server running  {"name": "remaining_auto_https_redirects", "protocols": ["h1", "h2", "h3"]}
2023/11/21 23:39:56.684 INFO    http    enabling automatic TLS certificate management   {"domains": ["inv.local"]}
2023/11/21 23:39:56.685 WARN    tls     stapling OCSP   {"error": "no OCSP stapling for [inv.local]: no OCSP server specified in certificate", "identifiers": ["inv.local"]}
2023/11/21 23:39:56.685 INFO    autosaved config (load with --resume flag)      {"file": "/Users/nateschreiner/Library/Application Support/Caddy/autosave.json"}
2023/11/21 23:39:56.685 INFO    serving initial configuration

As you can see, caddy isn’t showing any logs that would indicate it even received a request which I find weird.

I then attempted to browse to localhost:2019 just for the heck of it and sure enough, I see attempted requests in the terminal log output:

INFO    admin.api       received request        {"method": "GET", "host": "localhost:2019", "uri": "/", "remote_ip": "127.0.0.1", "remote_port": "51158", "headers": {"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.5"],"Connection":["keep-alive"],"Cookie":["local_adminer_session=515168; adminer_sid=11e1ea5b0b5ea0262e805643da0600ab"],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"]}}
2023/11/21 23:41:58.678 INFO    admin.api       received request        {"method": "GET", "host": "localhost:2019", "uri": "/favicon.ico", "remote_ip": "127.0.0.1", "remote_port": "51158", "headers": {"Accept":["image/avif,image/webp,*/*"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.5"],"Connection":["keep-alive"],"Cookie":["local_adminer_session=515168; adminer_sid=11e1ea5b0b5ea0262e805643da0600ab"],"Referer":["http://localhost:2019/"],"Sec-Fetch-Dest":["image"],"Sec-Fetch-Mode":["no-cors"],"Sec-Fetch-Site":["same-origin"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"]}}

3. Caddy version:

v2.7.5 h1:HoysvZkLcN2xJExEepaFHK92Qgs7xAiCFydN5x5Hs6Q=

4. How I installed and ran Caddy:

brew install caddy

caddy run --config Caddyfile

a. System environment:

MacOS Ventura 13.5.1

b. Command:

c. Service/unit/compose file:

d. My complete Caddy config:

 inv.local {
     root * /Users/nateschreiner/projects/php-project/
     php_fastcgi 127.0.0.1:9000
     file_server
 }

5. Links to relevant resources:

Try using inv.localhost instead. The *.local TLD is special, it’s used by multicast DNS which can have weird and surprising results.

Try making a request with curl -v https://inv.localhost. What do you see?

1 Like

Wow,

Sure enough, that worked. Appreciate it Francis

2 Likes

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