准备:无毒无害绿色纯洁的centos 7一只
前言:
1.准备docker
1.下载安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
2.网络问题就换源咯
yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
3.更新缓存,安装docker-ce
yum makecache fast
yum install docker-ce
4.启动docker
systemctl enable docker
systemctl start docker
docker run hello-world
run 提示timeout的话,就要国内镜像加速了
5.国内镜像加速
在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
6.之重新启动服务
systemctl daemon-reload
systemctl restart docker
如果能看到下面信息就代表正确咯
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
更多参考
docker中文文档
1.获取mysql镜像
docker pull mysql:5.7
启动容器cyt_mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name cyt_mysql mysql:5.7
命令参数解释:
-p:端口映射,映射容器的3306
后面就是密码和名称
2.获取php7.2镜像
docker pull php:7.2-fpm
创建PHPfpm容器
docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link cyt_mysql:mysql --name cyt_phpfpm php:7.2-fpm
命令参数解释:
-v 前面是主机的目录映射容器的目录
link:挂上msyql
测试目录映射:
进入到PHP容器 (可以用name也可以用容器id)
docker exec cyt_phpfpm /bin/bash
就会到var/www/html 目录,新建一个PHP文件
touch test.php
然后退出容器
exit
到主机的 var/nginx/www/html 目录下也出现了个test.php
php的扩展安装
docker-php-ext-install pdo_mysql(curl ...)
要安装php-redis的话,需要另外下载,执行下面这个命令就可以了,有问就no或者空格就好
pecl install redis && docker-php-ext-enable redis
安装后 php-m
发现就都有了哦
3.获取Nginx1.12.2镜像
docker pull ngixn:1.12.2
运行Nginx容器
docker run -d -p 80:80 --name cyt_nginx -v /var/nginx/www/html:/var/www/html --link cyt_phpfpm:phpfpm --name cyt_nginx nginx:1.12.2
参数解释
-p:映射80端口
-v:映射目录,最好和PHP的一样
-name:容器名
-link:跟PHP关联
修改Nginx容器配置让他支持PHP
进入Nginx容器:
docker exec -it cyt_nginx /bin/bash
cd到conf目录
cd etc/nginx/conf.d/
修改配置文件default.conf
vi default.conf
如果提示vi命令不存在,那就下一个vi
apt-get update
apt-get install vim
继续改配置,把对php支持的注释去掉并修改路径
前面有#号去掉
location ~ \.php$ {
root /var/www/html;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME/var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
重新加载Nginx
nginx -s reload
退出容器
exit
酱紫基本环境就搭建结束了
测试:
找到那个index.php
echo phpinfo();
访问下ip,看是不是PHPinfo?
如果有404什么的问题一般就是Nginx配置问题了,可以根据Nginx解析顺序自己改改
location = / {
root /var/www/html/;
index index.htm index.html;
}
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location配置如上,若访问http://xxx.com/,定位的流程是:
1:精准匹配命中"/",得到index页为index.htm,所以请求的地址变为http://xxx.com/index.htm
2:再次匹配"/index.htm",此次内部转跳uri已经是"/index.htm",命中普通匹配"/",根目录为/usr/local/nginx/html
3:最终结果,访问了/usr/local/nginx/html/index.htm