[Caddy V2] + PHP 7.2

1. Caddy version (caddy version):

v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

Google VPS

a. System environment:

Ubuntu 18.04

b. Command:

paste command here

c. Service/unit/compose file:

# caddy.service
# For using Caddy with a config file.
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
# See https://caddyserver.com/docs/install for instructions.
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.


ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile


d. My complete Caddyfile or JSON config:

domain.com {
	root * /var/www/domain.com
	encode zstd gzip
	php_fastcgi unix//run/php/php7.2-fpm.sock

3. The problem I’m having:

I get an error 502 on accessing the webpage, HTML works fine though.

4. Error messages and/or full log output:

var/log/syslog says;

Jun 13 11:57:07 web-server caddy[9391]: {"level":"error","ts":1592045827.215587,"logger":"http.log.error","msg":"dialing backend: dial unix /run/php/php7.2-fpm.sock: connect: permission denied","request":{"method":"GET","uri":"/","proto":"HTTP/2.0","remote_addr":"xx.xx.xx.xx:54309","host":"domain.com","headers":{"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45"],"Sec-Fetch-Dest":["document"],"Upgrade-Insecure-Requests":["1"],"Sec-Fetch-Site":["none"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-User":["?1"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-GB,en;q=0.9,en-US;q=0.8"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"]},"tls":{"resumed":false,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"domain.com"}},"duration":0.000390501,"status":502,"err_id":"zyv7e106q","err_trace":"reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:380)"}

/etc/php/7.2/fpm/pool.d/www.conf says;
listen = /run/php/php7.2-fpm.sock

5. What I already tried:

sudo reboot
sudo service caddy restart
sudo service php7.2-fpm restart

6. Links to relevant resources:

My issue is exactly like this one but following this thread doesn’t resolve the issue.

I think I have resolved the issue by the following taken from the 502 Bad Gateway with fastCGI post above.

50% resolved:
Thank you, that was it! I ran sudo usermod -a -G www-data caddy to add my caddy user (I have a user specifically for Caddy to keep stuff compartmentalized) to the www-data group and uncommented the line listen.mode = 0660 in /etc/php/7.0/fpm/pool.d/www.conf , and now it works!

1 Like

Well PHP works, however there is another issue where by if I go into a directory with a different PHP file in it I get the content of the default homepage.

for example;
https://domain.com/contact > gives me the PHP page served at https://domain.com/

This time there is no error in var/log/syslog or anywhere else. It looks like it can read php files in the main directory file1.php/file2.php e.t.c but not in other folders on the same domain.

EDIT: Not sure what’s going on but changing "/etc/php/7.2/fpm/pool.d/www.conf"
from: listen = /run/php/php7.2-fpm.sock
to: "listen ="

then in; "/etc/caddy/Caddyfile"
added: "php_fastcgi"

then rebooted the server; it works now.

1 Like

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