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
浏览器访问出下面就成功:
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
[[email protected] ~]# yum -y install MySQL
访问验证:
[[email protected] ~]# MySQL -u root -p123.com -h 127.0.0.1 -P 3306
随便新建一个库做验证:
MySQL [(none)]> create database name;
再查看有没有刚创建的库:
MySQL [(none)]> show databases;
4)运行 PHP 容器:
运行一个 PHP 容器
[[email protected] ~]# 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 测试界面:
- [[email protected] HTML]# pwd
- /wwwroot/HTML
- [[email protected] HTML]# VIM test.PHP
- <?PHP
- phpinfo();
- ?>
浏览器访问: 出现下面界面就成功
5)修改 nginx 配置文件, nginx 和 PHP 连接
- [[email protected] conf.d]# pwd
- /docker/nginx/conf.d
- [[email protected] 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 服务:
- [[email protected] conf.d]# docker restart nginx
- nginx
关闭防火墙:
- [[email protected] conf.d]# systemctl stop firewalld
- [[email protected] conf.d]# systemctl disable firewalld
重启 docker:
- [[email protected] conf.d]# systemctl daemon-reload
- [[email protected] conf.d]# systemctl restart docker
重启所有容器:
[[email protected] conf.d]# docker ps -a -q | xargs docker start
接下来是 PHP 和 MySQL 的连接, 在这里我们使用一个 phpMyAdmin 的数据库管理工具.
下载导入 phpMyAdmin-4.9.1-all-languages
导入需要的包:
- [[email protected] HTML]# pwd
- /wwwroot/HTML
- [[email protected] HTML]# ls
- 50x.HTML phpMyAdmin-4.9.1-all-languages.zip
index.HTML test.PHP
解压:
- [[email protected] HTML]# unzip phpMyAdmin-4.9.1-all-languages.zip
- [[email protected] HTML]# ls
- 50x.HTML phpMyAdmin-4.9.1-all-languages test.PHP
- index.HTML phpMyAdmin-4.9.1-all-languages.zip
名字太长不好记, 待会要在 nginx 配置文件更改, 重命名一下:
- [[email protected] HTML]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin
- [[email protected] HTML]# ls
50x.HTML phpmyadmin test.PHP
- index.HTML phpMyAdmin-4.9.1-all-languages.zip
- // 更改 nginx 配置文件:
- [[email protected] HTML]# cd /docker/nginx/conf.d/
- [[email protected] 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:
- [[email protected] conf.d]# docker restart nginx
- nginx
浏览器访问
本机 ip/phpmyadmin/index.PHP
报红框属于正常现象, 不要惊慌, 接下来就解决它
需要我们对 PHP 镜像做出更改, 添加 PHP 和 MySQL 连接的模块
写一个 Dockerfile:
- [[email protected] ~]# 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
执行一下: 这条命令加载时间有点长, 请耐心等待!!!
[[email protected] ~]# docker build -t phpmysql .
查看是否有新创建的镜像:
- [[email protected] ~]# docker images
- [[email protected] ~]# docker stop phpfpm
我的是这个:
phpmysql latest 6e65aee3bd4d 9 minutes ago 422MB
关闭原先的 PHP 镜像:
- [[email protected] ~]# docker stop phpfpm
- phpfpm
删除之前的 PHP 容器
- [[email protected] ~]# docker rm phpfpm
- phpfpm
使用新创建的 PHP 镜像创建新的 PHP 容器
- [[email protected] ~]# 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
- [[email protected] phpmyadmin]# pwd
- /wwwroot/HTML/phpmyadmin
改名:
[[email protected] phpmyadmin]# cp config.sample.inc.PHP config.inc.PHP
- [[email protected] phpmyadmin]# docker restart phpfpm
- phpfpm
来源: http://www.bubuko.com/infodetail-3341735.html