Search code examples
phpnginxdockersymfonyfpm

PHP file not found that should be found according to configured vHost


I'm trying to setup a Symfony 3.x application with Docker. I configured 3 docker containers through a docker-compose.yml file:

  • Nginx
  • Php-fpm
  • MySQL

When I navigate to my-project.dev:8080/, I see a simple 404-Not found-page.

  • I can't load my-project.dev:8080/app_dev.php or my-project.dev:8080/config.php (I get a "file not found" error)

I don't see any entries in the /var/log/nginx/access.log either.

docker-compose.yml:

web:
  image: nginx:latest
  ports:
    - "8080:80"
  volumes:
    - .:/var/www
    - ./docker/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
    - ./docker/nginx.conf:/etc/nginx/nginx.conf
  links:
    - php

php:
  image: php:5.6-fpm
  volumes:
    - .:/var/www
  links:
    - db

db:
  image: mysql:latest
  volumes:
    - /var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=my-password

nginx.conf file:

user  www-data;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

vhost.conf file:

server {
    listen *:80;

    server_name my-project.dev;
    root /var/www/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 php:9000;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;

       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ \.php$ {
       return 404;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

Solution

  • I had to override the WORKDIR of the php-fpm image by creating an image like this:

    Dockerfile:

    FROM php:5.6-fpm
    MAINTAINER Firstname Lastname <firstname.lastname@domain.com>
    WORKDIR /var/www
    

    Build image:

    docker build -t companyx/php-5.6-fpm .
    

    Update docker-compose file:

    web:
      image: nginx:latest
      ports:
        - "8080:80"
      volumes:
        - .:/var/www
        - ./docker/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
        - ./docker/nginx.conf:/etc/nginx/nginx.conf
      links:
        - php
    
    php:
      image: companyx/php-5.6-fpm
      volumes:
        - .:/var/www
      links:
        - db
    
    db:
      image: mysql:latest
      volumes:
        - /var/lib/mysql
      environment:
        - MYSQL_ROOT_PASSWORD=my-password