php icon indicating copy to clipboard operation
php copied to clipboard

PHP-FPM + nginx version

Open schmunk42 opened this issue 11 years ago • 37 comments

Since there's an Apache flavour available of the PHP Docker image, what's about a similar solution for PHP -FPM + nginx?

PS: I found no instructions for fpm at all - I can start the image, but I didn't manage it to get anything out of port 9000, not even an error.

schmunk42 avatar Dec 04 '14 11:12 schmunk42

We can't really do an FPM with Nginx image since it would be two separate processes in the image, which would require something like supervisord and that is not the direction we currently want for official images. We are working on documentation to run Nginx and FPM together.

yosifkit avatar Dec 04 '14 20:12 yosifkit

We can't really do an FPM with Nginx image since it would be two separate processes in the image.

I understand what you mean and support that view also, but having two containers just for a simple web-application looks also a bit too much to me - anyway - looking forward to the docs and your solution.

schmunk42 avatar Dec 05 '14 08:12 schmunk42

I also had this thought today, and I'm looking forward to the docs!

atmoz avatar Dec 21 '14 19:12 atmoz

I've just wanted to share my take on it https://github.com/wtool/nginx (control of PHP version trought env variable) and https://github.com/wtool/php (builds all php versions{latest 5.3+} with php-build).

dz0ny avatar Jan 21 '15 11:01 dz0ny

Here's my approach https://github.com/phundament/docker - tailored for our app and with composer pre-installed, but you'll get the idea. PHP is running as a service but exposes no ports, nginx is triggered via /root/run.sh and should be "the running process" in the container.

schmunk42 avatar Jan 21 '15 11:01 schmunk42

I also tried to use the fpm only version (php:5.6.5-fpm, docker 4.1) but can't get the fpm process to successfully bind to 9000. The fpm process outputs "ready for incoming connections" and thats it. Further investigations with netstat show now LISTEN statement at all. So I guess, the php-fpm manager doesn't start properly.

As we currently deploy out php apps with chef scripted nginx + php-fpm I don't want to include apache as a new depency. So I am very interested in having a solid solution for php-fpm and I want to avoid maintaining out own php-fpm Dockerfiles. I appreciate any help.

bennibu avatar Jan 29 '15 13:01 bennibu

Where did you run netstat? How is the nginx container finding out how to connect to the fpm container?

md5 avatar Jan 29 '15 14:01 md5

O.k. I have to confess. There were no problem in the fpm server. fpm binds sucessfully to 9000, I have used the wrong commands. Nevertheless, I will also have to include an nginx for myself, because for deploying wordpress we also need to serve some static content, which we can't deliver with the php-fpm server, as I understand for now.

So I don't see a solution to not violate the docker best practive of having only one running process in a container at the time. Maybe we can mount the wordpress sources in both containers, but that would require running them on the same host.

Or stick to the provided mode-php-apache concept. But this will introduce another dependency, and we haven't used apache for a long time.

Any suggestions?

bennibu avatar Jan 30 '15 15:01 bennibu

@bennibu I had the same thoughts. Could somebody elaborate more on the difference between the php-fpm "service" for nginx and the apapche mod_php - I know that the module is in a child fork of apache and therefore not counted as a separate service. What unexpected things could happen when using only one container with a running nginx and a PHP in the same container?

It looks to me like, if we'd use the PHP cli (theoretically speaking) with nginx we'd not violate the container principle. But that setup makes no sense.

schmunk42 avatar Jan 30 '15 15:01 schmunk42

As one example, the nginx process could die, and you wouldn't notice right away and be confused why your site is down when your container is up.

tianon avatar Jan 30 '15 17:01 tianon

@bennibu I'd look at making an analogous image to wordpress, except based on php:fpm instead of php:apache. To do this, I'd copy as much of the entrypoint of the existing wordpress image as possible as well as the logic for downloading Wordpress into /usr/src/wordpress and the exposure of /var/www/html as a volume. I'd then craft a custom image based on nginx that serves static assets from a shared /var/www/html/wp-content/whatever and share the volume with the Wordpress container. That same nginx would be configured to talk to FPM on port 9000 for non-static requests.

@tianon Do you think it would be reasonable to have a wordpress:fpm variant in the official images based on php:fpm?

md5 avatar Jan 30 '15 17:01 md5

@md5 that sounds divine :)

tianon avatar Jan 30 '15 17:01 tianon

I have a quick POC for the Wordpress part here: https://github.com/md5/wordpress/commit/c299bbb20d8021ec144a72fdcc79d8b959aa447a

I tried to whip up a quick nginx config to work with it and got it to proxy to the FPM container, but I hit a redirect loop on the install and don't have time today to work on it anymore.

The only thing I had to do besides copying the Dockerfile and entrypoint was to switch the base image to php:fpm, change the CMD to php-fpm, and remove Apache-related stuff that needs to be handled outside the wordpress:fpm image (i.e. the .htaccess stuff).

I also switched the rsync in the entrypoint to tar (after a helpful suggestion someone made in the synctree/mediawiki image) and removed the rsync install from the Dockerfile. This could probably be done in the main wordpress variant as well.

I'm not ready to open a PR for that wordpress:fpm work until I get it working with an example Nginx conf, but if someone wants to run with it I won't stop them.

md5 avatar Jan 30 '15 18:01 md5

Ooh, using "tar" as an "rsync" replacement is clever.

This definitely needs some love (as you outlined), but it looks like some good progress! :+1:

tianon avatar Jan 30 '15 18:01 tianon

Apache-related stuff that needs to be handled outside the wordpress:fpm image (i.e. the .htaccess stuff)

Might be useful to warn people that .htaccess only applies to Apache and running WordPress on NGINX may need extra configuration. IIRC, some plugins (and WordPress itself) modify the .htaccess to add rewrite-rules etc. and I'm not sure those properly detect NGINX. Sure, this is something the user should research, but there's a lot of non-too experienced users using WordPress that may need the extra pointers.

Looks like this page collects issues/hints for running WordPress on NGINX; http://codex.wordpress.org/Nginx

thaJeztah avatar Jan 30 '15 23:01 thaJeztah

Since Nginx would be running in a different container and the nginx/php-fpm integration would not work out of the box with a stock Nginx container, I think this concern could be easily handled in the docs that describe how to front the wordpress:fpm image with Nginx. This could be done either in the form of an example configuration or in the form of a preconfigured image derived from the official nginx image.

On Fri, Jan 30, 2015 at 3:03 PM, Sebastiaan van Stijn < [email protected]> wrote:

Apache-related stuff that needs to be handled outside the wordpress:fpm image (i.e. the .htaccess stuff)

Might be useful to warn people that .htaccess only applies to Apache and running WordPress on NGINX may need extra configuration. IIRC, some plugins (and WordPress itself) modify the .htaccess to add rewrite-rules etc. and I'm not sure those properly detect NGINX. Sure, this is something the user should research, but there's a lot of non-too experienced users using WordPress that may need the extra pointers.

Looks like this page collects issues/hints for running WordPress on NGINX; http://codex.wordpress.org/Nginx

— Reply to this email directly or view it on GitHub https://github.com/docker-library/php/issues/54#issuecomment-72286172.

md5 avatar Jan 30 '15 23:01 md5

Yes, the image should be accompanied with some example(s) and docs. If only to prevent loads of support questions.

Nice work on the image, though, having an official WordPress NGINX image would be great.

thaJeztah avatar Jan 30 '15 23:01 thaJeztah

I just had some time to play around and get a working Nginx config to front the wordpress:fpm image I created earlier today: https://gist.github.com/md5/d9206eacb5a0ff5d6be0

I started from the config in the Codex, but it seemed be having issues due to not passing through PATH_INFO or something. The working config added a few lines I found over here to set some additional fastcgi_param values: http://superuser.com/questions/717863/nginxphp-fpm-setup-why-is-this-configuration-workaround-needed-to-properly-set

md5 avatar Jan 31 '15 06:01 md5

@md5 Thanks for your work so far. I like this approach and I will give it a try later. Although we haven't used wordpress with apache in our deployments a long time ago, I will give this combination another chance. Especially when the wordpress world still sticks to apache-php as default deployment stack. Beside this I also think, that with an nginx in front of apache-php we maybe can profit of both worlds. Simplicity in setup and deployment and fast delivery of static contents via nginx (or varnish as the better proxy cache). I am not sure yet, we have to evaluate both strategies.

Beside the decision about the webserver the biggest problem is still wordpress requirements for a writable filesystem for uploads/themes so you can't scale your deployments over multiple hosts without a distributed filesystem. I opened an issue in the wordpress docker repo.

bennibu avatar Jan 31 '15 12:01 bennibu

I've built a working php-fpm + nginx setup now for my yii2-dockerized project. It was actually pretty simple. I build a small nginx image with a dedicated configuration for a yii2 application. And fire up the two (or 3 with db) machines with a simple docker-compose.yml. The core part there is:

app:
    build: ./
    expose:
        - "9000"
nginx:
    build: ./nginx
    ports:
        - "8080:80"
    links:
        - app
    volumes_from:
        - app

So I don't think there's a much better way to use php-fpm. If we don't want to break the one-process-per-container principle, this is the way to go.

EDIT: Oh, and of course it's crucial, that the file paths to the app code on both machines are exactly the same. So I've created a volume for this in the app container, that is shared with the nginx container.

mikehaertl avatar Mar 13 '15 19:03 mikehaertl