Not able to connect php and caddy via socket

PS

I am not asking for help. I had this problem and figured out why it was happening and how to fix it. I believe that someone else may have the same problem, so I am creating this to help others.

I also would like to let you know that I am no expert, just a beginner trying to help others beginners. (My english isn’t great as well).

The problem I had

I wasn’t being able to connect php and caddy via socket. Even after set up both caddy and php-fpm to work on socket, they wouldn’t work (but it would work if I set on port).

I figured out (after studying about sockets) that they have the same permission system of files. The problem I had was because I was trying to access the sock (on /run/php/php7.4-fpm.sock) with an user who hadn’t access to it.

Well, now that you understand the why of the problem, there are many ways to fix it. I am going to point one out (it is the I did)

Here is how I manged to fix it:

on file /etc/php/7.4/fpm/pool.d/www.conf, I set this configurations:

user = www-data ; user of the php-fpm process 
group = www-data ; group  of the php-fpm process

listen = /run/php/php7.4-fpm.sock ; where the socket will be created

listen.owner = www-data ; the user owner of the socket connection
listen.group = www-data ; the group owner of the socket connection
listen.mode = 0660 ; the permission of the socket connection

on Caddyfile, I set this configurations:

{
        debug # just to know if something is broken
}

localhost:9090 {
        php_fastcgi unix//run/php/php7.4-fpm.sock
        root * /home/benjamin/projects/website_cool_tea/src
        file_server
}

The problem is that our caddy user doesn’t have access in the sock.

To fix this, we could add the caddy user to the www-data group by using the command sudo usermod -a -G www-data caddy. Then restart the php-fpm (sudo systemctl restart php7.4-fpm.service) and caddy (sudo systemctl restart caddy or caddy restart, depending on how you are running caddy).

Also, make sure that your website’s file is accessible to caddy (if it has permission to read and write). If you are uncertain if it has permission, use chmod 777 /path/to/your/website/files -R, but be aware that it is just for testing, don’t keep 777. You may also need to restart you computer.

This is it folks, I hope it helps someone. Apologizes for not following the template, I judge that it wouldn’t be necessary.

FWIW, if you installed Caddy with the apt repo from Install — Caddy Documentation, the caddy user would be added to the www-data group automatically.

But if you installed Caddy manually, then you did the right thing to fix it :+1:

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