基于Docker-compose.yml部署lnmp

安装docker-compose

[root@node1 ~]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0   1148      0 --:--:-- --:--:-- --:--:--  1148
100 12.1M  100 12.1M    0     0   980k      0  0:00:12  0:00:12 --:--:-- 1195k

[root@node1 ~]# chmod +x /usr/local/bin/docker-compose 
[root@node1 ~]# ll /usr/local/bin/docker-compose 
-rwxr-xr-x. 1 root root 12737304 89 08:22 /usr/local/bin/docker-compose

[root@node1 harbor]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

部署lnmp

创建目录

[root@localhost ~]# tree
.
├── anaconda-ks.cfg
└── compose_lnmp
    ├── docker-compose.yml
    └── php
        └── Dockerfile


编辑php/Dockerfile

  • 由于官方php-fpm镜像缺少一些扩展,所以要先用dockerfile构建新的镜像
  • 注意配置时区和mysql扩展
[root@node1 ~]# cd compose_lnmp
[root@node1 compose_lnmp]# cd php
[root@node1 php]# vim Dockerfile 

FROM php:7.0-fpm
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        libmemcached-dev \
        zlib1g-dev \
        libcurl4-openssl-dev \
        libxml2-dev \
        --no-install-recommends && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install -j$(nproc) \
        iconv mcrypt gettext curl mysqli pdo pdo_mysql zip \
        mbstring bcmath opcache xml simplexml sockets hash soap \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

CMD ["php-fpm", "-F"]

编写docker-compose.yml

[root@node1 compose_lnmp]# vim docker-compose.yml

version: "3"

services:
  mysql:
    hostname: mysql
    restart: always
    image: mysql:5.6
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - mysql-config:/etc/mysql
      - mysql-log:/var/log/mysql
      - mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  php:
    hostname: php
    restart: always
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    links:
      - mysql:mysql
    volumes:
      - nginx-html:/var/www/html
      - php-config:/usr/local/etc
  nginx:
    hostname: nginx
    restart: always
    container_name: nginx
    image: nginx:1.17.0
    ports:
      - "80:80"
      - "443:443"
    links:
      - "php:php"
    volumes:
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
      - nginx-html:/usr/share/nginx/html

volumes:
  mysql-config:
  mysql-log:
  mysql-data:
  nginx-html:
  php-config:
  nginx-config:
  nginx-log:

启动服务

//启动服务
[root@node1 compose_lnmp]# docker-compose up -d
Creating network "lnmp_default" with the default driver
Creating volume "lnmp_mysql-config" with default driver
Creating volume "lnmp_mysql-log" with default driver
Creating volume "lnmp_mysql-data" with default driver
Creating volume "lnmp_nginx-html" with default driver
Creating volume "lnmp_php-config" with default driver
Creating volume "lnmp_nginx-config" with default driver
……  #此处省略n行

//重启
[root@node1 compose_lnmp]# docker-compose restart

查看容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS                                                                      NAMES
2eb50baae92c   nginx:1.17.0       "nginx -g 'daemon of…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
98c303ebc703   compose_lnmp_php   "docker-php-entrypoi…"   3 minutes ago   Up 3 minutes   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                                  php
2fc31b02918e   mysql:5.6          "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp                                  mysql

数据化持久目录

[root@localhost ~]# docker volume ls
DRIVER    VOLUME NAME
local     compose_lnmp_mysql-config #数据库配置文件
local     compose_lnmp_mysql-data	#数据库数据文件
local     compose_lnmp_mysql-log	#数据库日志
local     compose_lnmp_nginx-config #nginx配置文件
local     compose_lnmp_nginx-html	#nginx web目录
local     compose_lnmp_nginx-log	#nginx日志目录
local     compose_lnmp_php-config	#php配置文件

配置php

//默认的php.ini文件是没有的,需要手动把模板配置文件复制为php.ini
[root@localhost ~]# cd /var/lib/docker/volumes/compose_lnmp_php-config/_data/php
[root@localhost php]# cp php.ini-production php.ini
[root@localhost php]# vim php.ini

date.timezone = Asia/Shanghai  //取消注释添加值为Asia/Shanghai

配置nginx

[root@localhost ~]# vim /var/lib/docker/volumes/compose_lnmp_nginx-config/_data/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php; #此处添加index.php
    }


//将此段取消注释
   location ~ \.php$ {
        root           html;
        fastcgi_pass   php:9000;  #php为容器的名字
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name; 
        include        fastcgi_params;
    }

测试mysql

[root@localhost ~]# docker container exec -it mysql bash
root@mysql:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

验证php

//编写php界面
[root@localhost ~]# cd /var/lib/docker/volumes/compose_lnmp_nginx-html/_data/
[root@localhost _data]# vim index.php
//添加下列内容
<?php
   phpinfo();
?>

重启所有容器

[root@localhost compose_lnmp]# docker-compose restart
Restarting nginx ... done
Restarting php   ... done
Restarting mysql ... done
[root@localhost compose_lnmp]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:9000                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:3306                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:80                    0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                    0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:443                   0.0.0.0:*                      
LISTEN      0           128                       [::]:9000                     [::]:*                      
LISTEN      0           128                       [::]:3306                     [::]:*                      
LISTEN      0           128                       [::]:80                       [::]:*                      
LISTEN      0           128                       [::]:22                       [::]:*                      
LISTEN      0           128                       [::]:443                      [::]:*             

测试页面

请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值