欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker部署LNMP环境

程序员文章站 2022-04-18 22:05:43
...

Docker部署LNMP环境

172.16.10.0/24

Nginx:172.16.10.10

Mysql:172.16.10.20

PHP:172.16.10.30

 

网站的访问主目录:/wwwroot

Nginx的配置文件:/docker

[aaa@qq.com ~]# docker  run  -itd  --name  test  nginx:latest
[aaa@qq.com ~]# mkdir  /wwwroot
[aaa@qq.com ~]# mkdir  /docker
[aaa@qq.com ~]# docker cp test:/etc/nginx  /docker/
[aaa@qq.com ~]# ls /docker/
nginx
[aaa@qq.com ~]# docker cp test:/usr/share/nginx/html /wwwroot/
[aaa@qq.com ~]# ls  /wwwroot/
html
[aaa@qq.com ~]# vim   /wwwroot/html/index.html
[aaa@qq.com ~]# cat  /wwwroot/html/index.html 
hello  LNMP!

1)创建一个自定义网络

[aaa@qq.com ~]# docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp

2)运行nginx容器

[aaa@qq.com ~]# 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:latest

3)运行mysql容器

[aaa@qq.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
[aaa@qq.com ~]# yum  -y  install  mysql
[aaa@qq.com ~]# mysql -u root  -p123.com  -h  127.0.0.1  -P  3306
MySQL [(none)]> create  database  name;
MySQL [(none)]> show  databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| name               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

4)运行php容器

[aaa@qq.com ~]# 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测试页面:

[aaa@qq.com html]# pwd
/wwwroot/html
[aaa@qq.com html]# cat  test.php 
<?php
phpinfo();
?>

5)修改nginx配置文件,nginx和php连接

[aaa@qq.com ~]# cd  /docker/nginx/conf.d/
[aaa@qq.com conf.d]# vim  default.conf
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;  //添加php解析

//打开此模块,并更改相应信息
    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

[aaa@qq.com conf.d]# docker  restart  nginx

//到此,去浏览器验证,nginx服务和php服务界面

Docker部署LNMP环境

 

Docker部署LNMP环境

说明nginx和php的来连接,没有问题,接下来是php和mysql的连接,在这我们使用一个phpMyAdmin的数据库管理工具

[aaa@qq.com html]# pwd
/wwwroot/html
[aaa@qq.com html]# unzip phpMyAdmin-4.9.1-all-languages.zip
[aaa@qq.com html]# mv  phpMyAdmin-4.9.1-all-languages  phpmyadmin

//更改nginx的配置文件

[aaa@qq.com ~]# cd  /docker/nginx/conf.d/
[aaa@qq.com conf.d]# pwd
/docker/nginx/conf.d
[aaa@qq.com conf.d]# vim  default.conf
//在27行添加
    location  /phpmyadmin {
        root  /usr/share/nginx/html;
        index   index.html  index.htm  index.php;
    }

//在43行添加
    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

[aaa@qq.com conf.d]# docker  restart  nginx

//验证php主界面

Docker部署LNMP环境

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

写一个Docker

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
[aaa@qq.com ~]# docker build  -t  phpmysql  .

 

//删除之前的php容器,并用我们新制作的php镜像重新运行

[aaa@qq.com ~]# docker stop  phpfpm 
[aaa@qq.com ~]# docker rm  phpfpm 
[aaa@qq.com ~]# docker run  -itd  --name  phpfpm  -p 9000:9000  -v  /wwwroot/html/:/usr/share/nginx/html  --network lnmp  --ip 172.16.10.30  phpmysql:latest

//修改phpmyadmin的配置文件,指定连接的数据库的IP,然后重启php容器

[aaa@qq.com ~]# cd  /wwwroot/html/phpmyadmin/
[aaa@qq.com phpmyadmin]# pwd
/wwwroot/html/phpmyadmin
[aaa@qq.com phpmyadmin]# cp  config.sample.inc.php  config.inc.php
[aaa@qq.com phpmyadmin]# vim  config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.10.20';    //修改,指定数据库的IP地址
[aaa@qq.com ~]# docker restart  phpfpm 

用户名:root

密码:123.com

Docker部署LNMP环境

//登录成功之后会看到我们之前创建的数据库

Docker部署LNMP环境