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

centos 下使用 docker 搭建 lnmp#

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

前言:#

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

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 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

第一次写,有不足的请大家提出了,多多指教:blush:#
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 7年前 自动加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 15

nginx 和 mysql 可以挂载文件,这样方便配置 nginx 和 mysql 数据文件保存

7年前 评论
lol173

还可以更强

7年前 评论
susucool

感谢老铁,已经按照你的成功了。
小错误:nginx 的配置文件 default.conf 里的代码:fastcgi_param SCRIPT_FILENAME/var/www/html/$fastcgi_script_name; 这行应该改为 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
然后在开头

location = / { 
    root   /var/www/html/;
    index  index.htm index.html;
}

加入 index.php

location = / { 
    root   /var/www/html/;
    index  index.php index.htm index.html;
}

最后想问老铁,laravel 中需要用到 composer 命令怎么办?拉取了一个 composer 镜像,然后怎么用呢?

7年前 评论

安装 5.6php+mysql5.6 这个命令行报错
运行:
docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link cyt_mysql:mysql --name cyt_phpfpm php:5.6-fpm
报错:
ba4f3ff8d2466bdefac908077cc0e8a7180a43507f8c60cf0b73824df944057d
docker: Error response from daemon: Cannot link to a non running container: /cyt_mysql AS /cyt_phpfpm/mysql.

7年前 评论
ThinkQ

不错

7年前 评论

docker pull nginx:1.12.2

6年前 评论

谢谢谢谢,折腾了好久,错在 “fastcgi_pass phpfpm:9000;”,还好终于 ok 了。第一步好不容易啊 - -

6年前 评论
munxmayun

@aen233 "fastcgi_pass" directive is not allowed here in /etc/nginx/conf.d/default.conf:32 我也出现这个错误了 我改成 php:7.2-php 还是报错 求方法 谢谢

6年前 评论

@munxmayun 解决了么?fastcgi_pass phpfpm:9000; 这里写 phpfpm:9000,是因为 nginx 运行的时候有个参数是 --link cyt_phpfpm:phpfpm。用的是冒号后的 phpfpm。
如果写 fastcgi_pass phpfpm:9000,还是报错,建议查看下阿里云 ECS 的安全组的规则配置,默认貌似只有 80 端口和 220 端口这类基础端口,php 用的 9000,mysql 用的 3306,redis 的 6379 都没有配置上,如果你要添加 mysqladmin 这类的,还要加 8080 端口,看需求配置规则吧。

6年前 评论
munxmayun

@aen233 已经解决了。 谢谢。

6年前 评论
Mr_Xu

应该是 docker exec -it cyt_phpfpm /bin/bash

6年前 评论
萧风

请问如何解决呢 @bestcyt @aen233 @munxmayun @所有人
bash: docker-php-ext-install: 未找到命令

file

6年前 评论
CarlGao 5年前

把 PHP 和 nginx 的配置文件日志文件,还有 MySQL 的数据文件挂载上就更完美啦

5年前 评论