Considering the following working Apache config (used inside the httpd:2.4.48-alpine docker container)
<VirtualHost *:80>
DocumentRoot "/code2"
<Proxy "fcgi://php/">
ProxySet enablereuse=On
</Proxy>
<FilesMatch .php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
<Directory /code2>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
DirectoryIndex index.php
</VirtualHost>
However if I move the /code2 directory which matches the path in the php-fpm container (named "php") to e.g: /code ( I also update the DocumentRoot and Directory path ).
I get a 404 on all the php files. Why is this so? how can I provide a different path to the php files for each container?
Another interesting thing to note is that both the access log of apache and php don’t display the full path to the php file thats being accessed, just the filename only.
2
Answers
If you want to use the "DocumentRoot" directive with a PHP handler, you need to mount that volume to
httpd
as well. By default, non-existent files will directly trigger a 404 (See https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM "Proxy via handler")Here’s a small example (with docker-compose for simplicity) for your use-case:
./docker-compose.yaml
:./httpd/Dockerfile
./httpd/php-fpm.conf
(using PHP handler, see https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM)If you only want to serve .php files with
php-fpm
, this could be simplified:If you want to use a different path in httpd and php-fpm you can customize the environment variables sent to php-fpm with ProxyFCGISetEnvIf.
https://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html#proxyfcgisetenvif
Unfortunately the combination of variables php-fpm might look at to determine where to look on disk can be a little confusing — it’s actually the reason this directive exists to give you a chance at the last moment to see what mod_proxy_fcgi would have sent and then tweak them.
This example from the manual is eerily (with the d suffix being dropped) close to your issue:
I suggest using this pattern to transform PATH_TRANSLATED (filesystem path) from your httpd container to the one that’s valid in the FPM container.