yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce
systemctl enable docker
systemctl start docker
docker run hello-world
run 提示timeout的话,就要国内镜像加速了
在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
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 中文文档
docker pull mysql:5.7
启动容器 cyt_mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name scx_mysql mysql:5.7
命令参数解释:
-p:端口映射,映射容器的3306
后面就是密码和名称
docker pull php:7.2-fpm
创建 PHPfpm 容器
docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link scx_mysql:mysql --name scx_phpfpm php:7.2-fpm
命令参数解释:
-v 前面是主机的目录映射容器的目录
link:挂上msyql
测试目录映射:
进入到PHP容器 (可以用name也可以用容器id)
docker exec it scx_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
发现就都有了哦
docker pull nginx:1.12.2
运行 Nginx 容器
docker run -d -p 80:80 --name scx_nginx -v /var/nginx/www/html:/var/www/html --link scx_phpfpm:phpfpm --name scx_nginx nginx:1.12.2
参数解释
-p:映射80端口
-v:映射目录,最好和PHP的一样
-name:容器名
-link:跟PHP关联
修改 Nginx 容器配置让他支持 PHP
进入 Nginx 容器:
docker exec -it scx_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
原文作者:bestcyt
转自链接:https://learnku.com/articles/9200/centos-7-uses-docker-to-build-a-basic-lnmp-environment#4c36a6
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。