Docker:宿主机Nginx访问docker中的php

使用宿主机nginx搭配docker多版本php。

docker 镜像获取

docker pull php:7.1-fpm

创建容器

docker run -d --name php -p 9056:9000 -v /srv/http:/var/www/html --privileged=true php:7.1-fpm
#参数说明:

-name php       容器名称为 `php`
-p 9056:9000        宿主机的9056端口映射到容器的9000端口
-v /srv/http:/var/www/html              宿主机的 `/srv/http` 目录挂载到容器的 `/var/www/html` 目录下

宿主机Nginx 配置

server {
    listen      10064;
    set         $base /var/www/html/member-benefits;
    root        $base/backend/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    index index.html index.htm index.php;
    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9056;
        fastcgi_index index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/member-benefits/backend/public$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  REQUEST_URI  $fastcgi_script_name$fastcgi_path_info;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

容器配置以及扩展安装

容器主要需要安装一些扩展和一些项目需要的东西

扩展安装

进入容器

docker exec -it php /bin/bash
  • gd
#依赖
apt-get install -y build-essential libssl-dev zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev
#指定目录
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
#安装gd扩展
docker-php-ext-install gd
  • mysqli pdo-mysql zip …
docker-php-ext-install pdo pdo_mysql mysqli