温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker部署lnmp集群

发布时间:2020-06-24 10:44:02 来源:网络 阅读:631 作者:qq5bb2021f831d3 栏目:系统运维

LNMP的网段为:
172.16.10.0/24

服务容器与各个ip:
nginx 172.16.10.10
mysql 172.16.10.20
php 172.16.10.30**

导入一下镜像包:
mysql5.7
php.7.2-fpm.tar
wordpress.tar
nginx.tar

将导入的镜像包导成docker可用的镜像:
docker load < nginx.tar && docker load < wordpress.tar && docker load < mysql-5.7.tar && docker load < nginx.tar && docker load < php.7.2-fpm.tar

宿主机创建两个目录:
分别是:
网站的访问主目录:mkdir /wwwroot
配置文件目录: mkdir /docker

nginx准备工作:
nginx配置文件:/etc/nginx/conf.d
nginx主目录:/usr/share/nginx/html

先运行一个nginx容器,将容器里的nginx主目录和配置文件导出来:
docker run -itd --name test nginx:latest

docker cp test:/etc/nginx /docker

docker cp test:/usr/share/nginx/html /wwwroot

查看80端口,不可被占用:
netstat -anpt | grep 80

准备工作已经完成,接下来开始部署各个服务:

1)创建一个自定义网络:
docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp

2)运行nginx:
docker run -itd --name nginx -v /docker/nginx:/etc/nginx -v /wwwroot/html:/usr/share/nginx/html -p 80:80 --network lnmp --ip 172.16.10.10 nginx

更改nginx访问界面:
cd /wwwroot/html
cat index.html
Hello lnmp

访问本机验证:
curl 127.0.0.1
Hello lnmp

浏览器访问出下面就成功:

docker部署lnmp集群

3)运行mysql:
运行一个mysql容器,并为它创建密码为123.com:
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123.com -d -p 3306:3306 --network lnmp --ip 172.16.10.20 mysql:5.7

登录mysql验证:
先下载一个mysql
[root@localhost ~]# yum -y install mysql

访问验证:
[root@localhost ~]# mysql -u root -p123.com -h 127.0.0.1 -P 3306

随便新建一个库做验证:
MySQL [(none)]> create database name;

再查看有没有刚创建的库:
MySQL [(none)]> show databases;

4)运行php容器:

运行一个php容器
[root@localhost ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 php:7.2-fpm

添加php测试界面:
[root@localhost html]# pwd
/wwwroot/html
[root@localhost html]# vim test.php

<?php
phpinfo();
?>
浏览器访问:出现下面界面就成功

docker部署lnmp集群

5)修改nginx配置文件,nginx和php连接
[root@localhost conf.d]# pwd
/docker/nginx/conf.d

[root@localhost conf.d]# vim default.conf

10行:添加index.php解析

​ index index.html index.htm index.php;

30行:打开模块(去掉#并添加)并更改相信息:

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 172.16.10.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

重启nginx服务:

[root@localhost conf.d]# docker restart nginx
nginx

关闭防火墙:

[root@localhost conf.d]# systemctl stop firewalld
[root@localhost conf.d]# systemctl disable firewalld

重启docker:

[root@localhost conf.d]# systemctl daemon-reload
[root@localhost conf.d]# systemctl restart docker

重启所有容器:

[root@localhost conf.d]# docker ps -a -q | xargs docker start

接下来是php和mysql的连接,在这里我们使用一个phpMyAdmin的数据库管理工具。

下载导入phpMyAdmin-4.9.1-all-languages

导入需要的包:
[root@localhost html]# pwd
/wwwroot/html

[root@localhost html]# ls
50x.html phpMyAdmin-4.9.1-all-languages.zip
index.html test.php

解压:

[root@localhost html]# unzip phpMyAdmin-4.9.1-all-languages.zip

[root@localhost html]# ls
50x.html phpMyAdmin-4.9.1-all-languages test.php
index.html phpMyAdmin-4.9.1-all-languages.zip

名字太长不好记,待会要在nginx配置文件更改,重命名一下:

[root@localhost html]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin

[root@localhost html]# ls
50x.html phpmyadmin test.php
index.html phpMyAdmin-4.9.1-all-languages.zip

//更改nginx配置文件:

[root@localhost html]# cd /docker/nginx/conf.d/
[root@localhost conf.d]# vim default.conf

27行添加:

   location /phpmyadmin {
            root   /usr/share/nginx/html;
            index   index.html index.htm index.php;    }

41行添加:

location ~ /phpmyadmin/(?<after_ali>(.*).(php|php5)?$) {
root /usr/share/nginx/html;
fastcgi_pass 172.16.10.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}

重启nginx:

[root@localhost conf.d]# docker restart nginx
nginx

浏览器访问
本机ip/phpmyadmin/index.php

docker部署lnmp集群

报红框属于正常现象,不要惊慌,接下来就解决它

需要我们对php镜像做出更改,添加php和mysql连接的模块

写一个Dockerfile:
[root@localhost ~]# vim Dockerfile

FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mysqli pdo pdo_mysql

执行一下:这条命令加载时间有点长,请耐心等待!!!

[root@localhost ~]# docker build -t phpmysql .

查看是否有新创建的镜像:

[root@localhost ~]# docker images

[root@localhost ~]# docker stop phpfpm

我的是这个:

phpmysql latest 6e65aee3bd4d 9 minutes ago 422MB

关闭原先的php镜像:

[root@localhost ~]# docker stop phpfpm
phpfpm

删除之前的php容器

[root@localhost ~]# docker rm phpfpm
phpfpm

使用新创建的php镜像创建新的php容器

[root@localhost ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 phpmysql

//修改phpmyadmin的配置文件,指定连接的数据库的ip

[root@localhost phpmyadmin]# pwd
/wwwroot/html/phpmyadmin

改名:

[root@localhost phpmyadmin]# cp config.sample.inc.php config.inc.php

编辑:

[root@localhost phpmyadmin]# vim config.inc.php

31行:改为mysql数据库的ip

$cfg['Servers'][$i]['host'] = '172.16.10.20';

重启php容器:

[root@localhost phpmyadmin]# docker restart phpfpm
phpfpm

浏览器访问:本机ip/phpmyadmin/index.php
出现以下界面成功
docker部署lnmp集群
用户:root 密码:123.com

登录成功会看到之前创建的数据库:

docker部署lnmp集群

到这就lnmp部署完成!!!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI