来自 https://learnku.com/articles/9200/centos-7-uses-docker-to-build-a-basic-lnmp-environment

CentOS 7 使用 docker 搭建基本的 lnmp 环境

centos 下使用 docker 搭建 lnmp

准备:无毒无害绿色纯洁的 centos 7 一只

前言:

由于买了个新的服务器,空空白白的,所以一直由于是搭建 lnmp 还是用 docker,早上提了个问答,谢谢大家的回复,下午就尝试自己用 docker 来搭建 lnmp,都是比较基础的命令,因为我也比较菜,所以也有遇到挺多坑的,这边就记录下来,分享一下。
然后服务器因为就一个用户,所以我就省略 sudo 了

1. 准备 docker

1. 下载安装依赖包
  1. yum install -y yum-utils device-mapper-persistent-data lvm2

2. 网络问题就换源咯
  1. yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

3. 更新缓存,安装 docker-ce
  1. yum makecache fast yum install docker-ce

4. 启动 docker
  1. systemctl enable docker systemctl start docker docker run hello-world run 提示timeout的话,就要国内镜像加速了

5. 国内镜像加速

在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
{

  1. {
  2. "registry-mirrors": [
  3. "https://registry.docker-cn.com"
  4. ]
  5. }

] }

6. 之重新启动服务
  1. systemctl daemon-reload
  2. systemctl restart docker

如果能看到下面信息就代表正确咯

  1. $ docker run hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. latest: Pulling from library/hello-world
  4. ca4f61b1923c: Pull complete
  5. Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker!
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10. 1. The Docker client contacted the Docker daemon.
  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  12. (amd64)
  13. 3. The Docker daemon created a new container from that image which runs the
  14. executable that produces the output you are currently reading.
  15. 4. The Docker daemon streamed that output to the Docker client, which sent it
  16. to your terminal.
  17. To try something more ambitious, you can run an Ubuntu container with:
  18. $ docker run -it ubuntu bash
  19. Share images, automate workflows, and more with a free Docker ID:
  20. https://cloud.docker.com/
  21. For more examples and ideas, visit:
  22. https://docs.docker.com/engine/userguide/

更多参考
docker 中文文档

2. 拉取镜像 (nginx1.12.2,mysql5.7,php7.2)

1. 获取 mysql 镜像

  1. docker pull mysql:5.7

启动容器 cyt_mysql
  1. docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name cyt_mysql mysql:5.7

命令参数解释:
  1. -p:端口映射,映射容器的3306 后面就是密码和名称

2. 获取 php7.2 镜像

  1. docker pull php:7.2-fpm

创建 PHPfpm 容器
  1. 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

命令参数解释:
  1. -v 前面是主机的目录映射容器的目录 link:挂上msyql

测试目录映射:
  1. 进入到PHP容器 (可以用name也可以用容器id)
  2. docker exec cyt_phpfpm /bin/bash
  3. 就会到var/www/html 目录,新建一个PHP文件
  4. touch test.php
  5. 然后退出容器
  6. exit
  7. 到主机的 var/nginx/www/html 目录下也出现了个test.php

php 的扩展安装
  1. docker-php-ext-install pdo_mysqlcurl ...)
  2. 要安装php-redis的话,需要另外下载,执行下面这个命令就可以了,有问就no或者空格就好
  3. pecl install redis && docker-php-ext-enable redis
  4. 安装后 php-m
  5. 发现就都有了哦

3. 获取 Nginx1.12.2 镜像

  1. docker pull ngixn:1.12.2

运行 Nginx 容器
  1. docker run -d -p 80:80 --name cyt_nginx -v /var/nginx/www/html:/var/www/html --link
  2. cyt_phpfpm:phpfpm --name cyt_nginx nginx:1.12.2

参数解释
  1. -p:映射80端口
  2. -v:映射目录,最好和PHP的一样
  3. -name:容器名
  4. -link:跟PHP关联

修改 Nginx 容器配置让他支持 PHP

进入 Nginx 容器:

  1. docker exec -it cyt_nginx /bin/bash

cd 到 conf 目录

  1. cd etc/nginx/conf.d/

修改配置文件 default.conf

  1. vi default.conf

如果提示 vi 命令不存在,那就下一个 vi

  1. apt-get update apt-get install vim

继续改配置,把对 php 支持的注释去掉并修改路径

  1. 前面有#号去掉
  2. location ~ \.php$ {
  3. root /var/www/html;
  4. fastcgi_pass phpfpm:9000;
  5. fastcgi_index index.php;
  6. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  7. include fastcgi_params;
  8. }

重新加载 Nginx
  1. nginx -s reload

退出容器
  1. exit

酱紫基本环境就搭建结束了
测试:
找到那个 index.php
echo phpinfo();
访问下 ip,看是不是 PHPinfo?
如果有 404 什么的问题一般就是 Nginx 配置问题了,可以根据 Nginx 解析顺序自己改改

  1. location = / {
  2. root /var/www/html/;
  3. index index.php index.htm index.html;
  4. }
  5. location / {
  6. root /usr/local/nginx/html;
  7. index index.html index.htm;
  8. }

}
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