Docker deployment lnmp cluster
The IP address range of LNMP is:
172.16.10.0/24
Service container and each ip:
Nginx 172.16.10.10
Mysql 172.16.10.20
Php 172.16.10.30 *
Import the image package:
Mysql5.7
Php.7.2-fpm.tar
Wordpress.tar
Nginx.tar
Import the imported image package into an image available to 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) run mysql:
Run a mysql container and create a password of 123.com for it:
Docker run-- name mysql-e MYSQL_ROOT_PASSWORD=123.com-d-p 3306 MYSQL_ROOT_PASSWORD=123.com-- network lnmp-- ip 172.16.10.20 mysql:5.7
Log in to mysql to verify:
Download a mysql first
[root@localhost ~] # yum-y install mysql
Access authentication:
[root@localhost] # mysql-u root-p123.com-h 127.0.0.1-P 3306
Randomly create a new library for verification:
MySQL [(none)] > create database name
Then check to see if there is a library you just created:
MySQL [(none)] > show databases
4) run the php container:
Run a php container
[root@localhost] # docker run-itd-- name phpfpm-p 9000itd 9000-v / wwwroot/html:/usr/share/nginx/html-- network lnmp-- ip 172.16.10.30 php:7.2-fpm
Add the php test interface:
[root@localhost html] # pwd
/ wwwroot/html
[root@localhost html] # vim test.php
Browser access: successful when the following interface appears
5) modify nginx configuration file, nginx and php connection
[root@localhost conf.d] # pwd
/ docker/nginx/conf.d
[root@localhost conf.d] # vim default.conf
Line 10: add index.php parsing
index index.html index.htm index.php
Line 30: open the module (remove # and add) and change the phase information:
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
}
Restart the nginx service:
[root@localhost conf.d] # docker restart nginx
Nginx
Turn off the firewall:
[root@localhost conf.d] # systemctl stop firewalld
[root@localhost conf.d] # systemctl disable firewalld
Restart docker:
[root@localhost conf.d] # systemctl daemon-reload
[root@localhost conf.d] # systemctl restart docker
Restart all containers:
[root@localhost conf.d] # docker ps-a-Q | xargs docker start
Next is the connection between php and mysql, where we use a phpMyAdmin database management tool.
Download and import phpMyAdmin-4.9.1-all-languages
Import the required packages:
[root@localhost html] # pwd
/ wwwroot/html
[root@localhost html] # ls
50x.html phpMyAdmin-4.9.1-all-languages.zip
Index.html test.php
Decompress:
[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
The name is too long to remember. It will be changed in the nginx configuration file later. Rename it:
[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
/ / change the nginx configuration file:
[root@localhost html] # cd / docker/nginx/conf.d/
[root@localhost conf.d] # vim default.conf
27 lines add:
Location / phpmyadmin {root / usr/share/nginx/html; index index.html index.htm index.php;}
41 lines add:
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
}
Restart nginx:
[root@localhost conf.d] # docker restart nginx
Nginx
Browser access
Native ip/phpmyadmin/index.php
The red box is a normal phenomenon, don't panic, and then solve it.
We need to make changes to the php image to add modules for php and mysql connections
Write a 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
Execute it: this command takes a long time to load, please wait patiently!
[root@localhost] # docker build-t phpmysql.
Check to see if there are any newly created images:
[root@localhost ~] # docker images
[root@localhost ~] # docker stop phpfpm
Mine is this:
Phpmysql latest 6e65aee3bd4d 9 minutes ago 422MB
Turn off the previous php image:
[root@localhost ~] # docker stop phpfpm
Phpfpm
Delete the previous php container
[root@localhost ~] # docker rm phpfpm
Phpfpm
Create a new php container using the newly created php image
[root@localhost] # docker run-itd-- name phpfpm-p 9000itd 9000-v / wwwroot/html:/usr/share/nginx/html-- network lnmp-- ip 172.16.10.30 phpmysql
/ / modify the configuration file of phpmyadmin to specify the ip of the connected database
[root@localhost phpmyadmin] # pwd
/ wwwroot/html/phpmyadmin
Rename:
[root@localhost phpmyadmin] # cp config.sample.inc.php config.inc.php
Edit:
[root@localhost phpmyadmin] # vim config.inc.php
Line 31: change to ip of mysql database
$cfg ['Servers'] [$I] [' host'] = '172.16.10.20'
Restart the php container:
[root@localhost phpmyadmin] # docker restart phpfpm
Phpfpm
Browser access: native ip/phpmyadmin/index.php
The following interface appears successfully
User: root password: 123.com
If you log in successfully, you will see the database you created earlier:
At this point, lnmp deployment is complete!