[V2] Setting up PHP

1. My Caddy version (caddy version):

2.0 RC 2

2. How I run Caddy:

caddy start

a. System environment:

Debian 10 stable. PHP 7.3

d. My complete Caddyfile or JSON

    root * /home/user/www/example-com/
    php_fastcgi localhost:9000
    log {
        output file /home/user/caddy/log/example.com.log

3. The problem I’m having:

I’m trying to get PHP working on my website (example.com). I’ve installed package php7.3-fpm and launched it /etc/init.d/php7.3-fpm start. But I get HTTP 502 bad gateway on all the PHP site. I don’t know on which port php7.3-fpm is running. I assume it’s 9000…

4. Error messages and/or full log output:

2020/04/11 13:23:26.012 ERROR   http.log.error.log0     dialing backend: dial tcp [::1]:9000: connect: connection refused   {"request": {"method": "GET", "uri": "/test.php", "proto": "HTTP/2.0", "remote_addr": "", "host": "example.com", "headers": {"Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Cache-Control": ["max-age=0"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate, br"]}, "tls": {"resumed": false, "version": 772, "ciphersuite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "example.com"}}, "status": 502, "err_id": "s7whh7e0q", "err_trace": "reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:362)"}
2020/04/11 13:23:26.012 ERROR   http.log.access.log0    handled request {"request": {"method": "GET", "uri": "/test.php", "proto": "HTTP/2.0", "remote_addr": "", "host": "example.com", "headers": {"Cache-Control": ["max-age=0"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate, br"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"]}, "tls": {"resumed": false, "version": 772, "ciphersuite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "example.com"}}, "common_log": " - - [11/Apr/2020:15:23:26 +0200] \"GET /test.php HTTP/2.0\" 502 0", "latency": 0.001813994, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"]}}

5. What I already tried:

Read php_fastcgi (Caddyfile directive) — Caddy Documentation
Search for an how-to for using PHP with Caddy, but I only found old V1 things
Try to change owner of test.php to www-data:www-data

By default on debian, I think php-fpm runs with a unix socket, with a path like /run/php/php7.3-fpm.sock. Check if a similar file exists.

If so, then your php_fastcgi directive would look like this:

php_fastcgi unix//run/php/php7.3-fpm.sock

Thank you so much!
This issue was driving me nuts haha.
I think a “php quick start guide” will be welcome in the documentation.

1 Like

Honestly, I don’t think there’s enough content to warrant a separate guide for PHP. All you need is found in the php_fastcgi directive page.

I’ll make sure we add an example for running with a unix socket though, as that seems to be lacking.

Of course, I just meant to add it to the docs.

I made a PR to add a unix socket example. Thanks for pointing it out!


Thank you for your fast answer!

1 Like

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