PHP files not working in a LEMP Docker Compose setup

I’m trying to build a simple LEMP setup. Nginx and MySQL work fine. But I can’t manage to add PHP. Here are all the relevant files :

docker-compose.yml

version: '3.7'

services:
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: lemp

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    restart: always
    ports:
      - 8080:80

  nginx:
    build: ./docker/nginx/
    depends_on:
      - db
    ports:
      - 8000:80
    volumes:
      # - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      # - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./log:/var/log/nginx
      - ./code:/usr/share/nginx/html

  php:
    image: phpdockerio/php73-fpm
    depends_on:
      - db

Dockerfile for Nginx

FROM nginx:latest

COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d

nginx.conf

http {
  include /etc/nginx/conf.d/*.conf;

  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  open_file_cache max=100;
}

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

user www-data;
worker_processes 4;
pid /run/nginx.pid;

default.conf

server {
  server_name localhost;
  root /usr/share/nginx/html;

  index index.php index.html;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

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

When I run a docker-compose up, everything is going fine. Can create whatever HTML file in the shared folder, it’s OK. But, if I create a PHP file and try to access it, I get a File not found..

Here is what I can see in the logs:

access.log

192.167.150.1 - - [16/Mar/2020:08:17:17 +0000] "GET /test.php HTTP/1.1" 404 47 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0"

error.log

2020/03/16 08:16:39 [error] 6#6: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.160.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8000"
2020/03/16 08:16:39 [error] 6#6: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.160.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.160.4:9000", host: "localhost:8000"
2020/03/16 08:17:17 [error] 6#6: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.160.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://192.168.160.4:9000", host: "localhost:8000"

I can see the error about the folder access being forbidden, but I can found a way to fix that (if that’s even really the issue.

Source: StackOverflow