├── mysql ├── data │ └── ibtmp1 └── conf │ ├── my.cnf │ └── mysqld.cnf ├── phpfpm ├── conf │ ├── php.ini │ ├── php-fpm.conf │ └── www.conf └── Dockerfile ├── nginx ├── conf.d │ └── defualt.conf └── nginx.conf ├── docker-compose.yml └── README.md /mysql/data/ibtmp1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chichoyi/dnmp/HEAD/mysql/data/ibtmp1 -------------------------------------------------------------------------------- /phpfpm/conf/php.ini: -------------------------------------------------------------------------------- 1 | extension=grpc.so 2 | extension=protobuf.so 3 | extension=ffmpeg.so 4 | ;extension=php_sockets.dll 5 | 6 | upload_max_filesize = 100M 7 | post_max_size = 100M 8 | memory_limit = 100M 9 | max_execution_time = 600 10 | max_file_uploads 50 11 | 12 | max_input_vars = 2000 -------------------------------------------------------------------------------- /phpfpm/conf/php-fpm.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | ;pid = run/php-fpm.pid 3 | error_log = log/php-fpm.log 4 | 5 | ;syslog.facility = daemon 6 | ;syslog.ident = php-fpm 7 | ;log_level = notice 8 | ;emergency_restart_threshold = 0 9 | ;emergency_restart_interval = 0 10 | ;process_control_timeout = 0 11 | 12 | ;daemonize = yes 13 | ;rlimit_files = 1024 14 | ;rlimit_core = 0 15 | ;events.mechanism = epoll 16 | 17 | ;systemd_interval = 10 18 | include=etc/php-fpm.d/*.conf 19 | -------------------------------------------------------------------------------- /nginx/conf.d/defualt.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | 4 | server_name 127.0.0.1; 5 | 6 | root /www; # 此处是容器内部的路径,请保持一致 7 | index index.php index.html index.htm; 8 | charset utf-8; 9 | 10 | access_log /var/log/nginx/demo.access.log main; 11 | error_log /var/log/nginx/demo.error.log debug; 12 | 13 | location / { 14 | #try_files $uri $uri/ /index.php?_url=$uri&$args; 15 | try_files $uri $uri/ /index.php?$query_string; 16 | } 17 | 18 | location ~ \.php { 19 | # try_files $uri =404; 20 | 21 | fastcgi_pass PHP-FPM:9000; 22 | fastcgi_index /index.php; 23 | 24 | include fastcgi_params; 25 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 26 | fastcgi_param PATH_INFO $fastcgi_path_info; 27 | fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 28 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 29 | } 30 | 31 | location ~ /\.ht { 32 | deny all; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /nginx/nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes 1; 3 | 4 | error_log /var/log/nginx/error.log warn; 5 | pid /var/run/nginx.pid; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | http { 12 | include /etc/nginx/mime.types; 13 | default_type application/octet-stream; 14 | 15 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 16 | '$status $body_bytes_sent "$http_referer" ' 17 | '"$http_user_agent" "$http_x_forwarded_for"'; 18 | 19 | access_log /var/log/nginx/access.log main; 20 | 21 | sendfile on; 22 | #tcp_nopush on; 23 | 24 | keepalive_timeout 600; 25 | fastcgi_connect_timeout 600; 26 | fastcgi_send_timeout 600; 27 | fastcgi_read_timeout 600; 28 | 29 | gzip on; 30 | gzip_disable "MSIE [1-6].(?!.*SV1)"; 31 | 32 | #client_header_buffer_size 5m; 33 | client_max_body_size 100m; 34 | client_body_timeout 600s; 35 | client_header_timeout 120s; 36 | 37 | 38 | include /etc/nginx/conf.d/*.conf; 39 | } 40 | -------------------------------------------------------------------------------- /mysql/conf/my.cnf: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License, version 2.0, 5 | # as published by the Free Software Foundation. 6 | # 7 | # This program is also distributed with certain software (including 8 | # but not limited to OpenSSL) that is licensed under separate terms, 9 | # as designated in a particular file or component or in included license 10 | # documentation. The authors of MySQL hereby grant you an additional 11 | # permission to link the program and your derivative works with the 12 | # separately licensed software that they have included with MySQL. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License, version 2.0, for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program; if not, write to the Free Software 21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | 23 | !includedir /etc/mysql/conf.d/ 24 | !includedir /etc/mysql/mysql.conf.d/ -------------------------------------------------------------------------------- /phpfpm/conf/www.conf: -------------------------------------------------------------------------------- 1 | [www] 2 | ;prefix = /path/to/pools/$pool 3 | user = www-data 4 | group = www-data 5 | listen = 127.0.0.1:9000 6 | 7 | ;listen.backlog = 511 8 | ;listen.owner = www-data 9 | ;listen.group = www-data 10 | ;listen.mode = 0666 11 | ;listen.acl_users = 12 | ;listen.acl_groups = 13 | ;listen.allowed_clients = 127.0.0.1 14 | 15 | pm = dynamic 16 | pm.max_children = 512 17 | pm.start_servers = 16 18 | pm.min_spare_servers = 8 19 | pm.max_spare_servers = 30 20 | pm.max_requests = 1000 21 | 22 | ;pm.process_idle_timeout = 10s; 23 | ;pm.status_path = /status 24 | 25 | ;ping.path = /ping 26 | ;ping.response = pong 27 | ;access.log = log/$pool.access.log 28 | ;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" 29 | ;slowlog = log/$pool.log.slow 30 | ;request_slowlog_timeout = 0 31 | ;request_slowlog_trace_depth = 20 32 | request_terminate_timeout = 300 33 | 34 | rlimit_files = 1024 35 | rlimit_core = unlimited 36 | 37 | ;chroot = 38 | ;chdir = /var/www 39 | ;catch_workers_output = yes 40 | ;clear_env = no 41 | ;security.limit_extensions = .php .php3 .php4 .php5 .php7 42 | 43 | ;env[HOSTNAME] = $HOSTNAME 44 | ;env[PATH] = /usr/local/bin:/usr/bin:/bin 45 | ;env[TMP] = /tmp 46 | ;env[TMPDIR] = /tmp 47 | ;env[TEMP] = /tmp 48 | 49 | ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com 50 | ;php_flag[display_errors] = off 51 | ;php_admin_value[error_log] = /var/log/fpm-php.www.log 52 | ;php_admin_flag[log_errors] = on 53 | ;php_admin_value[memory_limit] = 32M 54 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | # lnmp 2 | version: '3' 3 | 4 | services: 5 | mysql: 6 | image: mysql:5.7 7 | container_name: mysql 8 | hostname: mysql 9 | ports: 10 | - "3306:3306" 11 | volumes: 12 | - ./mysql/data:/var/lib/mysql 13 | - ./mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf 14 | - ./mysql/conf/my.cnf:/etc/mysql/my.cnf 15 | - ./mysql/logs:/var/log/mysql 16 | environment: 17 | - MYSQL_ROOT_PASSWORD=root 18 | command: --default-authentication-plugin=mysql_native_password #支持远程访问 19 | networks: 20 | - lnmp 21 | 22 | redis: 23 | image: redis:4 24 | container_name: redis 25 | hostname: redis 26 | volumes: 27 | - ./redis/data:/data 28 | - ./redis/conf/redis.conf:/usr/local/etc/redis/redis.conf 29 | command: redis-server --requirepass a(&sabh928437 #设置密码 30 | ports: 31 | - "6379:6379" 32 | networks: 33 | - lnmp 34 | 35 | php: 36 | build: ./phpfpm 37 | container_name: php 38 | hostname: php 39 | ports: 40 | - "9000:9000" 41 | links: 42 | - mysql:mysql 43 | - redis:redis 44 | volumes: 45 | - /Users/chenyinshan/code:/www 46 | - ./phpfpm/conf/php.ini:/usr/local/etc/php/php.ini:ro 47 | - ./phpfpm/conf/php-fpm.conf:/usr/local/etc/php-fpm.conf 48 | - ./phpfpm/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf 49 | networks: 50 | - lnmp 51 | 52 | nginx: 53 | image: nginx:latest 54 | container_name: nginx 55 | hostname: nginx 56 | ports: 57 | - "80:80" 58 | volumes: 59 | - /Users/chenyinshan/code:/www 60 | - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro 61 | - ./nginx/conf.d/:/etc/nginx/conf.d/:ro 62 | links: 63 | - php:PHP-FPM 64 | networks: 65 | - lnmp 66 | 67 | networks: 68 | lnmp: 69 | driver: bridge 70 | 71 | 72 | -------------------------------------------------------------------------------- /mysql/conf/mysqld.cnf: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License, version 2.0, 5 | # as published by the Free Software Foundation. 6 | # 7 | # This program is also distributed with certain software (including 8 | # but not limited to OpenSSL) that is licensed under separate terms, 9 | # as designated in a particular file or component or in included license 10 | # documentation. The authors of MySQL hereby grant you an additional 11 | # permission to link the program and your derivative works with the 12 | # separately licensed software that they have included with MySQL. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License, version 2.0, for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program; if not, write to the Free Software 21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | 23 | # 24 | # The MySQL Server configuration file. 25 | # 26 | # For explanations see 27 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 28 | 29 | [mysqld] 30 | pid-file = /var/run/mysqld/mysqld.pid 31 | socket = /var/run/mysqld/mysqld.sock 32 | datadir = /var/lib/mysql 33 | #log-error = /var/log/mysql/error.log 34 | # By default we only accept connections from localhost 35 | #bind-address = 127.0.0.1 36 | # Disabling symbolic-links is recommended to prevent assorted security risks 37 | symbolic-links=0 38 | sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 39 | 40 | max_allowed_packet = 1024M 41 | 42 | # 主从配置 -------------------------------------------------------------------------------- /phpfpm/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh" 3 | # 4 | # PLEASE DO NOT EDIT IT DIRECTLY. 5 | # 6 | 7 | FROM php:7.2.2-fpm 8 | 9 | ENV PHPREDIS_VERSION 3.1.3 10 | 11 | #for redis and mysql 12 | RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \ 13 | && tar xfz /tmp/redis.tar.gz \ 14 | && rm -r /tmp/redis.tar.gz \ 15 | && mkdir -p /usr/src/php/ext \ 16 | && mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \ 17 | && docker-php-ext-install redis \ 18 | pdo pdo_mysql \ 19 | bcmath 20 | 21 | #for gd 22 | RUN apt-get update && apt-get install -y \ 23 | libfreetype6-dev \ 24 | libjpeg62-turbo-dev \ 25 | libpng-dev \ 26 | && docker-php-ext-install -j$(nproc) iconv \ 27 | && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ 28 | && docker-php-ext-install -j$(nproc) gd 29 | 30 | ## for grpc extention 31 | #RUN apt-get install -y \ 32 | # libmemcached-dev zlib1g-dev \ 33 | # && pecl install grpc \ 34 | # && pecl install protobuf \ 35 | # && rm -rf /usr/src/php 36 | 37 | # for zip 38 | RUN apt-get update \ 39 | && apt-get install -y --no-install-recommends libzip-dev \ 40 | && rm -r /var/lib/apt/lists/* \ 41 | && docker-php-ext-install -j$(nproc) zip 42 | 43 | #for ffmpeg -- aliuyun 没有ffpmeg 44 | #RUN echo "deb [check-valid-until=no] http://archive.debian.org/debian jessie-backports main" > /etc/apt/sources.list.d/jessie-backports.list 45 | #RUN sed -i '/deb http:\/\/deb.debian.org\/debian jessie-updates main/d' /etc/apt/sources.list 46 | #RUN apt-get -o Acquire::Check-Valid-Until=false update 47 | #RUN apt-get -y --force-yes install yasm ffmpeg 48 | 49 | 50 | ##for rabbitmq 51 | #RUN apt-get -qq update && apt-get -qq -y install \ 52 | # automake \ 53 | # cmake \ 54 | # g++ \ 55 | # git \ 56 | # libicu-dev \ 57 | # libmagickwand-dev \ 58 | # librabbitmq-dev \ 59 | # libreadline-dev \ 60 | # pkg-config \ 61 | # ssh-client \ 62 | # supervisor \ 63 | # && docker-php-ext-install \ 64 | # sockets \ 65 | # intl \ 66 | # && git clone git://github.com/alanxz/rabbitmq-c.git \ 67 | # && cd rabbitmq-c \ 68 | # && mkdir build && cd build \ 69 | # && cmake -DENABLE_SSL_SUPPORT=OFF .. \ 70 | # && cmake --build . --target install \ 71 | # && pecl install amqp imagick xdebug igbinary \ 72 | # && docker-php-ext-enable amqp imagick xdebug igbinary \ 73 | # && version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \ 74 | # && curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version \ 75 | # && mkdir -p /tmp/blackfire \ 76 | # && tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \ 77 | # && mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \ 78 | # && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \ 79 | # && curl -A "Docker" -L https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \ 80 | # && mv /tmp/blackfire/blackfire /usr/bin/blackfire \ 81 | # && rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz \ 82 | # && rm -rf /var/lib/apt/lists/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # why 2 | 3 | 什么都可以docker化,docker化的php环境开发、测试、部署等等 4 | 5 | ## 前言 6 | 使用前,默认你已经安装了 [docker](https://www.jianshu.com/search?q=docker%E5%AE%89%E8%A3%85&page=1&type=note) 和 [docker-compose](https://www.jianshu.com/p/f323aa0416da) 7 | 8 | ## 先使用阿里云镜像加速器 9 | 10 | [阿里云镜像加速器](https://dev.aliyun.com) 11 | 12 | 进入之后登录,然后进到 [ 管理中心 ],找到镜像加速器,里面有教你怎么配置本地的加速器,不再赘述 13 | 14 | ## 克隆此包 15 | 16 | git clone https://github.com/chichoyi/dnmp.git 17 | 18 | ### 修改docker-compose.yml 19 | 20 | - docker搭建lnmp环境,php 7.2 + nginx latest + mysql 5.7 + redis 4 + ... 21 | 22 | 参数说明 23 | 24 | nginx: 25 | image: nginx:latest 26 | container_name: nginx 27 | hostname: nginx 28 | ports: 29 | - "80:80" 30 | volumes: 31 | - /data/www:/www # /data/www 这个是宿主机的目录,可以修改指向你的代码文件夹,比如 /mnt/hgfs/www 32 | - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro # 这里是配置文件,不多,可以打开看看 33 | - ./nginx/conf.d/:/etc/nginx/conf.d/:ro 34 | links: 35 | - php:PHP-FPM # php容器的别名,nginx发送给php需要用到的,默认即可 36 | networks: 37 | - lnmp 38 | 39 | php: 40 | build: ./phpfpm # Dockerfile 文件地址 41 | container_name: php 42 | hostname: php 43 | ports: 44 | - "9000:9000" 45 | links: 46 | - mysql:mysql 47 | - redis:redis 48 | volumes: 49 | - /data/www:/www # /data/www 这个是宿主机的目录,可以修改指向你的代码文件夹,比如 /mnt/hgfs/www 50 | - ./phpfpm/conf/:/usr/local/etc/php/conf.d 51 | networks: 52 | - lnmp 53 | 54 | # 注意,nginx和php挂载出来的宿主机目录一定要一致(/data/www),不然没法访问php文件 55 | 56 | mysql: 57 | image: mysql:5.7 58 | container_name: mysql 59 | hostname: mysql 60 | ports: 61 | - "3306:3306" 62 | environment: 63 | - MYSQL_ROOT_PASSWORD=root #数据库的初始化密码,这里可以指定修改成你自己的 64 | networks: 65 | - lnmp 66 | 67 | redis: 68 | image: redis:4 69 | container_name: redis 70 | hostname: redis 71 | ports: 72 | - "6379:6379" 73 | networks: 74 | - lnmp 75 | 76 | networks: 77 | lnmp: 78 | driver: bridge 79 | 80 | 81 | ## 修改完之后执行命令 82 | 83 | cd dnmp 84 | 85 | docker-compose build 86 | 87 | # 修改代码目录 88 | vi docker-compose.yml 89 | # 把42行和54行的 /Users/chenyinshan/code 修改为你本机的代码目录 90 | 91 | vi ./nginx/conf.d/defualt.conf 修改你自己的虚拟域名和目录,容器的目录默认是/www, 92 | 93 | docker-compose up -d 94 | 95 | //如果没有目录的话请自己创建相关目录 96 | 97 | 第一次执行需要花点时间下载镜像 98 | 99 | php连接mysql需要在本机hosts文件添加域名映射,例如: 100 | 101 | 127.0.0.1 mysql 102 | 127.0.0.1 nginx 103 | 127.0.0.1 php 104 | 127.0.0.1 redis 105 | 106 | 这样就不用去查找mysql的容器的IP了 107 | 108 | # docker-compose up -d 的时候可能一些服务没有跑起来, 109 | # 比如mysql,可能你给的dnmp目录权限不够,要给充足权限 110 | 111 | sudo chmod -R 777 dnmp 112 | 113 | # 实践证明,有一些文件不可给最高权限,比如 114 | # ./mysql/conf/mysqld.cnf 建议给644即可,不然mysql容器会自动忽略,影响你的配置生效 115 | 116 | ## 如何composer安装php项目依赖包 117 | 118 | docker run --rm --interactive --tty \ 119 | --volume $PWD:/app \ 120 | composer install --ignore-platform-reqs --no-scripts 121 | 122 | - docker 会自动拉取composer镜像然后在当前目录执行composer命令 123 | - --ignore-platform-reqs --no-scripts 这条命令是忽略扩展要求 124 | 125 | ## docker php 如何安装扩展参考 126 | 127 | ### php的dockerfile讲解 128 | 129 | ~~~ 130 | FROM php:7.2.2-fpm 131 | 132 | ENV PHPREDIS_VERSION 3.1.3 133 | 134 | #for redis and mysql 135 | RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \ 136 | && tar xfz /tmp/redis.tar.gz \ 137 | && rm -r /tmp/redis.tar.gz \ 138 | && mkdir -p /usr/src/php/ext \ 139 | && mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \ 140 | && docker-php-ext-install redis \ 141 | pdo pdo_mysql \ 142 | bcmath 143 | 144 | #for gd 145 | RUN apt-get update && apt-get install -y \ 146 | libfreetype6-dev \ 147 | libjpeg62-turbo-dev \ 148 | libpng-dev \ 149 | && docker-php-ext-install -j$(nproc) iconv \ 150 | && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ 151 | && docker-php-ext-install -j$(nproc) gd 152 | 153 | # for grpc extention 154 | RUN apt-get install -y \ 155 | libmemcached-dev zlib1g-dev \ 156 | && pecl install grpc \ 157 | && pecl install protobuf \ 158 | && rm -rf /usr/src/php 159 | 160 | # for zip 161 | RUN apt-get update \ 162 | && apt-get install -y --no-install-recommends libzip-dev \ 163 | && rm -r /var/lib/apt/lists/* \ 164 | && docker-php-ext-install -j$(nproc) zip 165 | ~~~ 166 | 167 | - 定制化的php扩展,比如grpc扩展,你的项目不需要你可以注释掉,然后重新docker-compose build 168 | - 安装php扩展之前,优先去看[官方](https://hub.docker.com/_/php)是否有提供,比如这样的docker-php-ext-install 169 | - 如果docker php官方没有提供,那就需要自己去写dockerfile安装了,比如在./phpfpm/Dockerfile文件里面的rabbitmq,写出这些命令行的方法是,需要进去php容器,然后按照rabbitmq的安装方式一步一步去验证我的命令和依赖,最后才把命令总结出来写到dockerfile文件 170 | 171 | - [简书有人整理的安装扩展](https://www.jianshu.com/p/20fcca06e27e) 172 | 173 | ## 命令参考 174 | 175 | # 你自己修改了docker-compose文件或Dockerfile文件的话,请执行 176 | docker-compose build 177 | 178 | # 开启 dnmp 服务(-d 后台运行) 179 | docker-compose up -d 180 | 181 | # 重启 dnmp 服务 182 | docker-compose restart 183 | 184 | # 关闭 dnmp 服务 185 | docker-compose down 186 | 187 | # 如果修改或增加了 nginx 服务配置 188 | docker restart nginx 189 | 190 | ## tip 191 | 192 | 使用的都是官方的镜像,如有疑问,欢迎提出指正, 193 | 194 | ## contact me 195 | 196 | email: chichoyi@163.com 197 | 198 | --------------------------------------------------------------------------------