docker-compose部署LNMP
admin
2023-03-27 03:21:14
0

实验前准备:
下载:
[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 d92e9bee

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

//复制配置文件
[root@localhost ~]# mkdir -p compose-lnmp/docker/
[root@localhost ~]# cd compose-lnmp/
[root@localhost compose-lnmp]# mkdir wwwroot

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

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

//设置tab键的空格数量[root@localhost ~]# vim  .vimrc  
br/>[root@localhost ~]# vim  .vimrc  
br/>[root@localhost ~]# cat .vimrc   

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

  • 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 network

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

//修改nginx配置文件,nginx和php连接[root@localhost compose-lnmp]# cd  docker/nginx/conf.d/  
br/>[root@localhost compose-lnmp]# cd  docker/nginx/conf.d/  
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  

//php和mysql连接[root@localhost compose-lnmp]# cd  wwwroot/html/  
br/>[root@localhost compose-lnmp]# cd  wwwroot/html/  
[root@localhost html]# mv  phpMyAdmin-4.9.1-all-languages  phpmyadmin  

//更改nginx配置文件[root@localhost compose-lnmp]# cd  docker/nginx/conf.d/  
br/>[root@localhost compose-lnmp]# cd  docker/nginx/conf.d/  
//在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;  
}  

//重启
[root@localhost conf.d]# docker-compose  restart  

//需要对php镜像做出更改,添加php和mysql连接的模块写一个Dockerfile
[root@localhost ~]# vim  Dockerfile  
br/>写一个Dockerfile
[root@localhost ~]# vim  Dockerfile  
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文件,并重新运行[root@localhost compose-lnmp]# docker-compose  stop  
br/>[root@localhost compose-lnmp]# docker-compose  stop  
br/>[root@localhost compose-lnmp]# vim  docker-compose.yml 
image: phpmysql  
[root@localhost compose-lnmp]# docker-compose  up  -d  

//修改phpmyadmin的配置文件,指定连接数据库的IP,然后重启[root@localhost compose-lnmp]# cd  wwwroot/html/phpmyadmin/  
br/>[root@localhost compose-lnmp]# cd  wwwroot/html/phpmyadmin/  
br/>[root@localhost phpmyadmin]# vim  config.inc.php  
[root@localhost phpmyadmin]# cd  -  
br/>$cfg['Servers'][$i]['host'] = '172.16.10.20';  
[root@localhost phpmyadmin]# cd  -  
[root@localhost compose-lnmp]# docker-compose  restart  

//再次访问
用户名:root
密码:123.com

相关内容

热门资讯

特朗普下令不打了,美国的“余粮... 可以说戛然而止,快得让很多人都无法适应。前两周,每一天美国都要打击伊朗一通。就在7月24日白宫会议前...
穆杰塔巴最新表态:支持黎真主党 △伊朗最高领袖穆杰塔巴·哈梅内伊(资料图)当地时间7月26日晚,伊朗最高领袖穆杰塔巴·哈梅内伊对黎巴...
美军瘫痪一艘驶向伊朗的油轮,特... 美国总统特朗普虽下令暂停打击伊朗,但美军仍在执行对霍尔木兹海峡的封锁任务。针对美军瘫痪一艘驶向伊朗油...
“小牌大耍”的底气到底是哪里来... 澎湃首席评论员 李平这两天,“艺人李权哲乘高铁占座事件”在网上引发热议,该艺人参加的活动主办方曾发布...
越南籍船舶南海遇险沉没,中方已... 据凤凰卫视报道,一艘越南船舶7月24日夜间在越南庆和以东220海里附近遇险沉没,船上共62名越南籍人...
李权哲占座、蒙面、装死:娱乐圈... 你买了一张高铁一等座的票,找到自己的位置,18A。有人坐着。你拿出手机看了眼购票记录——没错,18A...
杭州景区咖啡店闯入1条幼蛇,仅... 夏天气温高、湿度大,蛇类活动进入高峰期,千万别被“小蛇无害”的刻板印象骗了!有些幼蛇看着迷你可爱,实...
AI开源公开信签署企业增至50... IT之家 7 月 26 日消息,英伟达、微软、IBM、Meta 等 25 家美国科技企业本周(7 月...
南阳快手短视频代运营企业的服务... 在短视频平台快速发展的背景下,企业通过快手等渠道进行内容运营已成为常见的市场动作。对于希望借助短视频...
普通人该如何跟上科技发展的步伐 这个被称作科技的词汇, 听起来既有宏大之感, 又颇为遥远, 好似仅仅归属于在实验室里专心致志钻研的科...