使用docker搭建lnmp环境

1 篇文章 0 订阅
1 篇文章 0 订阅

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 中文文档

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

1. 获取 mysql 镜像

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
后面就是密码和名称

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 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
发现就都有了哦

3. 获取 Nginx1.12.2 镜像

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
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值