2016-08-24 83 views
2

我正在嘗試使用Docker配置Symfony3。Symfony 3與Docker和nginx

我有兩個文件,並在我的項目文件夾目錄一個:

文件: - 泊塢窗 - compose.yml - site.conf

目錄: - 代碼

泊塢窗,撰寫.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 

site.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安裝。

沒有我第一次做的是碼頭 - 組成它然後安裝所有的圖像爲我,似乎運行。

我去,然後到瀏覽器,在我的網址欄中鍵入:

http://test-api.local/ 

這: http://test-api.local/app.php 這: http://test-api.local/app_dev.php

我得到這個錯誤:

502 Bad Gateway 

nginx/1.11.1 

我的命令行中沒有任何錯誤發生。任何人都可以看到我的設置有什麼問題......?

回答

0

嗯。看起來像你的nginx正在嘗試提供文件/連接到PHP容器中的套接字。但是容器將無法看到對方。

您可能需要在php容器中嵌入nginx,或通過卷共享容器之間的文件和/var/run/php5-fpm.sock插槽。

+0

in docker-compose.yml我有一個鏈接,雖然它鏈接了兩個容器....? – John

+0

是的 - 通過DNS!即您可以在Web容器內運行'ping php',但就是這樣!順便提一下,鏈接已經在docker-compose文件版本2中被棄用,以支持網絡 - 這使得它更清楚瞭解發生了什麼。您可能希望升級您的撰寫文件。 – Matthew

+0

嗯,好吧你有任何體面的閱讀這個..? – John

3

以下是您需要應用以使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的端口。

我測試了它,一切按預期工作。