php - Symfony 3 与 Docker 和 nginx

标签 php symfony nginx docker

我正在尝试使用 Docker 配置 Symfony3。

我的项目文件夹中有两个文件和一个目录:

文件: - docker-compose.yml - 站点.conf

目录: - 代码

docker-compose.yml:

web:
    image: nginx:latest
    ports:
        - "80:80"
    volumes:
        - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
        - php
php:
    image: php:7-fpm
    volumes:
        - ./test_api:/test_api/web

站点.conf:

我从官方 nginx 站点 here 获取了这个文件 我的 server_name 在主机文件中声明。

server {
    server_name test-api.local;
    root /var/www/project/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
       # When you are using symlinks to link the document root to the
       # current version of your application, you should pass the real
       # application path instead of the path to the symlink to PHP
       # FPM.
       # Otherwise, PHP's OPcache may not properly detect changes to
       # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
       # for more information).
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       # Prevents URIs that include the front controller. This will 404:
       # http://domain.tld/app.php/some-path
       # Remove the internal directive to allow URIs like this
       internal;
   }

   # return 404 for all other php files not matching the front controller
   # this prevents access to other php files you don't want to be accessible.
   location ~ \.php$ {
     return 404;
   }

   error_log /var/log/nginx/project_error.log;
   access_log /var/log/nginx/project_access.log;
}

代码目录:

它包含全新的 Symfony 3.1 安装。

不,我首先做的是 docker-compose up 它,然后为我安装所有图像并且似乎可以运行。

然后我进入浏览器并在网址栏中输入:

http://test-api.local/

还有这个: http://test-api.local/app.php 和这个: http://test-api.local/app_dev.php

我收到此错误:

502 Bad Gateway

nginx/1.11.1

我的命令行中没有任何错误,什么也没有发生。谁能看出我的设置有什么问题吗...?

最佳答案

以下是您需要应用的更改,以使您的 Symfony 项目能够与 Docker Compose 配合使用。

docker-compose.yml中:

web:
    image: nginx:latest
    ports:
        - "80:80"
    volumes:
        - ./site.conf:/etc/nginx/conf.d/site.conf
    # Add the volumes from the PHP container, as Nginx needs access to your project files
    volumes_from:
        - php
    links:
        - php

php:
    image: php:7-fpm
    volumes:
        # I changed to the path that is specified in your site.conf file
        - ./test_api:/var/www/project

site.conf 中,更改以下行:

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_pass php:9000;
  • php 指的是 PHP 容器的名称(您在 docker-compose.yml 文件中定义的容器)。
  • 9000 是 PHP 容器为 PHP-FPM 公开的端口。

我测试了它,一切都按预期工作。

关于php - Symfony 3 与 Docker 和 nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126359/

相关文章:

php - Symfony DI : Circular service reference with Doctrine event subscriber

docker - Docker Nginx端口不可用

codeigniter - Nginx CodeIgniter 移除 URL 中的 index.php

nginx - 如何在使用 nginx 作为 Web 服务器的 Amazon Linux AMI 2018.03 上运行的 EC2 上安装 PHP 7.1?

PHP header 未按预期工作

php - PDO 提交重复记录?

php - PHP 中的日期代码显示 01/01/1970

php - CakePHP 允许使用 API 按字段搜索吗?

forms - Symfony2 中是否可以有仅显示字段类型?

php - 在 Twig 中使用命名空间别名