首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP设置Docker问题中的LEMP

用PHP设置Docker问题中的LEMP
EN

Stack Overflow用户
提问于 2018-07-22 12:24:57
回答 1查看 496关注 0票数 0

一直试图在本地设置我自己的LEMP堆栈,nginx和php似乎都很好地单独工作,但是在nginx中集成php的尝试失败了…!获取误差

403禁止

nginx错误日志:

2018/07/22 12:06:48错误9#9:*1目录索引"/usr/share/nginx/html/“被禁止,客户机: 172.19.0.4,服务器: localhost,请求:"GET / HTTP/1.1",主机:"laradock.localhost:8000” 172.19.0.4 -22/711/2018:12:06:48 +0000 "GET / HTTP/1.1“403 571 - "Mozilla/5.0 (X11;Linux x86_64) AppleWebKit/537.36 (KHTML,类似壁虎) Chrome/67.0.3396.99 Safari/537.36”"172.19.0.1“

一直在使用下面的docker映像版本:

代码语言:javascript
复制
PHP_TAG=7.1-fpm-alpine
NGINX_TAG=alpine

我的docker-compose.yml文件

代码语言:javascript
复制
version: "2"

services:

  php:
    image: php:$PHP_TAG
    # restart: always
    container_name: "${PROJECT_NAME}_php"
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      DB_HOST: $DB_HOST
      DB_USER: $DB_USER
      DB_PASSWORD: $DB_PASSWORD
      DB_NAME: $DB_NAME
      DB_DRIVER: $DB_DRIVER
    volumes:
      - /var/www/ro_www/src/sample_site/:/usr/share/nginx/html

  nginx:
    image: nginx:$NGINX_TAG
    container_name: "${PROJECT_NAME}_nginx"
    depends_on:
      - php
      # - mysqld
    environment:
      NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/
#      NGINX_DRUPAL_FILE_PROXY_URL: http://example.com
    volumes:
      - /var/www/ro_www/src/sample_site/:/usr/share/nginx/html
      - ./config/site.conf:/etc/nginx/conf.d/site.conf:ro
      # - ./etc/ssl:/etc/ssl
    # ports:
      # - "8000:80"
    #   - "3000:443"
    labels:
      - 'traefik.backend=nginx'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'

  mailhog:
    image: mailhog/mailhog
    container_name: "${PROJECT_NAME}_mailhog"
    labels:
      - 'traefik.backend=mailhog'
      - 'traefik.port=8025'
      - 'traefik.frontend.rule=Host:mailhog.${PROJECT_BASE_URL}'

  adminer:
    container_name: "${PROJECT_NAME}_adminer"
    image: wodby/adminer:$ADMINER_TAG
    environment:
      ADMINER_SALT: adminer-salt
    volumes:
      - /var/www/ro_www/src/sample_site/adminer/:/usr/share/nginx/html
    labels:
      - 'traefik.backend=adminer'
      - 'traefik.port=9000'
      - 'traefik.frontend.rule=Host:adminer.${PROJECT_BASE_URL}'

  portainer:
    image: portainer/portainer
    container_name: "${PROJECT_NAME}_portainer"
    command: --no-auth -H unix:///var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - 'traefik.backend=portainer'
      - 'traefik.port=9000'
      - 'traefik.frontend.rule=Host:portainer.${PROJECT_BASE_URL}'

  traefik:
    image: traefik
    container_name: "${PROJECT_NAME}_traefik"
    command: -c /dev/null --web --docker --logLevel=INFO
    ports:
      - '8000:80'
      - '8080:8080' # Dashboard
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

nginx site.conf文件如下:

代码语言:javascript
复制
    server {
    listen       80;
    server_name  nginx;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
#    error_page   500 502 503 504  /50x.html;
#    location = /50x.html {
#        root   /usr/share/nginx/html;
#    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           html;
        #fastcgi_pass   php:9000;
         fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

(预先多谢你的帮助!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 06:11:08

由于我没有得到多少反馈,经过多次尝试和错误,我得到了我的解决方案!因此,我张贴这个,以防有人可能有类似的问题。

这里的问题似乎是nginx site.conf文件。

代码语言:javascript
复制
server {
    listen      80;
    listen [::]:80;

    index index.php index.html;
    server_name laradock.localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /usr/share/nginx/html;


    location / {
        autoindex on; #to list file in the directory if the index file is missing
    }

    #### this was where i was facing the issue, the php block
    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;
    }
}

另外要注意的是,nginx可以有多个.conf配置文件,并且它支持.htaccess文件,比如apache ( htaccess逻辑需要重写到nginx .d目录这里中的.conf文件)。

我的解决方案的完整代码可以在这个git存储库中找到。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51465121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档