Docker 搭建 LNMP + Redis

软件安装前的准备工作

安装 Docker
创建项目及服务目录
1
2
mkdir /data && cd /data
mkdir -p php/conf.d php/logs nginx/conf.d nginx/cert nginx/logs mysql/data mysql/logs redis/data app/default
/data/app/default 目录下创建测试用的项目文件
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
# /data/app/default/index.php
<?php
date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');

// Redis 测试
$redis = new Redis();
$redis->connect('172.18.0.10', 6379);
$redis->auth('123456');
$redis->set(mt_rand(1, 9999), date('Y-m-d H:i:s'));

// MySQL 测试
try {
$dbh = new PDO('mysql:host=172.18.0.9;dbname=test', 'root', '123456');
$result = $dbh->query('SHOW TABLES');
foreach ($result as $row) {
var_dump($row);
}
} catch (PDOException $e) {
var_dump($e->getMessage());
}

phpinfo();

# /data/app/default/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
It Works.
</body>
</html>
创建自定义网络
1
docker network create --subnet=172.18.0.0/16 lnmp

软件安装

PHP-FPM
  1. 复制配置文件

    1
    2
    3
    4
    5
    6
    7
    cd /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
  2. 创建 Dockerfile

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    FROM 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
  3. 生成并上传镜像, 然后运行容器

    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. 复制配置文件

    1
    2
    3
    4
    5
    6
    7
    cd /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
  2. 配置 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
    102
    server {
    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;
    #}
    }
  3. 创建 Dockerfile

    1
    2
    3
    4
    FROM 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
  4. 生成镜像并运行容器

    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. 拉取镜像

    1
    2
    3
    cd /data/mysql
    # 拉取镜像
    docker pull mysql:5.7
  2. 新建 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
  3. 新建 Dockerfile

    1
    2
    FROM mysql:5.7
    RUN chown -R mysql:mysql /var/log/mysql
  4. 生成镜像并运行容器

    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
  1. 进入 redis 目录 (cd /data/redis), 并新建 redis.conf

    1
    2
    3
    4
    # 持久化存储
    appendonly yes
    # 密码设置
    requirepass 123456
  2. 创建 Dockerfile

    1
    2
    FROM redis:latest
    CMD [ "redis-server", "/usr/local/etc/redis.conf" ]
  3. 生成镜像并运行容器

    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
2
3
4
5
6
7
location ~ \.php$ {
root /data/app/default;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
新建 /data/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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
version: '3'
services:
nginx:
build: ./nginx
container_name: nginx
restart: always
depends_on:
- php
ports:
- 80:80
- 443:443
volumes:
- /data/nginx/nginx.conf:/etc/nginx/nginx.conf
- /data/nginx/conf.d:/etc/nginx/conf.d
- /data/nginx/cert:/etc/nginx/cert
- /data/nginx/logs:/var/log/nginx
- /data/app:/data/app
networks:
lnmp:
ipv4_address: 172.18.0.8

php:
#build: ./php
image: sevming/php72:0.1
container_name: php
restart: always
volumes:
- /data/php/php.ini:/usr/local/etc/php/php.ini
- /data/app:/data/app
networks:
lnmp:
ipv4_address: 172.18.0.7

mysql:
build: ./mysql
container_name: mysql
restart: always
ports:
- 3306:3306
volumes:
- /data/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
- /data/mysql/data:/var/lib/mysql
- /data/mysql/logs:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
networks:
lnmp:
ipv4_address: 172.18.0.9

redis:
build: ./redis
container_name: redis
restart: always
ports:
- 6379:6379
volumes:
- /data/redis/redis.conf/:/usr/local/etc/redis.conf
- /data/redis/data:/data
networks:
lnmp:
ipv4_address: 172.18.0.10

networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
启动
1
2
# --build 重新构建
docker-compose up -d

Windows 下使用 Docker 作为开发环境

MySQL
1
2
3
4
5
6
7
8
# 需加入 --innodb-use-native-aio=0
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 --innodb-use-native-aio=0

# docker-compose.yml
mysql:
build: ./mysql
container_name: mysql
command: --innodb-use-native-aio=0
0%