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

See also my approach which contains nothing else as needed: https://github.com/stucki/docker-lemp

stucki avatar Mar 13 '15 20:03 stucki

So IMO what is really the issue, is only the lack of documentation. A fpm+nginx image is not really in the spirit of docker as was mentioned before. Maybe the README could contain a note on the php-fpm tags?

Something along the lines of:

To bring up an application with php-fpm you have to:

  • create a Dockerfile for a simple nginx container (FROM nginx) and add a custom /etc/nginx/nginx.conf
  • create a php-fpm container from php:fpm and mount your source code as a volume e.g. to /var/www/html
  • link the php-fpm container to the nginx container
  • link the volumes from php-fpm to your nginx

In the nginx.conf it's important that

  • the web root points to the volume that is linked from php-fpm and
  • you use the php-fpm container name as PHP proxy hostname

Example:

Dockerfile:

FROM nginx:1.7.10
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen 80;
        root /var/www/html;
        index index.html index.php;
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
            fastcgi_pass   php-fpm:9000;
            try_files $uri =404;
        }
    }
}
docker build -t mynginx .
docker run -d -v /path/to/app:/var/www/html --name php-fpm php:5.6.6-fpm
docker run -p 8080:80 --volumes-from php-fpm --link php-fpm:php-fpm mynginx

mikehaertl avatar Mar 14 '15 08:03 mikehaertl

@mikehaertl agreed :+1:

I did a very similar config to yours as a gist a while back for the wordpress:fpm image: https://gist.github.com/md5/d9206eacb5a0ff5d6be0

I'm sure nobody would object to having this better documented, it's just a matter of someone having the time and inclination to do so.

md5 avatar Mar 14 '15 08:03 md5

I did a very similar config to yours as a gist a while back for the wordpress:fpm image:

@md5 Oh, indeed. Must have missed that :).

mikehaertl avatar Mar 14 '15 08:03 mikehaertl

@mikehaertl Thanks for the above commands. I tried your example and it worked flawlessly

Thanks alot

IbnSaeed avatar Apr 29 '15 10:04 IbnSaeed

Thanks for this interesting discussion.

To me, as well, it makes totally sense to run run FPM and nginx in two separate containers.

But: How does this scale? E.g. in AWS Elastic Beanstalk with a load balancer in front of it? If you use php-apache, you’ve got one image per app and AWS may spawn/kill as many containers as needed.

In the FPM scenario this doesn’t work anymore. You would need two containers (nginx+fpm) per instance …

UPDATE: Ah, well. This is how Amazon proposes doing it, two containers per instance: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_ecstutorial.html

intellent avatar Aug 23 '15 06:08 intellent

@intellent I got confused by your comment because "EBS" usually means Elastic Block Store. At first I thought you actually meant ECS (i.e. EC2 Container Service), but I see now that you're actually talking about Elastic Beanstalk (which uses ECS under the hood for container deployments).

Looks like the docs you found are the right ones for what you're trying to do. :+1:

md5 avatar Aug 23 '15 14:08 md5

@md5 Oh, one should never make acronyms up on the fly. Sorry for the confusion. Updated. :-)

intellent avatar Aug 23 '15 15:08 intellent

@intellent From our tests I can say that the nginx container usually needs much less resources than the fpm container. Which means that you can, i.e. insert a haproxy container in between nginx and fpm and just scale the fpm process. We scaled from 1:1 to 1:30 (nginx:fpm) for load testing without having nginx as the limiting factor.

schmunk42 avatar Aug 24 '15 09:08 schmunk42

@schmunk42 Very interesting. How do you manage the deployment? Self-managed infrastructure? It should also be possible to use nginx as load balancer for the fpm instances, right?

intellent avatar Aug 24 '15 11:08 intellent

Yes, nginx should also be able to do that. We're using https://github.com/tutumcloud/haproxy on the tutum infrastructure. In this setup the haproxy takes also care about connecting containers on different physical nodes.

schmunk42 avatar Aug 24 '15 12:08 schmunk42

@schmunk42 How can I get a 1:30 relationship between nginx and php-fpm? The only solution I can think of is to have haproxy load balancing multiple nginx/php-fpm container pairs. Could you share how you balance multiple php-fpm backends with a single nginx frontend?

jamiehannaford avatar Sep 23 '15 11:09 jamiehannaford

@jamiehannaford To be honest, I haven't fully tested it, but the idea would be something like this: http://nginx.org/en/docs/http/ngx_http_upstream_module.html

upstream websbite {
    server PHPFPM_1_PORT_9000_TCP;
    server PHPFPM_2_PORT_9000_TCP;
    server PHPFPM_3_PORT_9000_TCP;
}

The tricky part is scaling up and down and changing the config dynamically, that's a feature haproxy is doing for you.

schmunk42 avatar Sep 23 '15 12:09 schmunk42

Would be great to have this documented! May be also giving a hint in the nginx docs about linking, because there are a lot of nginx-php, nginx-hhvm, nginx-memcached nginx-whatever containers out there.

J0WI avatar Dec 18 '15 21:12 J0WI

Hi @mikehaertl , Im using what you have suggested, all are running fine but in web browser im not getting any response. in nginx logs response code is 200 but no matter in the web browser.

knvpk avatar Dec 20 '15 07:12 knvpk

I solved it by using xdrum/nginx-extras image for perl/lua support in nginx config to use system environment variables so I can link nginx-fpm + php-fpm together.

strarsis avatar Jan 07 '16 17:01 strarsis

This really should be handled by using two containers linked together somehow (either with linking, Docker Networks, or some other method). To that end, I'm closing this issue in favor of https://github.com/docker-library/docs/issues/238. :+1:

tianon avatar Aug 22 '16 22:08 tianon