Symfony 3 avec Docker et nginx

J’essaie de configurer Symfony3 avec Docker.

J’ai deux fichiers et un répertoire dans mon dossier de projet:

fichiers: – docker-compose.yml – site.conf

répertoires: – code

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 

site.conf:

J’ai obtenu ce fichier depuis le site officiel nginx ici mon nom de serveur est déclaré dans le fichier hosts.

 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; } 

répertoire de code:

il contient une nouvelle installation de Symfony 3.1.

Non, ce que je fais en premier est docker-compose, puis installe toutes les images pour moi et semble fonctionner.

Je vais ensuite au navigateur et tapez ceci dans ma barre d’URL:

 http://test-api.local/ 

et ceci: http: //test-api.local/app.php et ceci: http: //test-api.local/app_dev.php

Je reçois cette erreur:

 502 Bad Gateway nginx/1.11.1 

Je n’ai aucune erreur dans ma ligne de commande, rien ne s’est passé. Est-ce que quelqu’un peut voir quelque chose de mal avec ma mise en place …?

Voici les modifications à apporter pour que votre projet Symfony fonctionne avec Docker Compose.

Dans 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 

Dans site.conf , modifiez ces lignes:

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

à

 fastcgi_pass php:9000; 
  • php fait référence au nom de votre conteneur PHP (celui que vous avez défini dans le fichier docker-compose.yml ).
  • 9000 est le port que le conteneur PHP expose pour PHP-FPM.

Je l’ai testé et tout fonctionne comme prévu.

Hmm. On dirait que votre nginx essaie de servir des fichiers / se connecter aux sockets dans le conteneur php. Cependant, les conteneurs ne pourront pas se voir.

Vous devez soit intégrer nginx dans le conteneur php, soit partager les fichiers et /var/run/php5-fpm.sock entre les conteneurs via des volumes.