CentOS7 Docker 安装 PHP,Mysql,phpmyadmin
20190408 自动上次学习 docker,三个月没有接触,基本上都忘记了上次是在 ubuntu 18.04 下面学习的,这次转到 CentOS 7 ,差别加上遗忘,基本上从头再来一次!环境:1、 visualbox 虚拟机CentOS 7.6 (IP:192.168.1.184)只安装了nginx (本次不做 Docker nginx 容器)2、 visualbox 虚拟机C...
1、安装 Docker
官网
https://docs.docker.com/install/
找到 linux --Centos,操作步骤如下
Install Docker Engine on CentOS
直接按照他的操作步骤进行即可
抄录一下
-
Set up the repository
sudo yum install -y yum-utils sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
-
Install Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io
-
sudo systemctl start docker
-
sudo docker run hello-world
设置开机启动的话,继续
-
$ sudo systemctl list-unit-files |grep docker
docker.service disabled
docker.socket disabled -
$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
2、安装 PHP
sudo docker pull php
** latest 版本是 7.2 的,本次只处理 php5.6
sudo docker pull php:5.6-fpm
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php latest 90102e6d83ad 47 hours ago 367MB
php 5.6-fpm 3458979c7744 2 months ago 344MB
hello-world latest fce289e99eb9 2 months ago 1.84kB
sudo docker run -p 9000:9000 --name myphp56 -v /usr/share/nginx/html:/var/www/html -d php:5.6-fpm
参数说明:
--name myphp56:将容器命名为 myphp56
-v /usr/share/nginx/html:/var/www/html : 将宿主机的网站目录(/usr/share/nginx/html)挂载到容器的网站目录(/var/www/html)
-p 9000:9000:将容器myphp72的 9000 端口映射到宿主机的 9000 端口
*** php 缺省端口是 9000 ,如果宿主机也安装过php,可能会冲突,可以改到 9001
3、测试 nginx + php
** 本次重点测试 docker 。所以, nginx 的配置直接修改 nginx.conf ,端口方式访问或域名方式网站另行配置
-
修改 nginx 配置,加入 php 配置
cd /etc/nginx
sudo vim nginx.conf
加入 php 文件的 locationlocation ~ \.php$ { # include fastcgi.conf; # this is on local #root /usr/share/nginx/html; # thsi is on docker,not local /var/www/html root /var/www/html; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $uri; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; include fastcgi_params; }
** centos 7 缺省的 nginx WEB 目录不在 /var/www/html ,而是 /usr/share/nginx/html
** 以上 root /var/www/html; 指的是 宿主机的 /var/www/html 映射到 docker 容器伤的 WEB 目录
-
建立一个 index.php 文件,用于测试
cd /var/www/html sudo vim index.php <?php phpinfo(); ?>
-
启动 docker php 容器
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 15 minutes 0.0.0.0:9000->9000/tcp myphp56 docker start myphp56 docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 14 minutes 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
-
测试一下
curl 127.0.0.1/index.php
curl 192.168.1.184/index.php
浏览器下打开网址:http://192.168.1.184/index.php
4、Docker PHP 错误处理
-
页面暂时找不到
The page you are looking for is temporarily unavailable. Please try again later.
docker ps -s 检查一下, docker myphp56 没有 start
-
找不到文件
curl 127.0.0.1/index.php
File not found.检查 nginx.conf php 模块中 root 定义,容易犯错误的是:将宿主机的 WEB 目录 和 docker 容器的目录搞混!
之前在 ubuntu 没有遇到这个问题,那里宿主机和 docker 的WEB 目录都是 /var/www/html
以下是我的 php 配置部分location ~ \.php$ { # include fastcgi.conf; # this is on local # root /usr/share/nginx/html; # thsi is on docker,not local /var/www/html root /var/www/html; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $uri; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; include fastcgi_params; }
-
浏览器打开变成下载
1).重点检查 fastcgi_pass 的配置 ** 我这里安装的 Centos 没有 php-fpm.sock,docker 里面也没有,所以只能使用端口方式 #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_pass 127.0.0.1:9000; 2). Centos 7.6 的防火墙规则需要开启 Docker 映射的 9000 端口 如果宿主机的 firewalld 是开启的,最好一起开放 80, 443, 9000等端口,否则,本地 curl 可能是对的,在局域网浏览器也打不开! sudo firewall-cmd --zone=public --add-port=80/tcp --permanent sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent sudo firewall-cmd --zone=public --add-port=9000/tcp --permanent sudo systemctl restart firewalld.service
5、安装 Docker mysql
docker search mysql
sudo docker pull mysql
** latest 版本是 mysql 8.0,本次处理 mysql 5.7
sudo docker pull mysql:5.6
sudo docker pull mysql:5.7
sudo docker ps
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 bc788a6a2745 12 days ago 256MB
mysql 5.7 98455b9624a9 12 days ago 372MB
mysql latest 7bb2586065cd 12 days ago 477MB
phpmyadmin/phpmyadmin latest c6ba363e7c9b 2 months ago 166MB
php 5.6-fpm 3458979c7744 2 months ago 344MB
先使用 latest 试试
sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
docker inspect mysql
......
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.15-1debian9"
],
......
再启动 mysql 5.7
sudo docker run --name mysql57 -v mysql57:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
参数说明:
--name mysql57 给容器取名为 mysql57
-v mysql57:/var/lib/mysql 挂在一个volume(取名为mysql57)到容器的/var/lib/mysql
**给volume(数据卷)取名便于管理
** mysql 缺省的数据都是放在 /var/lib/mysql 的
-e MYSQL_ROOT_PASSWORD=123456 设置mysql环境的root密码
-p 3306:3306 宿主机的 3306 端口(前一个3306)对应到docker 的 3306 端口(后面的 3306)
出现错误!
docker: Error response from daemon: Conflict. The container name "/mysql57" is already in use by container "7650608f3e1fc6f85614f059c9049f9259993cfcc4863cb566d504311d00df33". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
** 端口冲突了!
简单处理,直接删除 mysql 8.0 容器
docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
56605931a504 mysql "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql 7B (virtual 477MB)
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 26 minutes 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
docker stop mysql
docker rm mysql
再次启动 mysql 5.7
docker start mysql57
** 虽然刚才因为冲突启动失败,但是 mysql57 容器已经建立,所以,直接 start 即可
查看 mysql 容器信息
sudo docker inspect mysql57
...
"Mounts": [
{
"Type": "volume",
"Name": "mysql",
"Source": "/var/lib/docker/volumes/mysql/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
...
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.25-1debian9"
],
...
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
...
6、 连接 mysql57 容器测试
-
宿主机上远程连接 (刚才inspect 到的容器 IP= 172.17.0.3)
mysql -h172.17.0.3 -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> select version(); +-----------+ | version() | +-----------+ | 5.7.25 | +-----------+ 1 row in set (0.00 sec)
-
进入容器查看
docker exec -it mysql57 bash
root@e80eaaa17d57:/# mysql -V
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
容器内本地连接 mysql
mysql -uroot -p123456
…
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
7、 安装 Docker phpMyAdmin
查找 phpmyadmin 镜像
sudo docker search phpmyadmin
docker pull phpmyadmin/phpmyadmin
** 提前运行前面安装的 Docker php56 和 mysql 57
sudo docker run --name phpmyadmin --link mysql57:db -p 9998:80 -d phpmyadmin/phpmyadmin
参数说明:
--name phpmyadmin 容器取名 phpmyadmin
--link mysql57:db 连接到 容器mysql ,给这个 link 一个别名为db
-p 9998:80 宿主机9998 端口对应到 phpmyadmin的 80 端口
sudo docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
0f5599cb72a6 phpmyadmin/phpmyadmin "/run.sh supervisord…" 15 seconds ago Up 14 seconds 9000/tcp, 0.0.0.0:9998->80/tcp phpmyadmin 33.7MB (virtual 200MB)
e80eaaa17d57 mysql:5.7 "docker-entrypoint.s…" 21 minutes ago Up 21 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql57 157B (virtual 372MB)
dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 2 hours 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)
查看 phpmyadmin 容器信息
docker inspect phpmyadmin
...
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9998"
}
],
"9000/tcp": null
},
...
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PHPIZE_DEPS=autoconf \t\tdpkg-dev dpkg \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkgconf \t\tre2c",
"PHP_INI_DIR=/usr/local/etc/php",
"PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi",
"PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2",
"PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2",
"PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie",
"GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F",
"PHP_VERSION=7.2.14",
"PHP_URL=https://secure.php.net/get/php-7.2.14.tar.xz/from/this/mirror",
"PHP_ASC_URL=https://secure.php.net/get/php-7.2.14.tar.xz.asc/from/this/mirror",
"PHP_SHA256=ee3f1cc102b073578a3c53ba4420a76da3d9f0c981c02b1664ae741ca65af84f",
"PHP_MD5=",
"VERSION=4.8.5",
"URL=https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.tar.xz"
],
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
...
8 、测试 Docker phpMyAdmin
docker port phpmyadmin
80/tcp -> 0.0.0.0:9998
curl 127.0.0.1:9998
curl 192.168.1.184:9998
http://192.168.1.184:9998/
登录查看 mysql
由于本次学习的虚拟机上,没有安装 php 和 mysql ,所以,以上信息一定是 Docker 容器的
更多推荐
所有评论(0)