【问题标题】:Where i put my index.php when using docker nginx image使用 docker nginx 映像时我将 index.php 放在哪里
【发布时间】:2020-07-06 16:33:33
【问题描述】:

我找不到我将文件 index.php 放在哪里,以便 ngnix 可以解释我的文件。 所有容器都在工作,当我把localhost:8080 nginx 工作 这是我的 docker-compose.yml

web:
  image: nginx
  volumes:
   - ./templates:/etc/nginx/templates
  ports:
   - "8080:8080"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=8080

php:
       image: php:7.0-fpm
       expose:
           - 9000
       volumes_from:
           - app
       links:
           - elastic

app:
       image: php:7.0-fpm
       volumes:
           - .:/src

elastic:
       image: elasticsearch:2.3
       volumes:
         - ./elasticsearch/data:/usr/share/elasticsearch/data
         - ./elasticsearch/logs:/usr/share/elasticsearch/logs
       expose:
         - "9200"
       ports:
         - "9200:9200"

谁能帮帮我

【问题讨论】:

    标签: php docker nginx docker-compose


    【解决方案1】:

    您需要为 PHP 和 Nginx docker 映像安装相同的卷。

    version: '3'
    services:
      nginx:
        image: nginx:alpine
        volumes:
          - ./app:/app
          - ./nginx-config/:/etc/nginx/conf.d/
        ports:
          - 80:80
        depends_on:
          - php
      php:
        image: php:7.3-fpm-alpine
        volumes:
         - ./app:/app
    
    

    在上面的compose文件中,代码放在主机的app文件夹下。

    ├── app
    │   ├── helloworld.php
    │   └── index.php
    ├── docker-compose.yml
    └── nginx-config
        └── default.conf
    

    你的 Nginx 配置应该使用 docker 服务网络来连接 php-fpm 容器。

    server {
        index index.php index.html;
        server_name php-docker.local;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /app/;
    
        location ~ \.php$ {
            try_files $uri =404;
            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;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    

    或者你可以试试 Github 上的工作示例。

    git clone https://github.com/Adiii717/dockerize-nginx-php.git
    cd dockerize-nginx-php;
    docker-compose up
    

    现在打开浏览器

    http://localhost/helloworld.php

    【讨论】:

    • 感谢您的回复。好吧,我从 github 克隆了项目,然后我用 composer.json 安装了 ElasticSearch -> 生成了供应商,但是当我尝试执行“require '../vendor/autoload.php';”时我收到一条错误消息,说我找不到这个文件“无法打开流:/app/index.php 第 4 行中没有这样的文件或目录”
    • 顺便说一句,最好先检查 helloworld 是否有效,然后问题是否不同?
    • Helloworld 和 index.php 运行良好。即使我将文件放在 /app 下,也会出现问题
    • 似乎要求无效require '../vendor/autoload.php,供应商文件夹在哪里?
    • 第一次,我将 vendor 安装在 /app 的父文件夹下。我把你说的那样。然后我在/app下安装了composer.json然后我改成require 'vendor/autoload.php'这通常是同样的问题Fatal error: require(): Failed opening required '/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /app/test.php on line 5
    猜你喜欢
    • 2020-07-28
    • 2016-03-08
    • 2021-10-23
    • 2017-11-30
    • 2017-07-04
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多