As far as I understand (please correct me if I'm wrong), in case of a stand-alone php-fpm server (a php-fpm server without a web server), a web server passes *.php files as "bodies" and some meta info (hostname, http verb etc.) as environment variables to the port php-fpm is listening on (9000 by default).
So, all the files a web server ever serves reside in web server's container or in a volume attached to it, and no php files are touched from php-fpm container, because they are just inaccesible for the web server.
And if so, why do wordpress:*php* Dockerfiles add wordpress files to the images? Is it by mistake? How are they supposed to be used?
I wish this issue was resolved either as a fix for Dockerfiles or as a readme improvement, currently there's no example of how to use wordress fpm images. Or maybe both, if I'm right regarding to wordpress files in fpm images being useless.
Oh, I think I gradually get it. Web server sends to php-fpm meta information (http headers, http verb, and also path to php script) and a request body (if there is one, like in POST requests). php-fpm executes the file it has 'locally' and sends back to web server its stdout.
So, wordpress files should reside in php-fpm image indeed. Still, I think an example of using nginx & wordpress:fpm should be added to readme anyway, for clarity.
Yeah, you've got it right -- it's also because initially, when launching the wordpress container your "volume" is empty, and the wordpress:fpm container is responsible for filling it up with an initial WordPress install.
To document my thinking:
If you want execute php files and at the same time to serve static assets without any interaction with php (I guess it's the most common case for nginx):
wordpress folder to be available from nginx's container, so web server will serve static files,wordpress folder available from within php-fpm container, as php-fpm server receives just a path to a php file; and it needs the actual php file to be present "locally" (for php-fpm) to execute it.So, in addition to a database persistence volume you have to create a volume with wordpress folder that will be mounted to both nginx and php-fpm container. An empty container will do, as php-fpm container's entypoint will install it in that case.
In order not to mess with paths to wordpress folder for these tho containers in nginx config, it's easier to mount to /var/www/html directory in both containers, set root /var/www/html in server {} lexical environment and set fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;.
In nginx's config, don't forget to set php-fpm's hostname to the name of the container it's running in: default fastcgi_pass localhost:9000; won't work. And, if using docker-compose, keep in mind that you have to declare a network explicitly, otherwise containers won't be able to access each other by names.
Here's my setup, I have two files, default.conf (an nginx's site config bind-mounted to /etc/nginx/conf.d/default.conf):
upstream php {
server php-fpm:9000;
}
server {
listen 80;
server_name _;
index index.php;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
and docker-compose.yml file:
version: "3"
services:
web-server:
image: nginx:1.13
networks: [wordpress]
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- wordpress:/var/www/html
ports: ["8080:80"]
php-fpm:
image: wordpress:php7.1-fpm
networks: [wordpress]
volumes: ["wordpress:/var/www/html"]
environment:
- WORDPRESS_DB_PASSWORD=secret
- WORDPRESS_DB_HOST=db
db:
image: mysql:5.7
networks: [wordpress]
volumes: ["database:/var/lib/mysql"]
environment: [MYSQL_ROOT_PASSWORD=secret]
volumes:
database:
wordpress:
# Containers are united into single network by default, but won't access each
# other by hostname without an explicitly declared network.
networks:
wordpress:
Start with docker-compose up -d and navigate to localhost:8080.
That's about what's necessary to get going with apacheless nginx + php-fpm setup for wordpress on docker. Even if we don't update readme soon, maybe someone will find it here.
Thanks, very helpful.
Thank you, I got lost trying to set that up and I reverted to apache temporarily, but that's where I want to go next. I will return to this for a starting point.
See also https://github.com/docker-library/wordpress/issues/60, https://github.com/docker-library/php/issues/54, and especially https://gist.github.com/md5/d9206eacb5a0ff5d6be0. :+1:
Adding something to the documentation does make sense, but the setup is (necessarily) complex, and so might a bit much to include a complete example.
Since this appears to be resolved and isn't a direct issue with the image itself, I'm going to close.
Most helpful comment
To document my thinking:
If you want execute php files and at the same time to serve static assets without any interaction with php (I guess it's the most common case for nginx):
wordpressfolder to be available fromnginx's container, so web server will serve static files,wordpressfolder available from withinphp-fpmcontainer, asphp-fpmserver receives just a path to a php file; and it needs the actual php file to be present "locally" (forphp-fpm) to execute it.So, in addition to a database persistence volume you have to create a volume with
wordpressfolder that will be mounted to bothnginxandphp-fpmcontainer. An empty container will do, asphp-fpmcontainer's entypoint will install it in that case.In order not to mess with paths to
wordpressfolder for these tho containers innginxconfig, it's easier to mount to/var/www/htmldirectory in both containers, setroot /var/www/htmlinserver {}lexical environment and setfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;.In
nginx's config, don't forget to setphp-fpm's hostname to the name of the container it's running in: defaultfastcgi_pass localhost:9000;won't work. And, if usingdocker-compose, keep in mind that you have to declare a network explicitly, otherwise containers won't be able to access each other by names.Here's my setup, I have two files,
default.conf(annginx's site config bind-mounted to/etc/nginx/conf.d/default.conf):and
docker-compose.ymlfile:Start with
docker-compose up -dand navigate tolocalhost:8080.That's about what's necessary to get going with apacheless nginx + php-fpm setup for wordpress on docker. Even if we don't update readme soon, maybe someone will find it here.