PHP文件无法在LEMP Docker Compose设置中运行

时间:2020-03-16 08:32:13

标签: php docker nginx docker-compose

我正在尝试构建一个简单的LEMP设置。 Nginx和MySQL可以正常工作。但是我无法添加PHP。以下是所有相关文件:

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

运行docker-compose up时,一切正常。可以在共享文件夹中创建任何HTML文件,没关系。但是,如果我创建一个PHP文件并尝试访问它,则会得到File not found.

这是我在日志中看到的内容:

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"

我可以看到有关禁止文件夹访问的错误,但是我可以找到一种解决方法(如果这确实是问题所在。

1 个答案:

答案 0 :(得分:0)

尝试nginx:depends_on:php
或类似的东西


  nginx:
    build: ./docker/nginx/
    depends_on:
      - php
    ports:
      - 8000:80
    volumes:
      - ./:/usr/share/nginx/html
      - ./log:/var/log/nginx
      - ./code:/usr/share/nginx/html

  php:
    image: phpdockerio/php73-fpm
    working_dir: /usr/share/nginx/html
    depends_on:
      - db
    ports:
      - "9000"