用Docker部署一个PHP环境

用Docker部署一个PHP环境

我喜欢Docker,它那容器服务的方式使我部署生产环境方便了好几倍,仅需要在本机上做好Dockerfile等配置文件,在服务器上执行就能得到统一的环境;当容器服务出错挂掉的时候也有重启服务,极大解决了环境部署的工作。在近日开始做PHP的工程,虽然有LAMP/LNAMP等环境给我选择,作为一个Docker死忠粉,决定用Docker部署一个PHP的生产环境。

准备工作

决定好需要使用的容器,在本文选择了mysql:5.7phpmyadmin:phpmyadminphp:7.3-fpmnginx:latest这4个镜像。

php:7.3-fpm 用php-fpm配合Nginx的fastcgi使用。
phpmyadmin:phpmyadmin 管理mysql用的工具。
nginx:latest 转发代理用。
mysql:5.7 MySQL服务器。

文件结构

MySQL/
 |-logs/
 |-data/
 |-Dockerfile
Nginx/
 |-conf
  |-nginx.conf
  |-fastcgi_params
  |-fastcgi.conf
 |-Dockerfile
phpMyAdmin/
 |-Dockerfile
src/
 |-index.php
docker-compose.yml
Dockerfile

Dockerfile为Docker镜像构建文件,按照Docker的一个容器对应一个服务的约束,每一个服务都建设一个镜像。其中最外层的为php-fpm容器的构建文件。
docker-compose.yml为docker-compose用到的文件,一次配置并部署四个容器。
MySQL目录下包含的为MySQL的日志、数据。Nginx目录下的conf为Nginx配置文件夹。src为静态文件及php文件的路径

上手工作

首先编写最复杂的php-fpm的Dockerfile。

Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM php:7.3-fpm

RUN apt-get update && apt-get install -yq --no-install-recommends \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
&& 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 \
&& apt-get clean \
&& rm -r /var/lib/apt/lists/*

ENV TIMEZONE Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && echo $TIMEZONE > /etc/timezone

其他的容器也得统一时区,于是都写了一个Dockerfile用于在构建镜像的时候完成修改时区操作。用Nginx的Dockerfile作为例子。
Dockerfile
1
2
3
FROM nginx:latest
ENV TIMEZONE Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && echo $TIMEZONE > /etc/timezone

最重要的部分—编写大杀器docker-compose的配置文件
docker-compose.yml
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
version: '3.6'
services:
php-fpm:
build: .
volumes:
- ./src:/data/www:rw
links:
- db:db
restart: always
nginx:
build: ./Nginx
depends_on:
- phpmyadmin
- php-fpm
links:
- php-fpm:php-fpm
- phpmyadmin:phpmyadmin
volumes:
- ./Nginx/logs:/etc/nginx/logs:rw
- ./Nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
- ./Nginx/conf/fastcgi.conf:/etc/nginx/fastcgi.conf:ro
- ./Nginx/conf/fastcgi_params:/etc/nginx/fastcgi_params:ro
- ./src:/data/www:rw
ports:
- "8081:80"
command: nginx -g 'daemon off;'
db:
build: ./MySQL
volumes:
- ./MySQL/data:/var/lib/mysql:rw
- ./MySQL/logs:/var/lib/mysql-logs:rw
environment:
MYSQL_ROOT_PASSWORD: rootpass
command: "--character-set-server=utf8"
phpmyadmin:
build: ./phpMyAdmin
ports:
- "8082:80"
links:
- db:db
depends_on:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306

最后,用docker-compose up运行准备好的环境~
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
...
Starting php-docker-env_db_1 ... done
Starting php-docker-env_php-fpm_1 ... done
Starting php-docker-env_phpmyadmin_1 ... done
Starting php-docker-env_nginx_1 ... done
Attaching to php-docker-env_db_1, php-docker-env_php-fpm_1, php-docker-env_phpmyadmin_1, php-docker-env_nginx_1
php-fpm_1 | [18-Apr-2019 14:07:20] NOTICE: fpm is running, pid 1
db_1 | 2019-04-18T06:07:19.643459Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2019-04-18T06:07:19.645639Z 0 [Note] mysqld (mysqld 5.7.24) starting as process 1 ...
php-fpm_1 | [18-Apr-2019 14:07:20] NOTICE: ready to handle connections
phpmyadmin_1 | /usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
phpmyadmin_1 | 'Supervisord is running as root and it is searching '
phpmyadmin_1 | 2019-04-18 06:07:20,660 CRIT Supervisor is running as root. Privileges were not dropped because no user is specified in the config file. If you intend to run as root, you can set user=root in the config file to avoid this message.
phpmyadmin_1 | 2019-04-18 06:07:20,660 INFO Included extra file "/etc/supervisor.d/nginx.ini" during parsing
db_1 | 2019-04-18T06:07:19.651967Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1 | 2019-04-18T06:07:19.651994Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2019-04-18T06:07:19.652000Z 0 [Note] InnoDB: Uses event mutexes
db_1 | 2019-04-18T06:07:19.652006Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1 | 2019-04-18T06:07:19.652012Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1 | 2019-04-18T06:07:19.652017Z 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2019-04-18T06:07:19.652399Z 0 [Note] InnoDB: Number of pools: 1
db_1 | 2019-04-18T06:07:19.652542Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1 | 2019-04-18T06:07:19.654856Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1 | 2019-04-18T06:07:19.672218Z 0 [Note] InnoDB: Completed initialization of buffer pool
phpmyadmin_1 | 2019-04-18 06:07:20,660 INFO Included extra file "/etc/supervisor.d/php.ini" during parsing
db_1 | 2019-04-18T06:07:19.675293Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
...

看到这些初始化报告就代表环境已经部署好了~可以愉快的开发php了~先来写个简单的Hello World吧~在src的目录下创建index.php然后写入以下代码。

index.php
1
2
<?php
echo 'Hello World';

本文部署的服务器端口为8081,访问http://localhost:8081/就可以看见输出一个Hello World在页面了。

写在最后

项目已经传到Github上了,点我查看项目,欢迎大佬Fork一个/点一下Star/提交Issuse~

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×