docker 安装 php、nginx、MySQL8

1. 安装php

拉取官方镜像

1
docker pull php:7.2-fpm
2. 安装nginx

拉取官方镜像

1
docker pull nginx:1.17.1
3. 部署nginx+php

启动php:

1
docker run --name myphp-fpm -v ~/nginx/www:/usr/share/nginx/html -d php:7.2-fpm

说明:

  • –name myphp-fpm : 将容器命名为 myphp-fpm。
  • -v ~/nginx/www:/usr/share/nginx/html : 将主机中项目的目录 www 挂载到容器的 /usr/share/nginx/html

创建 ~/nginx/conf/conf.d 目录:

1
mkdir ~/nginx/conf/conf.d

在该目录下添加 ~/nginx/conf/conf.d/xxxxx.conf 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name xxxxx.com.cn www.xxxxx.com.cn;

location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}

配置文件说明:

  • php:9000: 表示 php-fpm 服务的 URL,下面我们会具体说明。
  • /usr/share/nginx/html/: 是 myphp-fpm 中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录。

启动nginx:

1
2
3
4
5
docker run --name php-nginx -p 80:80 -d \ 
-v ~/nginx/www:/usr/share/nginx/html:ro \
-v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
--link myphp-fpm:php \
nginx
  • -p 80:80: 端口映射,把 nginx 中的 80 映射到本地的 80 端口。
  • ~/nginx/www: 是本地 html 文件的存储目录,/usr/share/nginx/html 是容器内 html 文件的存储目录。
  • ~/nginx/conf/conf.d: 是本地 nginx 配置文件的存储目录,/etc/nginx/conf.d 是容器内 nginx 配置文件的存储目录。
  • –link myphp-fpm:php: 把 myphp-fpm 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把域名 php 映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。

接下来就可以在~/nginx/www目录下创建php项目来访问

4. 安装MySQL

拉取官方镜像:

1
docker pull mysql:8.0.16

创建MySQL相关目录:

1
mkdir -p  ~/mysql/data ~/mysql/log ~/mysql/conf

创建~/mysql/conf/my.cnf配置文件

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
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

lower_case_table_names=1
character-set-server=utf8
default_authentication_plugin=mysql_native_password

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

# Custom config should go here
!includedir /etc/mysql/conf.d/

创建mysql8容器:

1
2
3
4
5
6
7
8
9
10
11
docker run \
-p 3306:3306 \
--restart=always \
--privileged=true \
--name mysql8 \
-e MYSQL_ROOT_PASSWORD="TRlmRw8xGqYM" \
-v /root/mysql/data:/var/lib/mysql:rw \
-v /root/mysql/log:/var/log/mysql:rw \
-v /root/mysql/conf/my.cnf:/etc/mysql/my.cnf:rw \
-v /etc/localtime:/etc/localtime:ro \
-d mysql:8.0.16

相关命令:

  • 进入镜像:

    1
    docker run -it mysql:8.0.16 /bin/bash
  • 进入容器:

    1
    docker exec -it mysql8 /bin/sh