New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caddy support for php-fpm #190

Closed
Nixtren opened this Issue Jul 17, 2015 · 15 comments

Comments

5 participants
@Nixtren
Copy link

Nixtren commented Jul 17, 2015

Hi,

It seems Caddy only works with the old CGI implementation (php-cgi). If I try to use the new one (php-fpm) I only get a 502 Bad Gateway error. I have checked the active sockets, so that's not the problem.

Socket: 127.0.0.1:9000 (I have also tried with the Unix Socket)

I would appreciate if I could get confirmation on this, otherwise I'm doing something wrong. Thanks for the attention.

P.S: Linux.

@mholt

This comment has been minimized.

Copy link
Owner

mholt commented Jul 17, 2015

Hmm, I've actually only ever used php-fpm with it. It should work. What does your Caddyfile look like?

@abiosoft

This comment has been minimized.

Copy link
Collaborator

abiosoft commented Jul 17, 2015

Same here, I have only used it with php-fpm. However, I've seen someone on Windows that uses php-cgi without issues too.

@Nixtren

This comment has been minimized.

Copy link

Nixtren commented Jul 18, 2015

Thanks for the answers. This is my Caddyfile:

0.0.0.0:2015
fastcgi / 127.0.0.1:9000 php

I'm trying to access /php.php

@abiosoft

This comment has been minimized.

Copy link
Collaborator

abiosoft commented Jul 18, 2015

Have you tried connecting with nginx ? Just for confirmation if php-fpm is running correctly.

@Nixtren

This comment has been minimized.

Copy link

Nixtren commented Jul 18, 2015

Edit: Hold on, I think I made a mistake in telling you the right config, I'm going to clean everything and try again from scratch. This is driving me crazy.

It's strange. It works in nginx with the following configuration:

server {
    location / {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }
}

But if I take the fastcgi_param lines it stops working. An easy solution would be Caddy to pass these FastCGI params to php-fpm. According to the nginx configuration file, php-cgi doesn't need the fastcgi_param lines to be passed, only php-fpm, so this maybe would explain why Caddy works with php-cgi but not php-fpm (in my case).

I have also managed to get Caddy working with php-fpm without passing the FastCGI params, to do that I had to put the Caddy executable in the nginx document root folder and run it under the user www-data (the same user as php-fpm). So, it seems this is a permission problem, unless those FastCGI params are passed.

@mholt

This comment has been minimized.

Copy link
Owner

mholt commented Jul 18, 2015

Yeah, php-fpm is a beast. You might be able to glean more information from a Caddy error log, if you use errors error.log in your Caddyfile then try a request.

The php preset does configure a couple things related to what nginx does with fastcgi_params, see the docs for fastcgi. Also, try renaming php.php to something else, like test.php... just to experiment (if that's what fixes it, it may be a bug in Caddy).

@Nixtren

This comment has been minimized.

Copy link

Nixtren commented Jul 18, 2015

Caddy error:

18/Jul/2015:09:56:19 -0500 [ERROR 502 /php.php] malformed MIME header line: Primary script unknown
18/Jul/2015:09:56:24 -0500 [ERROR 502 /test.php] malformed MIME header line: Primary script unknown

It seems PHP can't find the script. So, looks like a permission issue and not Caddy's problem itself. All files have to be from the user www-data (the same user as php-fpm). By the way, I can run Caddy as root to serve the document root folder owned by www-data and it works normally. The strange part is that this issue doesn't apply to php-cgi, only php-fpm. Maybe the parameters passed by nginx to FastCGI really does some difference.

Maybe this should be mentioned somewhere?

@mholt

This comment has been minimized.

Copy link
Owner

mholt commented Jul 20, 2015

Mention what, where, exactly? I'm still not clear as to whether this is a php-fpm, file permissions, or Caddy issue. You aren't able to run Caddy as www-data? Are the error messages always the same?

@Nixtren

This comment has been minimized.

Copy link

Nixtren commented Jul 20, 2015

The issue can be considered resolved as I'm able to run Caddy as www-data and is able to serve the PHP files normally (assuming the files are in a folder owned by www-data too). However, I still have to go deeper in this to see if it's just a permissions problem or the parameters passed by nginx to php-fpm do some difference (the difference between running the PHP script or throwing an error due to insufficient permissions), I just don't have time right now to do that. I will be closing this issue and get back to it once I have time (or someone else that experiences the same problem can give some infomation).

By the way, the error messages are always the same.

@Nixtren Nixtren closed this Jul 20, 2015

@mholt

This comment has been minimized.

Copy link
Owner

mholt commented Jul 20, 2015

Okay. Thanks for your detective work and answering my questions. 👍 Let me know if there's any other way we can help!

@Prezto

This comment has been minimized.

Copy link

Prezto commented Aug 12, 2015

I know this is closed, but am experiencing this now too with caddy version 0.7.5 on Centos7 with PHP 5.6.11 (fpm-fcgi) (built: Jul 10 2015 09:45:11). I am a normal user and no dev.

It does seem to reach the FPM process :

Status: "Processes active: 2, idle: 6, Requests: 14, slow: 0, Traffic: 0.1req/sec"

0.0.0.0:9090
root /root/test
log access.log
errors error.log
fastcgi / /var/run/php.sock php
rewrite {
    regexp .*
    ext    /
    to     /index.php?{query}
}

@Nixtren Nixtren reopened this Aug 12, 2015

@Prezto

This comment has been minimized.

Copy link

Prezto commented Aug 13, 2015

Nevermind Nixtren. I have finally found out this morning that if i add the user to the group php-fpm all works fine. So for others having the same problem. Add the user that runs Caddy to the php-fpm or hhvm group. Whichever you are using.

@Nixtren

This comment has been minimized.

Copy link

Nixtren commented Aug 13, 2015

Thanks @Prezto for giving out information on how you fixed it :)

Edit: Are your .php files also owned by the group php-fpm?

@Nixtren Nixtren closed this Aug 13, 2015

@Prezto

This comment has been minimized.

Copy link

Prezto commented Aug 13, 2015

Yes. All files are owned by the user/group hhvm/php-fpm. I have tested with both php-fpm and hhvm and both work fine.

@notanark

This comment has been minimized.

Copy link

notanark commented Oct 30, 2016

I found the answer, you need a space after your content-type:

I was having the same problem with C# playing around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment