一、简介
常见网页的展示方式一般采用经典LNMP架构,如果一个个安装部署和配置应用服务器和数据库的话非常花费时间,在此通过Docker容器的方式,加上常用的单机编排工具docker-compose来实现此功能。【亲测无BUG】
实现功能:
1.运行docker-compose.yml直接通过配置清单文件,创建并配置对应应用;
2.根据需求,自动挂载宿主机应用程序代码,并实现自动导入数据库到mysql功能。
二、安装docker和docker-compose
1.安装docker
参考琼杰笔记:Docker安装部署方法总结【二】
2.安装docker-compose
参考琼杰笔记:Docker-Compose安装部署总结
三、自动创建和配置应用
1.创建相应目录和文件
[root@master01 ~]# mkdir -pv /opt/docker-compose/{conf,mysql/{data,backup},www/wordpress}
[root@master01 ~]# touch /opt/docker-compose/conf/{default.conf,php-fpm.conf,php.ini,redis.conf} && touch /opt/docker-compose/{Dockerfile,docker-compose.yml}
目录结构如下:
.
├── docker-compose
│ ├── conf
│ │ ├── default.conf
│ │ ├── php-fpm.conf
│ │ ├── php.ini
│ │ └── redis.conf
│ ├── mysql
│ │ └── data
│ └── www
│ └── wordpress
├── docker-compose.yml
└── Dockerfile
6 directories, 6 files
2.创建对应的配置清单文件
1.Docker文件
cat <<EOF > /opt/docker-compose/Dockerfile
FROM php:7.2.4-fpm
MAINTAINER "zhangqiongjie <zhangqiongjie@zhangqiongjie.com>"
# docker中php扩展安装方式如下:
# 1、PHP源码文件目录自带扩展docker-php-ext-install直接安装
# 2、pecl扩展 因为一些扩展不包含在PHP源码文件中,但PHP扩展库仓库中存在。因此这里用 pecl install 安装扩展,再用docker-php-ext-enable 命令启用扩展
# 3、其他一些既不在PHP源码包,也不再PECL扩展仓库中的扩展,可以通过下载扩展程序源码,编译安装的方式安装(如下清单文件所示)
# 扩展版本号定义
#redis 扩展
ENV PHPREDIS_VERSION 4.0.0
#msgpack扩展
ENV MSGPACK_VERSION 2.0.3
#memcached扩展
ENV MEMCACHED_VERSION 3.1.3
#mongodb扩展
ENV MONGODB_VERSION 1.5.3
#xhprof扩展 https://github.com/longxinH/xhprof/releases(pecl 不支持php7 使用这里的)
ENV XHPROF_VERSION 2.0.5
#swoole安装 如果以后用到的话,不用再安装了,4.0之后性能更好
ENV SWOOLE_VERSION 4.0.3
#swoole依赖hiredis
ENV HIREDIS_VERSION 0.13.3
# 设置时间
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' > /etc/timezone
# 扩展依赖
RUN apt-get update \
&& apt-get install -y \
curl \
wget \
git \
zip \
libz-dev \
libssl-dev \
libnghttp2-dev \
libpcre3-dev \
libmemcached-dev \
zlib1g-dev \
&& apt-get clean \
&& apt-get autoremove
# Composer安装
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer \
&& composer self-update --clean-backups
# Mysqli 扩展 自带 直接安装即可(当前数据库使用的mysqli查询的)
RUN docker-php-ext-install mysqli
# PDO 扩展 自带 直接安装即可
RUN docker-php-ext-install pdo_mysql
# Bcmath 扩展 自带 直接安装即可
RUN docker-php-ext-install bcmath
# Redis 扩展下载 pecl本地安装 开启扩展
RUN wget https://pecl.php.net/get/redis-\${PHPREDIS_VERSION}.tgz -O /tmp/redis.tgz \
&& pecl install /tmp/redis.tgz \
&& rm -rf /tmp/redis.tgz \
&& docker-php-ext-enable redis
# msgpack 扩展下载 pecl本地安装 开启扩展(延迟队列使用减少源数据占用空间)
RUN wget http://pecl.php.net/get/msgpack-\${MSGPACK_VERSION}.tgz -O /tmp/msgpack.tgz \
&& pecl install /tmp/msgpack.tgz \
&& rm -rf /tmp/msgpack.tgz \
&& docker-php-ext-enable msgpack
# memcached 扩展下载 pecl本地安装 开启扩展 前面已经通过 apt-get安装了libmemcached-dev依赖
RUN wget http://pecl.php.net/get/memcached-\${MEMCACHED_VERSION}.tgz -O /tmp/memcached.tgz \
&& pecl install /tmp/memcached.tgz \
&& rm -rf /tmp/memcached.tgz \
&& docker-php-ext-enable memcached
# mongodb 扩展下载 pecl本地安装 开启扩展 前面已经通过
RUN wget http://pecl.php.net/get/mongodb-\${MONGODB_VERSION}.tgz -O /tmp/mongodb.tgz \
&& pecl install /tmp/mongodb.tgz \
&& rm -rf /tmp/mongodb.tgz \
&& docker-php-ext-enable mongodb
# xhprof github上下载支持php7的扩展 安装 开启扩展
RUN wget https://github.com/longxinH/xhprof/archive/v\${XHPROF_VERSION}.tar.gz -O /tmp/xhprof.tar.gz \
&& mkdir -p /tmp/xhprof \
&& tar -xf /tmp/xhprof.tar.gz -C /tmp/xhprof --strip-components=1 \
&& rm /tmp/xhprof.tar.gz \
&& ( \
cd /tmp/xhprof/extension \
&& phpize \
&& ./configure \
&& make -j\$(nproc) \
&& make install \
) \
&& rm -r /tmp/xhprof \
&& docker-php-ext-enable xhprof
# Hiredis依赖安装
RUN wget https://github.com/redis/hiredis/archive/v\${HIREDIS_VERSION}.tar.gz -O /tmp/hiredis.tar.gz \
&& mkdir -p /tmp/hiredis \
&& tar -xf /tmp/hiredis.tar.gz -C /tmp/hiredis --strip-components=1 \
&& rm /tmp/hiredis.tar.gz \
&& ( \
cd /tmp/hiredis \
&& make -j\$(nproc) \
&& make install \
&& ldconfig \
) \
&& rm -r /tmp/hiredis
# Swoole 扩展安装 开启扩展
RUN wget https://github.com/swoole/swoole-src/archive/v\${SWOOLE_VERSION}.tar.gz -O /tmp/swoole.tar.gz \
&& mkdir -p /tmp/swoole \
&& tar -xf /tmp/swoole.tar.gz -C /tmp/swoole --strip-components=1 \
&& rm /tmp/swoole.tar.gz \
&& ( \
cd /tmp/swoole \
&& phpize \
&& ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
&& make -j\$(nproc) \
&& make install \
) \
&& rm -r /tmp/swoole \
&& docker-php-ext-enable swoole
EOF
2.docker-compose.yml文件
cat <<EOF >/opt/docker-compose/docker-compose.yml
---
version: "3"
services:
nginx:
image: nginx:1.15.7-alpine
container_name: nginx15
ports:
- "80:80"
volumes:
- ./www/:/var/www/html:rw
- ./conf/default.conf:/etc/nginx/conf.d/default.conf:rw
environment:
TZ: "Asia/Shanghai"
restart: always
networks:
- default
php:
build: .
container_name: php72
ports:
- "9000:9000"
volumes:
- ./www/:/var/www/html:rw
- ./conf/php.ini:/usr/local/etc/php/php.ini:rw
- ./conf/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
restart: always
cap_add:
- SYS_PTRACE
networks:
- default
mysql:
#image: mysql:8.0
image: mariadb:10.5.8
container_name: mysql57
ports:
- "3306:3306"
volumes:
- ./mysql/data:/var/lib/mysql/:rw
- ./mysql/backup:/docker-entrypoint-initdb.d:rw
restart: always
networks:
- default
environment:
MYSQL_ROOT_PASSWORD: "12345678"
TZ: "Asia/Shanghai"
redis:
image: redis:5.0.3
container_name: redis50
ports:
- "6379:6379"
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf:ro
restart: always
entrypoint: ["redis-server", "/usr/local/etc/redis/redis.conf"]
environment:
TZ: "Asia/Shanghai"
networks:
- default
networks:
default:
EOF
3.default.conf
cat <<EOF > /opt/docker-compose/conf/default.conf
server {
listen 80;
server_name localhost;
index index.php index.html index.htm;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#access_log /dev/null;
access_log /var/www/html/nginx.access.log main;
error_log /var/www/html/nginx.error.log warn;
# 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 / {
root /var/www/html;
index index.html index.htm index.php;
try_files \$uri \$uri/ /index.html;
autoindex off;
}
location ~ \.php(\$|/){
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME /var/www/html/\$fastcgi_script_name;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)\$;
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_param PATH_TRANSLATED /var/www/html/\$fastcgi_path_info;
proxy_cookie_path /speedacc /;
include fastcgi_params;
}
}
EOF
4.php-fpm.conf
cat <<EOF > /opt/docker-compose/conf/php-fpm.conf
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 50
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 7
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 5
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 9
pm.max_requests = 5000
pm.status_path = /fpm-status.php
EOF
5.php.ini
cat <<EOF > /opt/docker-compose/conf/php.ini
date.timezone = "Asia/Shanghai"
display_errors = On
error_reporting = E_ALL
short_open_tag=Off
upload_max_filesize = 50M
post_max_size = 50M
memory_limit=512M
EOF
6..执行docker-compose
[root@master01 ~]# docker-compose -f /opt/docker-compose/docker-compose.yml up -d
执行成功可以看到如下内容:
[root@master01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5c8f8bdad8d dockercompose_php "docker-php-entrypoi…" 41 minutes ago Up 3 minutes 0.0.0.0:9000->9000/tcp php72
cfdd8a8210cd redis:5.0.3 "redis-server /usr/l…" 55 minutes ago Up 3 minutes 0.0.0.0:6379->6379/tcp redis50
ecd02416ca19 nginx:1.15.7-alpine "nginx -g 'daemon of…" 55 minutes ago Up 3 minutes 0.0.0.0:5000->80/tcp nginx15
da0b6e232aba mysql:5.7 "docker-entrypoint.s…" 55 minutes ago Up 3 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp mysql57
三、程序代码挂载并导入数据到mysql数据库
1.应用程序
将应用程序放置于目录/opt/docker-compose/www下即可
2.mysql数据库
将备份数据库放置/opt/docker-compose/mysql/backup目录下即可完成自动导入到mysql容器数据库中
3.重新创建应用
[root@master01 ~]# docker-compose -f /opt/docker-compose/docker-compose.yml down
[root@master01 ~]# docker-compose -f /opt/docker-compose/docker-compose.yml up -d
此时即可在浏览器打开宿主机的地址访问web网页,如下:
至此所实现的docker-compose自动创建容器应用和自动导入数据库并挂载应用目录功能已实现。
重要提醒:
若网站连接数据报错,按如下文章操作即可:
内容查看价格3.99元立即支付
注意:本站少数资源收集于网络,如涉及版权等问题请及时与站长联系,我们会在第一时间内与您协商解决。如非特殊说明,本站所有资源解压密码均为:zhangqiongjie.com。
作者:1923002089
评论前必须登录!
注册