如何正确地制作自包含的nginx和php-fpm容器

时间:2017-10-09 21:19:49

标签: php docker nginx fpm

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

我想让它们自成一体(容器将克隆来自构建的git的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;
    }
}

1 个答案:

答案 0 :(得分:3)

您应该只在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;
}
相关问题