docker随笔8--手动安装lnmp

    如果不想使用别人搭建好的环境,或者想使用一个相对比较纯净的环境,就需要自己搭建环境,搭建lnmp有两种方法,一种是在一个容器里搭建所有,另一种是通过容器互联的方式,前一种启动的时候较方便一些,但是多个应用之间会有影响,第二种,不同的容器之间负载的时候不会有影响,但是启动的时候,要处理先后关系。

下面是通过数据卷和容器互联的方式建立lnmp环境:

 1.获取镜像:

docker pull php:7.2-fpm
docker pull mysql:5.7
docker pull nginx

2.设置mysql,并运行容器

docker run -d -p 3309:3306 -e MYSQL_ROOT_PASSWORD=root --name mysqler mysql:5.7

-e  设置环境变量,可以通过docker镜像去搜索mysql容器的使用方法 地址点这里

3.运行php容器:

docker run -d -v /docker/nginx/www/html/:/var/www/html -p 9001:9000 --name phper --link mysqler php:7.2-fpm

数据卷/docker/nginx/www/html,如果没有会自动创建。

php运行还需要一些必要的扩展,进入正在运行的php容器:

docker exec -ti phper  /bin/bash 

进入容器后,安装pdo插件:

docker-php-ext-install pdo_mysql

查看是否安装成功

php -m #出现pdo则说明安装成功

退出容器,重启容器

exit  #退出命令
docker restart phper #重启容器

4.运行nginx 容器

启动容器:

docker run -d -p 8088:80 --name mynginx -v /docker/nginx/www/html/:/var/www/html  --link phper:phper nginx

修改nginx,让其支持php:

进入nginx:

docker exec -ti  mynginx /bin/bash

修改配置:

拉取的nginx并没有任何文本编辑器,需要手动下载vim

apt-get update

更换为国内的源:

进入到/etc/apt目录:

echo

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse“ >> b.txt

将原来的源文件作为备份:

mv source.list  source.list.bak
mv b.txt source.list

修改nginx的配置,使其支持php模块:

进入/etc/nginx模块:查看nginx.conf,里面server模块,是通过include 同级目录下的conf.d加载的,修改conf.d

location ~ .php$ {
        root           /var/www/html;
        fastcgi_index  index.php;
        fastcgi_pass   phper:9000;//注意这个是一定要改的对应link时,对应php的名字
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;                                                                                                                                               
                                                                                                                                                                                     
    }

增加一条伪静态的重写规则,如果不加的话访问localhost:8088/index 的时候无法找到文件,必须增加后缀才能访问:

 location / {
        try_files $uri $uri/ $uri.php?$args;
    }

退出,并重启nginx容器:

exit
docker restart mynginx

测试,在本地的宿主机/docker/nginx/www/html/上,新建test.php

<?php
phpinfo();

通过localhost:8088/test.php访问,出现php配置信息,lnmp环境搭建成功。

原文地址:https://www.cnblogs.com/callmelx/p/11065558.html