1.网络规划
172.16.10.0/24
nginx:172.16.10.10
mysql:172.16.10.20
php:172.16.10.30
网站访问主目录:/wwwroot
nginx的配置文件:/docker
mysql持久化的目录:/mysql
2.docker运行nginx服务,拷贝出所需文件后删除
[root@docker ~]# mkdir /docker
[root@docker ~]# mkdir /wwwroot
[root@docker ~]# docker run -itd --name test nginx
[root@docker ~]# docker cp test:/etc/nginx /docker/
[root@docker ~]# docker cp test:/usr/share/nginx/html /wwwroot/
[root@docker ~]# docker rm -f test
3.创建自定义网络
[root@docker ~]# docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp
4.运行nginx容器
[root@docker ~]# docker run -itd --name nginx -p 80:80 -v /wwwroot/html:/usr/share/nginx/html -v /docker/nginx:/etc/nginx --network lnmp --ip 172.16.10.10 nginx
5.运行mysql容器
[root@docker html]# docker pull mysql:5.7
[root@docker html]# docker run -itd --name test -e MYSQL_ROOT_PASSWORD=asd123 mysql:5.7
[root@docker html]# docker exec -it test bash
root@d46a7cc8407f:/# mysql -uroot -pasd123
mysql> show databases;
mysql> create database test;
mysql> show databases;
mysql> exit
root@d46a7cc8407f:/# cd /var/lib/mysql
root@d46a7cc8407f:/var/lib/mysql# ls
root@d46a7cc8407f:/var/lib/mysql# exit
[root@docker html]# docker cp test:/var/lib/mysql /
[root@docker html]# ls /mysql/
[root@docker html]# docker run -itd --name mysql -p 3306:3306 -v /mysql:/var/lib/mysql --network lnmp --ip 172.16.10.20 mysql:5.7
[root@docker html]# yum -y install mariadb
[root@docker html]# mysql -uroot -pasd123 -h 127.0.0.1
6.运行php容器
[root@docker ~]# docker run -itd --name php-fpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 php:7.2-fpm
1.编写nginx配置文件
[root@docker ~]# cd /docker/nginx/conf.d
[root@docker conf.d]# vim default.conf
location / {
root /usr/share/nginx/html;
index index.html index.htm index.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;
}
2.浏览器验证nginx服务和php服务
(1)添加nginx静态的测试界面
[root@docker ~]# cd /wwwroot/html/
[root@docker html]# echo 123 > index.html
[root@docker html]# curl 127.0.0.1
(2)添加php测试界面
[root@docker html]# vim index.php
<?php
phpinfo();
?>
[root@docker html]# docker restart nginx
访问
http://192.168.229.187
http://192.168.229.187/index.php
1.测试连接
测试php和mysql的连接,需要使用一个phpMyAdmin的数据库管理工具。
zhangjiedeMacBook-Pro:~ zhangjie$ scp Desktop/phpMyAdmin-4.9.1-all-languages.zip [email protected]:/wwwroot/html
[root@docker html]# unzip phpMyAdmin-4.9.1-all-languages.zip
[root@docker html]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin
[root@docker html]# cd phpmyadmin/
[root@docker phpmyadmin]# cp config.sample.inc.php config.inc.php
[root@docker phpmyadmin]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.10.20'; #改为数据库的IP
[root@docker phpmyadmin]# cd /docker/nginx/conf.d/
[root@docker conf.d]# vim default.conf
新添加
location /phpmyadmin {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
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;
}
[root@docker conf.d]# docker restart nginx
访问:http://192.168.229.187/phpmyadmin/index.php
2.修改php镜像
需要对php镜像做出更改,添加php和mysql连接的模块。
[root@docker ~]# 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@docker ~]# docker build -t php-mysql .
或者
zhangjiedeMacBook-Pro:~ zhangjie$ scp Desktop/phpmysql.tar [email protected]:/root
[root@docker ~]# docker load -i phpmysql.tar
3.删除之前的php容器,并用新制作的php镜像重新运行
[root@docker ~]# docker rm -f php-fpm
[root@docker ~]# docker run -itd --name php-fpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 php_mysql
访问:http://192.168.229.187/phpmyadmin/index.php
页面显示正常
登录账户:root
密码:数据库密码