John
John

Reputation: 1655

Symfony 3 with Docker and nginx

I am trying to config Symfony3 with Docker.

I have two files and one directories in my project folder:

files: - docker-compose.yml - site.conf

directories: - 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:

I got this file from official nginx site here my server_name is declared in hosts file.

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

code directory:

it contains fresh Symfony 3.1 installation.

No what i do first is docker-compose up it then installs all the images for me and seems to run.

I go then to the browser and type this in my url bar:

http://test-api.local/

and this: http://test-api.local/app.php and this: http://test-api.local/app_dev.php

I get this error:

502 Bad Gateway

nginx/1.11.1

I get no erros in my comand line nothing has happened. Can anyone see anything wrong with my set up...?

Upvotes: 2

Views: 3446

Answers (2)

Michaël Perrin
Michaël Perrin

Reputation: 6238

Here are the changes you need to apply to make your Symfony project work with Docker Compose.

In 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

In site.conf, change these lines:

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

to

fastcgi_pass php:9000;
  • php refers to the name of your PHP container (the one you defined in the docker-compose.yml file).
  • 9000 is port the PHP container exposes for PHP-FPM.

I tested it and everything works as expected.

Upvotes: 4

Matthew
Matthew

Reputation: 11337

Hmm. Looks like your nginx is trying to serve files / connect to sockets in the php container. However the containers won't be able to see each other.

You either need to embed nginx within the php container, or share the files and /var/run/php5-fpm.sock socket between the containers via volumes.

Upvotes: 0

Related Questions