【问题标题】:How to make self-contained nginx and php-fpm containers properly如何正确制作自包含的 nginx 和 php-fpm 容器
【发布时间】:2018-03-21 04:15:19
【问题描述】:

我有两个 docker 容器:nginx 和 php-fpm。

我想让它们自包含(容器将在构建时从 git 克隆 repo)用于生产。但是在克隆 repo 之后,我需要做一些初始的东西,比如项目文件夹中的composer install。我可以在 php-fpm 容器内完成,因为我有 php。但是如何在 nginx 容器中准备代码呢?我没有作曲家的 php。

一切都很好,当我将相同的初始化文件夹安装到两个容器中时。

也许我做错了什么,为 nginx+php-fpm 做自包含容器的最佳方法是什么?

现在我有这个 nginx-config:

server {
    listen       80;
    server_name  localhost;

    index index.php index.html;

    # I need only /api/ path
    location /api/ {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {      
        root /var/www/public;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass api:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

【问题讨论】:

    标签: php docker nginx fpm


    【解决方案1】:

    您应该只在 FPM 服务器上拥有 PHP 文件。正如您之前提到的,您应该有 php cli 来在该服务器上运行 composer。您不应该在 nginx 服务器上需要任何 PHP 文件。 SCRIPT_FILENAME 将由 CGI 服务器解析,因此该位置不需要存在于 Web 代理上。如果您需要对 nginx 服务器进行配置更改,您可能希望使用更面向系统的东西,例如 Salt、Chef 或 Puppet。

    location ~ \.php$ {  
        # This is the webserver root for static junk
        root /var/www/public;
         ...
        # Point this somewhere else if the docroot is in a different location on the FPM server.
        fastcgi_param SCRIPT_FILENAME /home/php-fpm/wwwroot/$fastcgi_script_name;
    }
    

    【讨论】:

    • 谢谢,您的回答帮助了我。我从头开始,然后我能够正确设置nginx和fpm,它可以工作。
    猜你喜欢
    • 2015-07-06
    • 2021-04-08
    • 2017-04-30
    • 2021-02-23
    • 1970-01-01
    • 2021-05-18
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多