在一个dockerfile中将PHP-fpm与nginx结合 [英] Combining PHP-fpm with nginx in one dockerfile

查看:737
本文介绍了在一个dockerfile中将PHP-fpm与nginx结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将php-fpm与nginx结合在一个dockerfile中以进行生产部署.

I have a need to combine the php-fpm with nginx in one dockerfile for production deployment.

那么:

(1)使用php:7.1.8-fpm启动dockerfile,然后在其之上安装nginx图像层?

(1) Start the dockerfile using php:7.1.8-fpm and then install nginx image layer on top of it ?

(2)还是建议使用Nginx映像,然后使用apt-get安装php-fpm?

(2) Or do you recommend using nginx image and then installing php-fpm using apt-get ?

PS:我没有用于生产部署的docker-compose构建选项.在我的开发环境中,我已经使用docker-compose并从两个映像轻松构建了多容器应用程序.我们的组织开发人员不支持在产品环境中进行基于docker-compose的部署.

PS: I do not have a docker-compose build option for production deployment. On my development environment, I already use docker-compose and build multi-container app easily from two images. Our organization devops do not support docker-compose based deployment for prod environment.

推荐答案

Nginx的安装比PHP容易得多,因此应该更容易将Nginx安装到现成的官方PHP映像中.这是Dockerfile示例,显示了如何通过安装一些PHP扩展示例来实现您的目标:

Nginx installation is much easier than PHP so it should be easier for you to install Nginx into ready-to-use official PHP image. Here is the example Dockerfile showing how your goal can be reached with example of installing few PHP extensions:

FROM php:7.2-fpm

RUN apt-get update -y \
    && apt-get install -y nginx

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"

RUN docker-php-ext-install pdo_mysql \
    && docker-php-ext-install opcache \
    && apt-get install libicu-dev -y \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl \
    && apt-get remove libicu-dev icu-devtools -y
RUN { \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/php-opocache-cfg.ini

COPY nginx-site.conf /etc/nginx/sites-enabled/default
COPY entrypoint.sh /etc/entrypoint.sh

COPY --chown=www-data:www-data . /var/www/mysite

WORKDIR /var/www/mysite

EXPOSE 80 443

ENTRYPOINT ["/etc/entrypoint.sh"]

nginx-site.conf文件包含您的nginx http主机配置.下面的示例适用于Symfony应用:

The nginx-site.conf file contain your nginx http host configuration. The example below is for Symfony app:

server {
    root    /var/www/mysite/web;

    include /etc/nginx/default.d/*.conf;

    index app.php index.php index.html index.htm;

    client_max_body_size 30m;

    location / {
        try_files $uri $uri/ /app.php$is_args$args;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index app.php;
        include fastcgi.conf;
    }
}

entrypoint.sh将在容器启动时运行nginx和php-fpm(否则,将仅启动php-fpm作为官方PHP映像的默认操作):

The entrypoint.sh will run nginx and php-fpm on container startup (otherwise only php-fpm will be started as the default action of the official PHP image):

#!/usr/bin/env bash
service nginx start
php-fpm

从最佳实践的角度来看,这当然不是最佳方法,但我希望这是您的问题的答案.

Of course this is not a best way from the best practice perspective, but I hope this is the answer for your question.

这篇关于在一个dockerfile中将PHP-fpm与nginx结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆