为php设置docker开发环境而不会出现“403拒绝访问”错误'

时间:2018-02-21 17:12:30

标签: php docker dockerfile

我开发了一个php应用程序,我希望能够以www-data运行我的应用程序而不会出现http错误403.

我有以下Dockerfile

FROM php:5.6-fpm-alpine
MAINTAINER Dimitrios Desyllas <pcmagas@disroot.org>

ARG XDEBUG_HOST="172.17.0.1"
ARG XDEBUG_PORT="9021"

RUN apk add --update --virtual build_deps gcc g++ autoconf make &&\
    apk add --update postgresql-dev libmcrypt-dev libxml2-dev &&\
    docker-php-source extract &&\
    docker-php-ext-install pdo mysqli pdo_mysql pgsql pdo_pgsql mbstring mcrypt xmlreader &&\
    pecl install xdebug-2.5.5 &&\
    docker-php-ext-enable xdebugpdo mysqli pdo_mysql pgsql pdo_pgsql mbstring mcrypt xmlreader &&\
    echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_autostart=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_log=/var/log/xdebug/xdebug_remote.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    mkdir -p /var/log/xdebug &&\
    touch  /var/log/xdebug/xdebug_remote.log &&\
    chmod 766 /var/log/xdebug/xdebug_remote.log &&\
    docker-php-source delete &&\
    apk del build_deps &&\
    rm -rf /var/cache/apk/* &&\
    rm -rf /tmp/*


ENTRYPOINT ["/bin/sh", "-l", "-c", "php-fpm"]

我使用它来为我的php应用程序提供统一的开发环境,而无需安装LEMP或LAMP堆栈。 (与流浪者的工作方式类似)

我使用以下docker-compose.yml

启动它
version: '2'
services:

 phpBB_dev:
  build:
    context: .
    dockerfile: Dockerfile
    args:
      #Over GNU/Linux run ifconfig and look for docker0 interface
      XDEBUG_HOST: 172.17.0.1
      # If phpstorm or your IDE listens to anoither port please change it
      XDEBUG_PORT: 9021
  image: 'pcmagas/phpbb_dev'
  links:
    - mariadb
    - postgresql
  volumes:
    - "$SRC_PATH:/var/www/html:Z"

 nginx:
  image: nginx:alpine
  ports:
    - "5092:5092"
  links:
    - "phpBB_dev"
  volumes:
    - './nginx.conf:/etc/nginx/nginx.conf:ro'
    - './logs/dev/nginx:/var/logs'
  volumes_from:
    - 'phpBB_dev'

 mariadb:
  image: mariadb
  environment:
    MYSQL_RANDOM_ROOT_PASSWORD: "yes"
    MYSQL_ONETIME_PASSWORD: "yes"
    MYSQL_DATABASE: 'phpBB'
    MYSQL_USER: 'phpbb'
    MYSQL_PASSWORD: 'phpp_unsafe_passwd'

 postgresql:
  image: postgres
  environment:
    POSTGRES_DB: 'phpbb'
    POSTGRES_USER: 'phpbb'
    POSTGRES_PASSWORD: 'phpp_unsafe_passwd'

我通过$SRC_PATH文件将我的项目路径安装到.env环境变量中。如上所述,我通过以下设置得到错误403.

1 个答案:

答案 0 :(得分:0)

您应该像这样修改Dockerfile

FROM php:5.6-fpm-alpine
MAINTAINER Dimitrios Desyllas <pcmagas@disroot.org>

ARG XDEBUG_HOST="172.17.0.1"
ARG XDEBUG_PORT="9021"
ARG UID='1000'
ARG GID='1000'

COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
COPY ./fpm.conf /usr/local/etc/php-fpm.d/zz-docker.conf

RUN chmod +x /usr/local/bin/entrypoint.sh &&\
    apk add --update --virtual build_deps gcc g++ autoconf make &&\
    apk add --update postgresql-dev libmcrypt-dev libxml2-dev &&\
    docker-php-source extract &&\
    docker-php-ext-install pdo mysqli pdo_mysql pgsql pdo_pgsql mbstring mcrypt xmlreader &&\
    pecl install xdebug-2.5.5 &&\
    docker-php-ext-enable xdebug pdo mysqli pdo_mysql pgsql pdo_pgsql mbstring mcrypt xmlreader &&\
    echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_autostart=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_log=/var/log/xdebug/xdebug_remote.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    echo "xdebug.remote_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
    mkdir -p /var/log/xdebug &&\
    touch  /var/log/xdebug/xdebug_remote.log &&\
    chmod 766 /var/log/xdebug/xdebug_remote.log &&\
    docker-php-source delete &&\
    apk del build_deps &&\
    rm -rf /var/cache/apk/* &&\
    rm -rf /tmp/* &&\
    adduser -D -H -S -s /bin/false -G www-data -u ${UID} developer

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]

如您所见,我对您的Dockerfile进行了以下更改:

  • 添加了拥有系统用户ID和群组ID的用户,因此无法解决任何权限问题,您可以随意修改文件
  • 我插入了一个entrypoint.sh脚本,因为任何* .php文件都应该具有可执行的linux权限,可以通过以下入口点来完成:

    #!/bin/sh
    
    find /var/www/html -iname "*.php" | xargs chmod +x
    
    exec "$@"
    

我也是为了工作,推荐以下docker-compose.yml

version: '2'
services:

 phpBB_dev:
  build:
    context: .
    dockerfile: Dockerfile
    args:
      #Over GNU/Linux run ifconfig and look for docker0 interface
      XDEBUG_HOST: 172.17.0.1
      # If phpstorm or your IDE listens to anoither port please change it
      XDEBUG_PORT: 9021
      # IN order to change theese parameters run whoami in your terminal
      UID: 1000
      GID: 1000
  image: 'pcmagas/phpbb_dev'
  links:
    - mariadb
    - postgresql
  volumes:
    - "$SRC_PATH:/var/www/html:Z"

 nginx:
  image: nginx:alpine
  ports:
    - "5092:5092"
  links:
    - "phpBB_dev"
  volumes:
    - './nginx.conf:/etc/nginx/nginx.conf:ro'
    - './logs/dev/nginx:/var/logs'
  volumes_from:
    - 'phpBB_dev'

 mariadb:
  image: mariadb
  environment:
    MYSQL_RANDOM_ROOT_PASSWORD: "yes"
    MYSQL_ONETIME_PASSWORD: "yes"
    MYSQL_DATABASE: 'phpBB'
    MYSQL_USER: 'phpbb'
    MYSQL_PASSWORD: 'phpp_unsafe_passwd'

 postgresql:
  image: postgres
  environment:
    POSTGRES_DB: 'phpbb'
    POSTGRES_USER: 'phpbb'
    POSTGRES_PASSWORD: 'phpp_unsafe_passwd'

正如您所见UID&amp;&amp; GID必须拥有当前用户的组ID和用户ID。 linux / unix命令whoami应该提供此信息。另外,为了工作,您应该创建名为fpm.conf

的以下文件
[global]
daemonize = no

[www]
listen = 9000
user = developer
group = www-data

换句话说,我扩展了zz-docker.conf使用的默认php:5.6-fpm-alpine文件,以便使用自定义用户和自定义组,其用户ID和组ID与主机相同一个用户,用于开发没有任何神秘的&#39;用户和所有者更改。

即使没有经过测试的php-7。*版本,我想它应该像魅力一样。