caddyserver / caddy

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
https://caddyserver.com
Apache License 2.0
55.71k stars 3.92k forks source link

Can't server any PHP content #3170

Closed ilium007 closed 4 years ago

ilium007 commented 4 years ago

OS 10.14.6

Removed all hombrew php installs

Installed php 7.4 via homebrew


❯ /usr/local/sbin/php-fpm -v
PHP 7.4.4 (fpm-fcgi) (built: Mar 19 2020 20:12:43)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.4, Copyright (c), by Zend Technologies

Testing via caddyfile:

❯ cat caddyfile
:8000 {
    root * /Users/xxxx/support/xxxx/website/xxxx_dev01
    php_fastcgi 127.0.0.1:9000
    file_server
}
I get static HTML rendered but no PHP content.

php-fpm is running and listening on port 9000

❯ nc -vz 127.0.0.1 9000
found 0 associations
found 1 connections:
     1: flags=82<CONNECTED,PREFERRED>
    outif lo0
    src 127.0.0.1 port 59367
    dst 127.0.0.1 port 9000
    rank info not available
    TCP aux info available

Connection to 127.0.0.1 port 9000 [tcp/cslistener] succeeded!
francislavoie commented 4 years ago

Can you share Caddy logs and tree your site? What version of Caddy are you running? We need more info here.

FYI, the preferred place to ask usage questions is on https://caddy.community. This may be a bug, but we won't know until you give us more information.

ilium007 commented 4 years ago

Caddy 2.0 beta 18 (latest as of today)

I have made the test simpler:

The HTML file

❯ cat index.html
<?php require 'phpinfo.php';?>

PHP file

❯ cat phpinfo.php
<?php

// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);

?>

I can't get logging to work as per: https://caddyserver.com/docs/caddyfile/directives/log run: adapting config using caddyfile: ./caddyfile:6: unrecognized directive: log

high3eam commented 4 years ago

@ilium007 What does your newest Caddyfile look like (because you've tried to add logging)?

Logging can look like this (if output is written to a file):

log {
                output file /path/to/access.log
        }
ilium007 commented 4 years ago

Thats what I used after reading the docs. That is what failed and caused the error message above.

ilium007 commented 4 years ago

caddyfile

:8000 {
    root * /Users/xxxx/support/xxxx/caddy/
    php_fastcgi 127.0.0.1:9000
    file_server
    log {
        output file /Users/xxxx/support/access.log
    }
}
ilium007 commented 4 years ago

Also tried:

:8000 {
    root * /Users/xxxx/support/xxxx/caddy/
    php_fastcgi 127.0.0.1:9000
    file_server
}

log {
    output file /Users/xxxx/support/access.log
}
mohammed90 commented 4 years ago

What is your Caddy version as reported by caddy version?

ilium007 commented 4 years ago

Beta 18 as I stated above:

❯ ~/Downloads/caddy2_beta18_macos version
v2.0.0-beta.18 h1:N8CR8nO+n9Sk16CBx6ysVZWIZv0hgtU1J84st1bFWgI=
high3eam commented 4 years ago

@ilium007 Does your Caddy v2 user have write access to access.log?

Also, try to delete autosave.json before running new Caddy v2, so it is forced to adapt a fresh Caddyfile. (rm /YourUsersHomeDir/.config/caddy/autosave.json)

ilium007 commented 4 years ago

Yes it has write access. Why is a binary download writing config files into:

/Users/xxxxx/Library/Application\ Support/Caddy/autosave.json

ilium007 commented 4 years ago

Deleted autosave.json:


~/support/xxxx/caddy
❯ ~/Downloads/caddy2_beta10_macos run --config ./caddyfile --adapter caddyfile
run: adapting config using caddyfile: ./caddyfile:5: unrecognized directive: log

~/support/xxxx/caddy
❯

Removed the log directive and it starts. I can serve an info.php page now but includes / requires in the html do not work

francislavoie commented 4 years ago

In that last comment you ran beta10, not beta18. That might be your issue.

francislavoie commented 4 years ago

As for your question about the config directory, that's documented here: https://caddyserver.com/docs/conventions#configuration-directory

This is particularly useful for users of the API to push config changes. It's persisted to disk so that no config is lost if Caddy stops unexpectedly.

ilium007 commented 4 years ago

I hit the up arrow too many times - I have been testing all beta versions tonight

ilium007 commented 4 years ago

Beta18

❯ ~/Downloads/caddy2_beta18_macos run --config ./caddyfile --adapter caddyfile
2020/03/22 11:26:06.396 INFO    using provided configuration    {"config_file": "./caddyfile", "config_adapter": "caddyfile"}
2020/03/22 11:26:06.398 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["localhost:2019"]}
2020/03/22 22:26:06 [INFO][cache:0xc00065bdb0] Started certificate maintenance routine
2020/03/22 11:26:06.400 INFO    tls cleaned up storage units
2020/03/22 11:26:06.400 INFO    autosaved config    {"file": "/Users/xxxx/Library/Application Support/Caddy/autosave.json"}
2020/03/22 11:26:06.400 INFO    serving initial configuration

I have worked out it is PHP includes and requires not working. I can execute a php page if I access directly - ie. my test info.php page

ilium007 commented 4 years ago

When I used to run PHP-FPM for NGINX I would have a snippets file for PHP-FPM config:

❯ cat /usr/local/etc/nginx/snippets/fastcgi-php.conf
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 512k;
fastcgi_buffers 512 16k;
fastcgi_busy_buffers_size 1m;
fastcgi_temp_file_write_size 4m;
fastcgi_max_temp_file_size 4m;
fastcgi_intercept_errors off;

#fastcgi_param SCRIPT_FILENAME   $request_filename;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO         $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
fastcgi_param QUERY_STRING      $query_string;
fastcgi_param REQUEST_METHOD    $request_method;
fastcgi_param CONTENT_TYPE      $content_type;
fastcgi_param CONTENT_LENGTH    $content_length;
fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
fastcgi_param REQUEST_URI       $request_uri;
fastcgi_param DOCUMENT_URI      $document_uri;
fastcgi_param DOCUMENT_ROOT     $document_root;
fastcgi_param SERVER_PROTOCOL   $server_protocol;
fastcgi_param REQUEST_SCHEME    $scheme;
fastcgi_param HTTPS             $https if_not_empty;
fastcgi_param HTTP_PROXY        "";
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version;
fastcgi_param REMOTE_ADDR       $remote_addr;
fastcgi_param REMOTE_PORT       $remote_port;
fastcgi_param SERVER_ADDR       $server_addr;
fastcgi_param SERVER_PORT       $server_port;
fastcgi_param SERVER_NAME       $server_name;
fastcgi_param REDIRECT_STATUS   200;

Where does this config live now?

francislavoie commented 4 years ago

Most of those options are automatic/implicit so you won't need them - but others are available via the reverse_proxy options https://caddyserver.com/docs/caddyfile/directives/reverse_proxy you can specify the same transport options to the php_fastcgi directive if I remember correctly.

Also, if you haven't already, read the documentation for php_fastcgi https://caddyserver.com/docs/caddyfile/directives/php_fastcgi it's effectively just a shortcut to reverse_proxy with the fastcgi transport, but with a few things to make the typical PHP app work out of the box.

Closing because this definitely is looking like more usage questions than any bugs.

ilium007 commented 4 years ago

Logging working. log {} block had to go before the rest of the config:

:8000 {

    log {
        output file /Users/xxxx/support/access.log
        level DEBUG
    }

    root * /Users/xxxx/support/xxxx/caddy/

    php_fastcgi 127.0.0.1:9000

    file_server
}

Log shows no error when I attempt to access the html file with the embedded include / require

The only error is a favicon 404 which I don't care about

❯ ~/Downloads/caddy2_beta18_macos run --config ./caddyfile --adapter caddyfile
2020/03/22 11:45:15.437 INFO    using provided configuration    {"config_file": "./caddyfile", "config_adapter": "caddyfile"}
2020/03/22 11:45:15.441 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["localhost:2019"]}
2020/03/22 22:45:15 [INFO][cache:0xc000651f90] Started certificate maintenance routine
2020/03/22 11:45:15.443 INFO    tls cleaned up storage units
2020/03/22 11:45:15.443 INFO    autosaved config    {"file": "/Users/xxxx/Library/Application Support/Caddy/autosave.json"}
2020/03/22 11:45:15.443 INFO    serving initial configuration

2020/03/22 11:45:23.161 INFO    http.log.access handled request {"request": {"method": "GET", "uri": "/index.html", "proto": "HTTP/1.1", "remote_addr": "[::1]:50080", "host": "localhost:8000", "headers": {"User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Cache-Control": ["no-cache"], "Pragma": ["no-cache"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "Cookie": ["_gcl_au=1.1.991188801.1584583760"], "Upgrade-Insecure-Requests": ["1"]}}, "common_log": "::1 - - [22/Mar/2020:22:45:23 +1100] \"GET /index.html HTTP/1.1\" 200 189", "latency": 0.003742685, "size": 189, "status": 200, "resp_headers": {"Last-Modified": ["Sun, 22 Mar 2020 11:41:44 GMT"], "Accept-Ranges": ["bytes"], "Content-Length": ["189"], "Server": ["Caddy"], "Etag": ["\"q7lf5k59\""], "Content-Type": ["text/html; charset=utf-8"]}}

2020/03/22 11:45:23.242 ERROR   http.log.access handled request {"request": {"method": "GET", "uri": "/favicon.ico", "proto": "HTTP/1.1", "remote_addr": "[::1]:50080", "host": "localhost:8000", "headers": {"Cache-Control": ["no-cache"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0"], "Accept": ["image/webp,*/*"], "Accept-Encoding": ["gzip, deflate"], "Cookie": ["_gcl_au=1.1.991188801.1584583760"], "Pragma": ["no-cache"], "Accept-Language": ["en-US,en;q=0.5"], "Connection": ["keep-alive"]}}, "common_log": "::1 - - [22/Mar/2020:22:45:23 +1100] \"GET /favicon.ico HTTP/1.1\" 404 0", "latency": 0.000183923, "size": 0, "status": 404, "resp_headers": {"Server": ["Caddy"]}}

2020/03/22 11:45:33.046 INFO    http.log.access handled request {"request": {"method": "GET", "uri": "/info.php", "proto": "HTTP/1.1", "remote_addr": "[::1]:50080", "host": "localhost:8000", "headers": {"Connection": ["keep-alive"], "Cookie": ["_gcl_au=1.1.991188801.1584583760"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate"]}}, "common_log": "::1 - - [22/Mar/2020:22:45:33 +1100] \"GET /info.php HTTP/1.1\" 200 90893", "latency": 0.001973814, "size": 90893, "status": 200, "resp_headers": {"X-Powered-By": ["PHP/7.4.4"], "Content-Type": ["text/html; charset=UTF-8"], "Server": ["Caddy"]}}

2020/03/22 11:45:42.503 INFO    http.log.access handled request {"request": {"method": "GET", "uri": "/footer.php", "proto": "HTTP/1.1", "remote_addr": "[::1]:50080", "host": "localhost:8000", "headers": {"User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "Cookie": ["_gcl_au=1.1.991188801.1584583760"], "Upgrade-Insecure-Requests": ["1"]}}, "common_log": "::1 - - [22/Mar/2020:22:45:42 +1100] \"GET /footer.php HTTP/1.1\" 200 33", "latency": 0.000862664, "size": 33, "status": 200, "resp_headers": {"X-Powered-By": ["PHP/7.4.4"], "Content-Type": ["text/html; charset=UTF-8"], "Server": ["Caddy"]}}
ilium007 commented 4 years ago

Great. Thanks for the help.

Also, if you haven't already, read the documentation for php_fastcgi https://caddyserver.com/docs/caddyfile/directives/php_fastcgi it's effectively just a shortcut to reverse_proxy with the fastcgi transport, but with a few things to make the typical PHP app work out of the box.

I've been reading the docs for the past 3 hours

francislavoie commented 4 years ago

Oh I see what's going on. You're including PHP code inside of an .html file. That's definitely not standard. Rename your file to index.php and it will work fine.

ilium007 commented 4 years ago

Rename your file to index.php and it will work fine.

Whats wrong with that?? Its inline PHP - its perfectly valid and has worked fine on NGINX and Apache for years.

francislavoie commented 4 years ago

No, PHP code only runs in PHP files. A PHP file can include and output HTML. It's non-standard to do otherwise.

mholt commented 4 years ago

It can work in Caddy too, you just have to tell it to execute php in HTML files ...

ilium007 commented 4 years ago

It can work in Caddy too, you just have to tell it to execute php in HTML files ...

Thats exactly what I have on the legacy Apache site I am trying to move in to dev

ilium007 commented 4 years ago

I think this (form docs page) is what I am looking for:

Most modern PHP apps work well with this preset. If yours does not, feel free to borrow from this and customize it as needed instead of using the php_fastcgi shortcut.

I've been thinking I can do what I want with the php_fastcgi shortcut

francislavoie commented 4 years ago

I'd very strongly recommend just renaming the files. You don't want to keep dragging along these legacy practices forward. There's no good reason to keep them as .html files.

ilium007 commented 4 years ago

Understood - but I don't have time to change the whole site. Trying to react quickly due to Covid-19 and loss of 95% of our airline business.

mholt commented 4 years ago

You should be able to change the instances of .php in the expanded form in the docs to .html, see how far that gets you. Let us know!

ilium007 commented 4 years ago

You should be able to change the instances of .php in the expanded form in the docs to .html, see how far that gets you. Let us know!

I did that and it worked. Thanks. v2 of the site will be fixed once I get my job back. Working for free to try and save our livelihoods currently.

mholt commented 4 years ago

I'm cheering you on. Let me know if Caddy can be of any further help.