软件安装前的准备工作
安装 Docker
创建项目及服务目录
1 | mkdir /data && cd /data |
在 /data/app/default
目录下创建测试用的项目文件
1 | # /data/app/default/index.php |
创建自定义网络
1 | docker network create --subnet=172.18.0.0/16 lnmp |
软件安装
PHP-FPM
复制配置文件
1
2
3
4
5
6
7cd /data/php
# 拉取 php-fpm
docker pull php:7.2-fpm
# 复制 php.ini-development 到本地
docker run --rm php:7.2-fpm cat /usr/local/etc/php/php.ini-development > php.ini
# 拉取 php-extension-installer
docker pull mlocati/php-extension-installer创建
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24FROM php:7.2-fpm
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster main non-free contrib' > /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian-security buster/updates main' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian-security buster/updates main' >> /etc/apt/sources.list
RUN apt update && apt install -y wget && apt install -y vim && apt install -y iputils-ping
# Install Extensions
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions zip gd xdebug redis \
&& docker-php-ext-install bcmath pcntl pdo_mysql
# Add Composer
RUN php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& mv composer.phar /usr/local/bin/composer生成并上传镜像, 然后运行容器
1
2
3
4
5
6# 生成镜像
docker build -t sevming/php72:0.1 .
# 上传镜像
docker push sevming/php72:0.1
# 运行容器
docker run -itd --rm --name php --net lnmp --ip 172.18.0.7 -v /data/app:/data/app -v /data/php/php.ini:/usr/local/etc/php/php.ini sevming/php72:0.1
Nginx
复制配置文件
1
2
3
4
5
6
7cd /data/nginx
# 拉取 nginx
docker pull nginx:1.19.0
# 复制 nginx.conf 到本地
docker run --rm nginx:1.19.0 cat /etc/nginx/nginx.conf > nginx.conf
# 复制 default.conf 至 conf.d 目录
docker run --rm nginx:1.19.0 cat /etc/nginx/conf.d/default.conf > ./conf.d/default.conf配置
default.conf
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#禁止使用ip直接访问
#if ( $host ~* "\d+\.\d+\.\d+\.\d+" ) {
# return 501;
#}
location / {
root /data/app/default;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/app/default;
}
# 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 ~ \.php$ {
root /data/app/default;
fastcgi_pass 172.18.0.7:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# SSL 配置,不需要就注释
server {
listen 443 ssl;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
ssl_certificate /etc/nginx/cert/default.pem;
ssl_certificate_key /etc/nginx/cert/default.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /data/app/default;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/app/default;
}
# 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 ~ \.php$ {
root /data/app/default;
fastcgi_pass 172.18.0.7:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}创建
Dockerfile
1
2
3
4FROM nginx:1.19.0
RUN mkdir -p /data/app
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone生成镜像并运行容器
1
2
3
4# 生成镜像
docker build -t sevming/nginx .
# 运行容器
docker run -itd --rm --name nginx -p 80:80 -v /data/app:/data/app -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/nginx/cert:/etc/nginx/cert -v /data/nginx/logs:/var/log/nginx --net lnmp --ip 172.18.0.8 sevming/nginx
MySQL
拉取镜像
1
2
3cd /data/mysql
# 拉取镜像
docker pull mysql:5.7新建
my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[client]
port = 3306
default-character-set=utf8
[mysqld]
port = 3306
datadir = /var/lib/mysql
character-set-server=utf8
skip-name-resolve
log_error = /var/log/mysql/error.log
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION新建
Dockerfile
1
2FROM mysql:5.7
RUN chown -R mysql:mysql /var/log/mysql生成镜像并运行容器
1
2
3
4# 生成镜像
docker build -t sevming/mysql .
# 运行容器
docker run --rm -itd --name mysql -p 3306:3306 -v /data/mysql/data:/var/lib/mysql -v /data/mysql/my.cnf:/etc/mysql/conf.d/my.cnf -v /data/mysql/logs:/var/log/mysql --net lnmp --ip 172.18.0.9 -e MYSQL_ROOT_PASSWORD=123456 sevming/mysql
Redis
进入
redis
目录 (cd /data/redis
), 并新建redis.conf
1
2
3
4# 持久化存储
appendonly yes
# 密码设置
requirepass 123456创建
Dockerfile
1
2FROM redis:latest
CMD [ "redis-server", "/usr/local/etc/redis.conf" ]生成镜像并运行容器
1
2
3
4# 生成镜像
docker build -t sevming/redis .
# 运行容器
docker run --rm -d --name redis -p 6379:6379 -v /data/redis/data:/data -v /data/redis/redis.conf:/usr/local/etc/redis.conf --net lnmp --ip 172.18.0.10 sevming/redis
浏览器访问 服务器IP
以上这种方式, 需要一个个启动容器, 可以采用 docker-compose
来配置服务
修改 /data/nginx/conf.d/default.conf
1 | location ~ \.php$ { |
新建 /data/docker-compose.yml
1 | version: '3' |
启动
1 | # --build 重新构建 |
Windows 下使用 Docker
作为开发环境
MySQL
1 | # 需加入 --innodb-use-native-aio=0 |