Undefined array key "SERVER_ADDR" with Caddy + PHP-FPM

Environment

Server

Ubuntu 22.04 (5.15.0-41-generic x86_64)
PHP 8.1 FPM (from package php8.1-fpm 8.1.2-1ubuntu2)
Lightttpd 1.4.63-1ubuntu3
Docker 20.10.13~3-0~ubuntu-jammy (package docker-ce from docker repo)

Pi-hole v5.11.4
FTL v5.16.1
Web Interface v5.13

Docker

caddy v2.5.1 (from caddy latest image)

docker run -d \
  --name caddy \
  --restart unless-stopped \
  --net host \
  -v /opt/caddy/Caddyfile:/etc/caddy/Caddyfile:ro \
  -v /opt/caddy/config:/config \
  -v /opt/caddy/data:/data \
  -v /run/php/php8.1-fpm.sock:/run/php/php-fpm.sock \
  caddy:latest

Caddyfile relevant piece

:80 {
        root * /var/www/html
        php_fastcgi unix//run/php/php-fpm.sock
        file_server browse
}

Expected Behaviour:

  1. Access http://server_ip/admin/
  2. Presented with Pi-Hole login, use password, login
  3. Dashboard and Query Log show server data

Actual Behaviour:

On steps 2 and 3, the below message is shown at the top of the page:

Undefined array key "SERVER_ADDR" in /var/www/html/admin/scripts/pi-hole/php/auth.php

On clicking on Query Log, it shows a JavaScript alert with other errors including error message above.

If I access the installation through the lighttpd installed on server (running on another port), no error message is shown.

Debug Token:

https://tricorder.pi-hole.net/1DRxdakV/

1 Like

A post was split to a new topic: Undefined array key "client_id" in api_db.php

FYI. Same behavior observed when running caddy outside of Docker.

As you've observed, Pi-hole comes with its own web server lighttpd.

As lighttpd is included in Pi-hole's image, it would always be active to serve Pi-hole's web UI if you would run your Pi-hole as a Docker container.
This would conflict over port 80/HTTP with any other webserver on the same machine.

To coexist with another webserver like Caddy, you'd have the option of mapping one of the containers to a different port with Docker, or to configure your Pi-hole container for a different WEB_PORT environment variable.

Yes, I'm aware of the install and the ports.

I've configured Caddy to be served on port 80 while Lighthttpd runs on another port. Need to say that any upgrade to Pi-Hole does reset my custom Lighthttpd.conf (just setting the port)

A permanent method of changing the lighttpd port (for lighttpd version 1.4.46 or later):

Check your lighttpd version with this command.

lighttpd -v

If that returns 1.4.46 or above, you can overwrite the port from Pi-hole's lighttpd.conf in your external.conf like this:

server.port := 8888

Note that the := operator isn't supported in older versions of lighttpd .

/etc/lighttpd/lighttpd.conf should not be edited at all, as per disclaimer at the top of that file:

###############################################################################
#     FILE AUTOMATICALLY OVERWRITTEN BY PI-HOLE INSTALL/UPDATE PROCEDURE.     #
# ANY CHANGES MADE TO THIS FILE AFTER INSTALL WILL BE LOST ON THE NEXT UPDATE #
#                                                                             #
#              CHANGES SHOULD BE MADE IN A SEPARATE CONFIG FILE:              #
#                         /etc/lighttpd/external.conf                         #
###############################################################################

Would you run a dockered or a bare metal Pi-hole?

For a Pi-hole Docker container, you should make use of the options I've mentioned above.

For a bare metal Pi-hole, put your custom configuration in external.conf, and apply jfb's suggestion as required.

A possible approach to have your additional webserver work with both kinds of Pi-hole may consider to have Caddy redirect, rewrite or proxy the URLs to lighttpd's port.

Right. I applied the change to external.conf as indicated.
However, it does not fix the problem with both problem messages. This on caddy (bare metal or docker) in the Dashboard:

Undefined array key "SERVER_ADDR" in /var/www/html/admin/scripts/pi-hole/php/auth.php

And this into the Long Term Data > Query Log in lighttpd bare metal

Undefined array key "client_id" in /var/www/html/admin/api_db.php on line 127

Give this second error is within the bare metal default installation, do I need to create another thread in the help since this is not a customized installation?

Note that lighttpd is the only web server officially supported by Pi-hole.

So you are running a bare metal Pi-hole with its lighttpd web server active and configured for a custom port.

In that case, Pi-hole's UI should be available at http://pi.hole:<custom-port>/admin - there would be no immediate need to involve a different webserver.

Nevertheless, as mentioned, based on your intention, you should be able to configure your Caddy webserver to point HTTP requests to be handled by lighttpd's port instead of having Caddy handling them.

This would essentially be a question of how to configure Caddy, so you may want to consider consulting Caddy's community support as well.


From your description, your second observation may constitute a different issue altogether (click for quote details):

I'm going to split that into a separate topic.

Yeah, my intention was to only use Caddy. I only maintaned and ran lighttpd to see if I could reproduce it there. Since I cannot reproduce it on lighttpd, I found this to be a Caddy problem on not populating the $_SERVER array in PHP.

This is the print_r function on $_SERVER in lighttpd

Array ( 
	[PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin 
	[PHP_FCGI_CHILDREN] => 4 
	[PHP_FCGI_MAX_REQUESTS] => 10000 
	[PATH_TRANSLATED] => /var/www/html 
	[ORIG_SCRIPT_FILENAME] => /var/www/html/admin/db_queries.php/ 
	[HTTP_X_FORWARDED_PROTO] => http 
	[HTTP_X_FORWARDED_HOST] => 192.168.0.3 
	[HTTP_X_FORWARDED_FOR] => 192.168.0.30 
	[HTTP_UPGRADE_INSECURE_REQUESTS] => 1 
	[HTTP_REFERER] => http://192.168.0.3/admin/db_queries.php 
	[HTTP_CACHE_CONTROL] => max-age=0 
	[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7 
	[HTTP_ACCEPT_ENCODING] => gzip, deflate 
	[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 
	[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 
	[HTTP_HOST] => 192.168.0.3 
	[REMOTE_PORT] => 53512 
	[REMOTE_ADDR] => 127.0.0.1 
	[SERVER_NAME] => 192.168.0.3 
	[SERVER_ADDR] => 127.0.0.1 
	[SERVER_PORT] => 9015 
	[REQUEST_SCHEME] => http 
	[GATEWAY_INTERFACE] => CGI/1.1 
	[SERVER_SOFTWARE] => lighttpd/1.4.63 
	[SERVER_PROTOCOL] => HTTP/1.1 
	[REQUEST_METHOD] => GET 
	[DOCUMENT_ROOT] => /var/www/html 
	[SCRIPT_FILENAME] => /var/www/html/admin/db_queries.php 
	[SCRIPT_NAME] => /admin/db_queries.php 
	[REDIRECT_STATUS] => 200 
	[REQUEST_URI] => /admin/db_queries.php 
	[QUERY_STRING] => 
	[CONTENT_LENGTH] => 0 
	[FCGI_ROLE] => RESPONDER 
	[PHP_SELF] => /admin/db_queries.php 
	[REQUEST_TIME_FLOAT] => 1658163472.3519 
	[REQUEST_TIME] => 1658163472 
)

And in Caddy:

Array ( 
	[USER] => www-data 
	[HOME] => /var/www 
	[SERVER_PROTOCOL] => HTTP/1.1 
	[REMOTE_HOST] => 192.168.0.30 
	[QUERY_STRING] => 
	[HTTP_X_FORWARDED_PROTO] => http 
	[SERVER_PORT] => 81 
	[REMOTE_USER] => 
	[REMOTE_PORT] => 55889 
	[GATEWAY_INTERFACE] => CGI/1.1 
	[SCRIPT_NAME] => /admin/index.php 
	[REQUEST_URI] => /admin/ 
	[DOCUMENT_ROOT] => /var/www/html 
	[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7 
	[SCRIPT_FILENAME] => /var/www/html/admin/index.php 
	[REQUEST_METHOD] => GET 
	[HTTP_REFERER] => http://192.168.0.3:81/ 
	[SERVER_SOFTWARE] => Caddy/v2.5.2 
	[REQUEST_SCHEME] => http 
	[CONTENT_TYPE] => 
	[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 
	[HTTP_HOST] => 192.168.0.3:81 
	[DOCUMENT_URI] => /admin/index.php 
	[REMOTE_ADDR] => 192.168.0.30 
	[PATH_INFO] => 
	[CONTENT_LENGTH] => 0 
	[HTTP_X_FORWARDED_FOR] => 192.168.0.30 
	[HTTP_X_FORWARDED_HOST] => 192.168.0.3:81 
	[SERVER_NAME] => 192.168.0.3 
	[HTTP_ACCEPT_ENCODING] => gzip, deflate 
	[REMOTE_IDENT] => 
	[AUTH_TYPE] => 
	[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 
	[HTTP_UPGRADE_INSECURE_REQUESTS] => 1 
	[FCGI_ROLE] => RESPONDER 
	[PHP_SELF] => /admin/index.php 
	[REQUEST_TIME_FLOAT] => 1658163666.311 
	[REQUEST_TIME] => 1658163666 
)

I found this issue in Caddy that is related and suggests a fix for passing an environment variable:

After modifying my Caddyfile to add the environment variable, Pi-hole works fine now. Leaving here if any other users face the same problem:

        root * /var/www/html
        php_fastcgi unix//run/php/php-fpm.sock {
                env SERVER_ADDR {host}
        }
        file_server browse

I used {host} instead of a fixed value as this value is used for CORS validation.

Yeah, and have a fix :slight_smile:

1 Like

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