前言

nginxphp-fpm放在一个镜像里面,这样更好配置,但是有一个问题,这样不利于php-fpmnginx两个服务动态扩容,因为二者的吞吐量可能不一样,同时扩容可能会造成资源浪费,但是分开,又会造成一个问题,那就是nginxphp-fpm如何通信的问题变得有些棘手。

以下是Dockerfile文件的内容,其余可以参考我的github地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
FROM alpine

LABEL MAINTAINER="<crazyjums@gmail.com>"

ADD nginx-1.18.0.tar.gz /tmp
COPY nginx/nginx.conf /tmp/nginx.conf
COPY start.sh /tmp/start.sh
COPY nginx/include/phptest.conf /tmp/phptest.conf
COPY nginx/include/default.conf /tmp/default.conf
COPY nginx/html/php_nginx/ /tmp/php_nginx/

## isntall nginx by source code
WORKDIR /tmp/nginx-1.18.0

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apk update && apk upgrade\
&& apk add --no-cache --virtual .build-deps \
mlocate \
g++ \
pcre-dev \
zlib-dev \
make\
gcc\
libc-dev\
autoconf \
&& apk add pcre \
libxml2-dev\
sqlite-dev \
&& addgroup -S nginx\
&& adduser -S -G nginx -s /sbin/nologin -h /usr/local/nginx nginx\
&& ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx\
&& make\
&& make install\
&& mkdir -p /data/nginx/logs /data/nginx/logs /usr/local/nginx/conf/include \
&& mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back\
&& mv /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf\
&& mv /tmp/start.sh /start.sh\
&& mv /tmp/phptest.conf /usr/local/nginx/conf/include/phptest.conf \
&& mv /tmp/default.conf /usr/local/nginx/conf/include/default.conf \
&& mv /tmp/php_nginx /usr/local/nginx/html/php_nginx \
&& ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ \
&& /usr/local/nginx/sbin/nginx -t

## install php(enable-fpm) by sourece code
ADD php-7.4.27.tar.gz /tmp
COPY php/php-fpm.conf /tmp/php-fpm.conf
COPY php/php-fpm.d/www.conf /tmp/www.conf
COPY php/php.ini /tmp/php.ini

WORKDIR /tmp/php-7.4.27

RUN ./configure --prefix=/usr/local/php --enable-fpm\
&& make \
&& make install \
&& mv /tmp/php-fpm.conf /usr/local/php/etc/php-fpm.conf \
&& mv /tmp/www.conf /usr/local/php/etc/php-fpm.d/www.conf \
&& mv /tmp/php.ini /usr/local/php/lib/php.ini \
&& ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/ \
&& rm -rf /tmp/* \
&& apk del .build-deps

EXPOSE 80 443

ENTRYPOINT ["/bin/sh","/start.sh"]

如果觉得麻烦,也可以直接从远程仓库直接拉取下来:

1
$ docker pull crazyjums/nginx-php-fpm-alpine

安装完镜像之后,还可以通过docker-compose进行管理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: "3"
services:
nginx-php-fpm:
# image: mmm
build:
context: .
dockerfile: Dockerfile
container_name: nginx-php-fpm-one
ports:
- 891:80
tty: true
volumes:
- ./nginx/include:/usr/local/nginx/conf/include
- ./nginx/html/php_nginx:/usr/local/nginx/html/php_nginx
- ./nginx/html/vue_nginx:/usr/local/nginx/html/vue_nginx
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./nginx/logs:/data/nginx/logs:rw
- ./php/php.ini:/usr/local/php/lib/php.ini
- ./php/php-fpm.conf:/usr/local/php/etc/php-fpm.conf
- ./php/php-fpm.d:/usr/local/php/etc/php-fpm.d
redis:
image: redis
container_name: redis
ports:
- 6380:6379
volumes:
- ./redis/data:/data
- ./redis/conf:/etc/redis/redis.conf:rw
privileged: true
mysql:
image: mysql:5.7
container_name: mysql
restart: always
ports:
- 3307:3306
volumes:
- ./mysql/conf.d:/etc/mysql/conf.d:rw #配置文件挂载到当前宿主机的/home/mysql/conf
- ./mysql/data:/var/lib/mysql:rw #数据挂载到当前宿主机的 /home/mysql/data
- ./mysql/my.cnf:/etc/my.cnf
environment:
- MYSQL_ROOT_PASSWORD=12345678

Reference

写在最后

欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。
微信公众号