准备工作


镜像

nginx:latest
php:7.2-fpm
mysql:5.7
redis:3.2

目录

/server/nginx # nginx配置文件目录
/server/php # 配置文件夹
/server/mysql # 配置文件夹
/server/redis # 数据文件夹
/server/compose # 镜像包文件夹
/server/compose/php # 镜像包文件夹
/server/compose/nginx # 镜像包文件夹
/server/compose/mysql # 镜像包文件夹
/server/compose/redis # 镜像包文件夹
/server/logs # 服务日志
/server/www/ # 项目

配置

docker-compose.yml

# /server/compose/docker-compose.yml
version: "2"
services:
  mydb:
    build: ./mysql
    container_name: "mydb"
    restart: always
    ports:
    - "3306:3306"
    volumes:
    - ~/server/mysql:/var/lib/mysql
    environment:
      - TZ=Asia/Shanghai  # 设置时区
      - MYSQL_ROOT_PASSWORD=123456
  myredis:
    build: ./redis
    container_name: "myredis"
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ~/server/redis:/data
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
  myphp:
    build: ./php
    container_name: "myphp"
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - ~/server/www:/var/www/html
      - ~/server/php:/usr/local/etc/php
    links:
      - "mydb"
      - "myredis"
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
  mynginx:
    build: ./nginx
    container_name: "mynginx"
    restart: always
    ports:
      - "80:80"
    volumes:
      - ~/server/www:/usr/share/nginx/html
      - ~/server/nginx:/etc/nginx
      - ~/server/logs:/var/log/nginx
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
    links:
      - "myphp"

Dockerfile

# /server/compose/php/Dockerfile
FROM php:7.2-fpm

RUN echo "deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \
        && echo "deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \
        && echo "deb http://mirrors.aliyun.com/debian-security stretch/updates main" >> /etc/apt/sources.list \
        && echo "deb-src http://mirrors.aliyun.com/debian-security stretch/updates main" >> /etc/apt/sources.list \
        && echo "deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \
        && echo "deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \
        && echo "deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \
        && echo "deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \
        && apt-get update -y \
        && apt-get upgrade \
        && apt-get install -y --allow-downgrades --no-install-recommends zlib1g zlib1g-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev zip unzip \
        && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
        && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-install zip \
        && docker-php-ext-install pdo_mysql \
        && docker-php-ext-install opcache \
        && docker-php-ext-install mysqli


RUN pecl install redis && docker-php-ext-enable redis \
    && pecl install xdebug && docker-php-ext-enable xdebug \
    && pecl install swoole && docker-php-ext-enable swoole

RUN echo "" >> /usr/local/etc/php/php.ini
RUN usermod -u 1000 www-data

# /server/compose/nginx/Dockerfile
FROM nginx

# /server/compose/mysql/Dockerfile
FROM mysql:5.7

# /server/compose/redis/Dockerfile
FROM redis:3.2

nginx配置放在/server/nginx下即可。

#user  nobody;
worker_processes 4;
events {
  worker_connections 40960;
}

http {
  include       mime.types;
  default_type  application/octet-stream;
  include vhost/*.conf;

  client_max_body_size 50m;
  client_body_buffer_size 60k;
  client_body_timeout 60;
  client_header_buffer_size 64k;
  client_header_timeout 60;

  keepalive_requests 100;
  large_client_header_buffers 4 64k;
  reset_timedout_connection on;
  send_timeout 60;
  sendfile_max_chunk 512k;
  server_names_hash_bucket_size 256;

  server {
     listen        80;
     server_name  localhost;
     root   "/usr/share/nginx/html";
     location / {
          index index.php index.html;
          if (!-e $request_filename) {
               rewrite  ^(.*)$  /index.php?s=/$1  last;
          }
          autoindex  off;
      }
      location ~ \.php(.*)$ {
          fastcgi_pass   myphp:9000;
          fastcgi_index  index.php;
          fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
          fastcgi_param  SCRIPT_FILENAME     /var/www/html$fastcgi_script_name;
          fastcgi_param  PATH_INFO  $fastcgi_path_info;
          fastcgi_param  PATH_TRANSLATED     /var/www/html$fastcgi_path_info;
          include        fastcgi_params;
      }
   }

}
worker_rlimit_nofile 100000;

php配置放在/server/php下即可。

注意

nginx在配置fpm转发时

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
# 不能使用$document_root需要使用php挂载的项目目录

需要ssl证书时

查看证书配置的位置
使用 php函数openssl_get_cert_locations(),也可以到php.ini文件中寻找到。
在ssh中输入如下命令php -r "print_r(openssl_get_cert_locations());"

证书地址

配置挂载该文件即可。

安装拓展

docker-php-ext-install 拓展名

详情可看↓

Docker php安装扩展步骤详解

最后修改:2020 年 09 月 23 日 01 : 37 PM
如果觉得我的文章对你有用,请随意赞赏