Docker部署LNMP环境

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

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

1)创建一个自定义网络

[root@localhost ~]# docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp

2)运行nginx容器

[root@localhost ~]# 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容器

[root@localhost ~]# docker run  --name  mysql  -e  MYSQL_ROOT_PASSWORD=123.com  -d  -p  3306:3306  --network  lnmp  --ip  172.16.10.20  mysql:5.7
[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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| name               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

4)运行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]# cat  test.php 

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

[root@localhost ~]# cd  /docker/nginx/conf.d/
[root@localhost 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

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

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

Docker部署LNMP环境_第1张图片

 

Docker部署LNMP环境_第2张图片

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

[root@localhost html]# pwd
/wwwroot/html
[root@localhost html]# unzip phpMyAdmin-4.9.1-all-languages.zip
[root@localhost html]# mv  phpMyAdmin-4.9.1-all-languages  phpmyadmin

//更改nginx的配置文件

[root@localhost ~]# cd  /docker/nginx/conf.d/
[root@localhost conf.d]# pwd
/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;
    }

//在43行添加
    location ~ /phpmyadmin/(?(.*)\.(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

//验证php主界面

Docker部署LNMP环境_第3张图片

//需要我们对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
[root@localhost ~]# docker build  -t  phpmysql  .

 

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

[root@localhost ~]# docker stop  phpfpm 
[root@localhost ~]# docker rm  phpfpm 
[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:latest

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

[root@localhost ~]# cd  /wwwroot/html/phpmyadmin/
[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
$cfg['Servers'][$i]['host'] = '172.16.10.20';    //修改,指定数据库的IP地址
[root@localhost ~]# docker restart  phpfpm 

用户名:root

密码:123.com

Docker部署LNMP环境_第4张图片

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

Docker部署LNMP环境_第5张图片

你可能感兴趣的:(Docker)