docker-compose部署LNMP

實驗前準備:
下載:
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname -s-uname -m` -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose -v
docker-compose version 1.25.1-rc1, build d92e9beephp

//導入鏡像
[root@localhost ~]# docker  load   <  nginx.tar  &&  docker  load <  php.7.2-fpm.tar  &&  docker load  <  mysql-5.7.tar html

//複製配置文件
[root@localhost ~]# mkdir -p compose-lnmp/docker/
[root@localhost ~]# cd compose-lnmp/
[root@localhost compose-lnmp]# mkdir wwwrootmysql

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

//添加php測試界面br/>[root@localhost ~]# vim  /root/compose-lnmp/wwwroot/html/test.php  
[root@localhost ~]# cat /root/compose-lnmp/wwwroot/html/test.php  
<?php  
phpinfo();  
?>  git

//設置tab鍵的空格數量br/>[root@localhost ~]# vim  .vimrc  
[root@localhost ~]# source  .vimrc   br/>[root@localhost ~]# cat .vimrc   
set  tabstop=2  github

//編寫docker-compose.yml文件br/>[root@localhost ~]# cd  /root/compose-lnmp/  
[root@localhost compose-lnmp]# vim  docker-compose.yml  
version: "3.1"
services:
nginx:
container_name: nginx
image: nginx
networks:
lnmp:
ipv4_address: 172.16.10.10
restart: always
ports:sql

  • 80:80
    volumes:
  • /root/compose-lnmp/wwwroot/html:/usr/share/nginx/html
  • /root/compose-lnmp/docker/nginx:/etc/nginx
    mysql:
    container_name: mysql
    image: mysql:5.7
    networks:
    lnmp:
    ipv4_address: 172.16.10.20
    restart: always
    ports:
  • 3306:3306
    environment:
    MYSQL_ROOT_PASSWORD: 123.com
    php:
    container_name: phpfpm
    image: php:7.2-fpm
    networks:
    lnmp:
    ipv4_address: 172.16.10.30
    restart: always
    ports:
  • 9000:9000
    volumes:
  • /root/compose-lnmp/wwwroot/html:/usr/share/nginx/html
    networks:
    lnmp:
    driver: bridge
    ipam:
    config:
    • subnet: 172.16.10.0/24

[root@localhost ~]# echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
[root@localhost ~]# systemctl restart networkdocker

[root@localhost compose-lnmp]# docker-compose  up  -d  數據庫

//修改nginx配置文件,nginx和php鏈接br/>[root@localhost compose-lnmp]# cd  docker/nginx/conf.d/  
[root@localhost conf.d]# vim  default.conf  
10行: 
   location / {  
        root   /usr/share/nginx/html;  
       index  index.html index.htm index.php;  //添加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;  
}  
//重啓
[root@localhost conf.d]# docker-compose  restart  vim

//php和mysql鏈接br/>[root@localhost compose-lnmp]# cd  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配置文件br/>[root@localhost compose-lnmp]# cd  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/(?<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@localhost conf.d]# docker-compose  restart  

//須要對php鏡像作出更改,添加php和mysql鏈接的模塊br/>寫一個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
[root@localhost ~]# docker build  -t  phpmysql  .  

//刪除容器,更改docker-compose.yml文件,並從新運行br/>[root@localhost compose-lnmp]# docker-compose  stop  
[root@localhost compose-lnmp]# docker-compose  rm  br/>[root@localhost compose-lnmp]# vim  docker-compose.yml 
//將php使用的鏡像改成剛纔建立的鏡像
image: phpmysql  
[root@localhost compose-lnmp]# docker-compose  up  -d  

//修改phpmyadmin的配置文件,指定鏈接數據庫的IP,而後重啓br/>[root@localhost compose-lnmp]# cd  wwwroot/html/phpmyadmin/  
[root@localhost phpmyadmin]# cp  config.sample.inc.php   config.inc.php   br/>[root@localhost phpmyadmin]# vim  config.inc.php  
31行:br/>$cfg['Servers'][$i]['host'] = '172.16.10.20';  
[root@localhost phpmyadmin]# cd  -  
/root/compose-lnmp  
[root@localhost compose-lnmp]# docker-compose  restart  

//再次訪問用戶名:root密碼:123.com

相關文章
相關標籤/搜索