部署思路,编写docker-compose创建nginx,php,mysql三个容器,并把相应的路径映射到本地目录。
我的php框架为thinkphp6rc,前端为vue。我将tp6的入口文件改为home.php。访问 域名/home.php/xxxx/xxx是访问tp6,访问 域名/xxx/xxx则访问的是vue
ps:文章在Windows10下部署的,暂时未在linux上运行
Docker Compose是一个用来定义和运行复杂应用的Docker工具。使用Compose,你可以在一个文件中定义一个多容器应用,然后使用一条命令来启动你的应用,完成一切准备工作。
1.创建文件/文件夹
在电脑某个空目录下创建nginx,php,mysql,src文件夹和docker-compose.yml文件
说明:
- nginx文件夹:存放nginx相关配置文件,
- php文件夹:存放php的Dockerfile文件以及php.ini和php-fpm.conf
- mysql文件夹:存放mysql的数据文件和配置文件
- src文件夹:代码的部署目录
- docker-compose.yml文件:docker-compose的配置文件
详细目录
├── nginx
| └──nginx.conf #nginx配置文件
| └──conf.d:
| └──xxx.xxx.com.conf #自己的域名配置文件
├── php:
| └──Dockerfile #php的cockerFile 指定了镜像以及初始化的命令
| └── php.ini #php配置文件
| └── php-fpm.conf #php-fpm的文件
├── mysql:
| └──data #mysql的数据文件
| └──init
| └──init.sql #初始化文件
| └──my.conf#mysql的配置文件
├── src #代码文件
├── docker-compose.yml
-
2.编写docker-compose.yml
具体如下
version: '2' #docker-compose语法的版本
services:
nginx: #创建 nginx容器
image: hub.c.163.com/library/nginx:latest #这里我直接用的网易的镜像库
ports: #映射 80和443端口到本机
- 80:80
- 4433:443
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf #把容器的nginx.conf映射到 ./nginx/nginx.conf,
- ./nginx/conf.d:/etc/nginx/conf.d #把容器的conf.d映射到 ./nginx/conf.d
- ./src:/var/www #把容器的/var/www 映射到./src
php: #创建 php的容器
build: ./php #直接到 ./php文件下找Dockerfile
volumes:
- ./src:/var/www #把容器的/var/www 映射到./src
- ./php/php.ini:/usr/local/etc/php/php.ini #把容器的 php.ini映射到 ./php/php.ini
- ./php/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf #把容器的 php-fpm文件映射到 ./php/php-fpm.conf
mysql:#mysql的容器
image: hub.c.163.com/library/mysql:5.6 #直接用网易的镜像
volumes:
- ./mysql/data:/var/lib/mysql #把容器的数据文件夹映射到 ./mysql/data
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf #把容器的my.conf文件映射到 ./mysql/my.conf
- ./mysql/init:/docker-entrypoint-initdb.d/ #把初始化的文件夹映射到 ./mysql/init
ports:
- 3306:3306 #把容器的3306端口映射到本机的3306
environment:
- MYSQL_ROOT_PASSWORD=2LCqvSOJ6m0Ut6ui #root的喵喵
- MYSQL_USER=test#另一个帐号
- MYSQL_PASS=123#另一个帐号的密码
3.编写nginx相应文件
nginx.conf
这个基本固定
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 66M;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx\conf.d下的文件
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;
# 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/public;
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/public$fastcgi_script_name;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED /var/www/public$fastcgi_path_info;
proxy_cookie_path /speedacc /;
include fastcgi_params;
}
}
上面的文件更目录为/var/www/public(本机的 ./src/public),由于tp根目录为public所以才解析到public中
fastcgi_pass php:9000;这个反向代理到了php容器中,在docker中容器之间可以使用名字来代替ip
4.编写php的相应文件
Dockerfile
这个里面指定了php的镜像地址(还是网易的),安装了php常用的一些扩展
alpine是非常轻量级的Linux系统,只有5M的大小,Docker官方也推荐在alpine上做镜像。
FROM hub.c.163.com/library/php:7.1-fpm-alpine
RUN apk --update add \
autoconf \
build-base \
linux-headers \
libaio-dev \
zlib-dev \
curl \
git \
subversion \
freetype-dev \
libjpeg-turbo-dev \
libmcrypt-dev \
libpng-dev \
libtool \
libbz2 \
bzip2 \
bzip2-dev \
libstdc++ \
libxslt-dev \
openldap-dev \
imagemagick-dev \
make \
unzip \
wget && \
docker-php-ext-install bcmath mcrypt zip bz2 pdo_mysql mysqli simplexml opcache sockets mbstring pcntl xsl && \
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
pecl install imagick && \
docker-php-ext-enable imagick && \
pecl install swoole && \
docker-php-ext-enable swoole && \
docker-php-ext-install gd && \
docker-php-ext-enable opcache && \
apk del build-base \
linux-headers \
libaio-dev \
&& rm -rf /var/cache/apk/*
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /tmp
ENV COMPOSER_VERSION 1.5.1
RUN curl -s -f -L -o /tmp/installer.php https://raw.githubusercontent.com/composer/getcomposer.org/da290238de6d63faace0343efbdd5aa9354332c5/web/installer \
&& php -r " \
\$signature = '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410'; \
\$hash = hash('SHA384', file_get_contents('/tmp/installer.php')); \
if (!hash_equals(\$signature, \$hash)) { \
unlink('/tmp/installer.php'); \
echo 'Integrity check failed, installer is either corrupt or worse.' . PHP_EOL; \
exit(1); \
}" \
&& php /tmp/installer.php --no-ansi --install-dir=/usr/bin --filename=composer --version=${COMPOSER_VERSION} \
&& rm /tmp/installer.php \
&& composer --ansi --version --no-interaction
VOLUME /var/www
WORKDIR /var/www
CMD php-fpm
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
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
5.编写mysql相应文件
/init/init.sql
这个文件是初始化的文件,里面可以定义用户,权限,创建数据库等操作,
下面是一个栗子
#创建数据库
CREATE DATABASE test;
#给予test用户test数据库的权限
GRANT Alter, Alter Routine, Create, Create Routine, Create Temporary Tables, Create View, Delete, Drop, Event, Execute, Grant Option, Index, Insert, Lock Tables, References, Select, Show View, Trigger, Update ON `test`.* TO `test`@`%`;
#进入test数据库
use test;
#创建一个表
DROP TABLE IF EXISTS `qxwh_activity_event`;
CREATE TABLE `qxwh_activity_event` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NULL DEFAULT NULL
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
my.conf
[mysqld]
max_allowed_packet = 20M
innodb_buffer_pool_size=128M
query_cache_size=56M
key_buffer_size = 16M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=/var/lib/mysql/binlog
server-id=344
binlog_format=ROW
6.部署代码
将代码部署到./src下,注意访问根目录为public
当然也可以手动创建一个文件来测试
- 在 ./src下创建public文件夹并在public文件夹下创建home.php
home.php
<?php
phpinfo();
7.运行
在docker-compose.yml同级目录下打开运行窗口
运行
docker-compose up
等执行完毕后访问 http://127.0.0.1/home.php就可以看到php的环境输出
9.docker-compose的命令
- 开启所有服务:docker-compose up
- 关闭所有服务:docker-compose down
10.文件下载
打包了一份到码云上面,src目录下已经有了一个thinkphp6rc4的源码,public入口文件为home.php
地址
https://gitee.com/cq-vip/cocker-composenpm/tree/master
11.参考文章
https://segmentfault.com/a/1190000012834069
https://blog.csdn.net/hjxzb/article/details/84927567
网友评论