• docker部署lnmp环境


    前期准备:

    • 系统环境:centos7
    • 软件环境:docker:18.09.6
    • lnmp环境:nginx:1.14 ,mysql:5.7.22,php:7.2

    注:软件版本可自选

    一、部署mysql

    1.1 获取 Mysql 5.7.22 镜像

    [root@localhost ~]# docker pull mysql:5.7.22
    5.7.22: Pulling from library/mysql
    be8881be8156: Pull complete 
    ......
    c3995dabd1d7: Pull complete 
    2b3c2e6bacee: Pull complete 
    Digest: sha256:aaba540cdd9313645d892f4f20573e8b42b30e5be71c054b7befed2f7da5f85b
    Status: Downloaded newer image for mysql:5.7.22
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.2 启动mysql容器

    • 创建mysql挂载目录
    [root@localhost ~]# mkdir -p /opt/app/mysql/mysql
    
    • 1
    • 编写mysql挂载配置文件
    [root@localhost ~]# vim  /opt/app/mysql/my.cnf
    
    [client]
    port            = 3306
    socket          = /tmp/mysql.sock
    [mysqld]
    datadir=/var/lib/mysql
    socket=/tmp/mysql.sock
    port=3306
    sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    long_query_time = 5
    slow_query_log = 1
    slow_query_log_file = slow.log
    max_connections=3000
    skip-name-resolve
    back_log=384
    log-bin=mysql-bin
    binlog_format=mixed
    expire_logs_days = 7
    server-id = 123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 编写mysql容器启动脚本
    [root@localhost ~]# cd /opt/app/mysql/
    [root@localhost mysql]# ls
    my.cnf  mysql
    [root@localhost mysql]# vim mysql-run.sh 
    #!/bin/bash
    
    docker run -itd --restart=unless-stopped -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone --name mysql -p 13306:3306 -v /opt/app/mysql/mysql:/var/lib/mysql -v /opt/app/mysql/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.22
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 为启动脚本赋予执行权限
    [root@localhost mysql]# chmod +x mysql-run.sh 
    
    [root@localhost mysql]# ll mysql-run.sh 
    -rwxr-xr-x 1 root root 271 8月   6 15:10 mysql-run.sh
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 启动mysql数据库容器
    [root@localhost mysql]# pwd
    /opt/app/mysql
    
    [root@localhost mysql]# ./mysql-run.sh 
    c6d5d69bcede92f6b5ea685af50277924c76ca04b7808bc7dda48292a1b862b9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 查看mysql容器启动情况
    [root@localhost php]# docker ps  -a --no-trunc --filter name=^mysql$
    CONTAINER ID                                                       IMAGE               COMMAND                         CREATED             STATUS              PORTS                     NAMES
    c6d5d69bcede92f6b5ea685af50277924c76ca04b7808bc7dda48292a1b862b9   mysql:5.7.22        "docker-entrypoint.sh mysqld"   18 minutes ago      Up 18 minutes       0.0.0.0:13306->3306/tcp   mysql,php/mysql
    
    • 1
    • 2
    • 3

    二、部署php

    2.1 获取php 7.2镜像

    [root@localhost mysql]# docker pull php:7.2-fpm
    7.2-fpm: Pulling from library/php
    f5d23c7fed46: Pull complete 
    ......
    8d5ce70a71c0: Pull complete 
    Digest: sha256:e52c0cd1487d76decf8523da5b0a65f2cfd47d8bbbfef077476fff90ceae2fa8
    Status: Downloaded newer image for php:7.2-fpm
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.2 启动php 容器

    • 创建php存放文件目录
    [root@localhost ~]# mkdir -p /opt/app/php
    
    • 1
    • 编写php容器启动脚本
    [root@localhost ~]# cd /opt/app/php/
    [root@localhost php]# vim php-run.sh
    #!/bin/bash
    
    docker run -d -v /opt/app/nginx/html:/var/www/html -p 9000:9000 --link mysql:mysql --name php php:7.2-fpm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 为启动脚本赋予执行权限
    [root@localhost php]# chmod +x php-run.sh 
    
    [root@localhost php]# ll php-run.sh 
    -rwxr-xr-x 1 root root 120 8月   6 15:28 php-run.sh
    
    • 1
    • 2
    • 3
    • 4
    • 启动php容器
    [root@localhost php]# ./php-run.sh 
    7750e24826b74c8fb355cf2abfa27d4eb8a7104672728ad18cd75332e62e2fb9
    
    • 1
    • 2
    • 查看php容器启动情况
    [root@localhost php]# docker ps  -a --no-trunc --filter name=^php$
    CONTAINER ID                                                       IMAGE               COMMAND                           CREATED             STATUS              PORTS                    NAMES
    7750e24826b74c8fb355cf2abfa27d4eb8a7104672728ad18cd75332e62e2fb9   php:7.2-fpm         "docker-php-entrypoint php-fpm"   54 seconds ago      Up 53 seconds       0.0.0.0:9000->9000/tcp   php
    
    • 1
    • 2
    • 3

    php容器启动参数说明

    -d 让容器运行在后台

    -p 添加主机到容器的端口映射

    -v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一定非要一样,如果不一样在配置nginx的时候需要注意

    -name 容器的名称

    -link 容器之间建立起来联系 (需要建立联系的容器名:mysql)

    2.3 php的扩展安装

    • 安装php-redis

    进入php容器
    docker exec -it php bash

    安装php-redis命令
    pecl install redis && docker-php-ext-enable redis

    [root@localhost ~]# docker exec -it php bash
    root@7750e24826b7:/var/www/html# pecl install redis && docker-php-ext-enable redis
    downloading redis-5.0.2.tgz ...
    Starting to download redis-5.0.2.tgz (243,270 bytes)
    ..................................................done: 243,270 bytes
    25 source files, building
    running: phpize
    Configuring for:
    PHP Api Version:         20170718
    Zend Module Api No:      20170718
    Zend Extension Api No:   320170718
    enable igbinary serializer support? [no] :  //no  或者空格都可以
    enable lzf compression support? [no] :      //no  或者空格都可以
    building in /tmp/pear/temp/pear-build-defaultuser9cKHej/redis-5.0.2
    running: /tmp/pear/temp/redis/configure 
    ......此处省略
    configuration option "php_ini" is not set to php.ini location
    You should add "extension=redis.so" to php.ini
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 查看安装的php插件
      容器内执行:php -m
    root@7750e24826b7:/var/www/html# php -m
    [PHP Modules]
    Core
    ctype
    curl
    date
    dom
    fileinfo
    filter
    ftp
    hash
    iconv
    json
    libxml
    mbstring
    mysqlnd
    openssl
    pcre
    PDO
    pdo_sqlite
    Phar
    posix
    readline
    redis   //这就是安装的redis
    Reflection
    session
    SimpleXML
    sodium
    SPL
    sqlite3
    standard
    tokenizer
    xml
    xmlreader
    xmlwriter
    zlib
    
    [Zend Modules]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    三、部署nginx

    3.1 获取nginx:1.14镜像

    [root@localhost ~]# docker pull nginx:1.14
    1.14: Pulling from library/nginx
    27833a3ba0a5: Pull complete 
    0f23e58bd0b7: Pull complete 
    8ca774778e85: Pull complete 
    Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Status: Downloaded newer image for nginx:1.14
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.2 启动nginx容器

    • 新建nginx挂载目录
    [root@localhost ~]# mkdir -p /opt/app/nginx/{conf,html,logs,cert}
    
    • 1
    • 编写nginx主配置文件
    [root@localhost ~]# cd /opt/app/nginx/
    
    [root@localhost nginx]# vim nginx.conf
    
    worker_processes  4;
    user  nginx;
    #pid /opt/app/nginx/sbin/nginx.pid;
    events {
        worker_connections  409600;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_tokens  off;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log /var/log/nginx/access.log  main;
        error_log  /var/log/nginx/error.log  error;
        include /etc/nginx/conf.d/*.conf;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 编写nginx容器启动脚本
    [root@localhost nginx]# pwd
    /opt/app/nginx
    
    
    [root@localhost nginx]# vim  nginx-run.sh
    #!/bin/bash
    
    docker run -itd --restart=unless-stopped  -v /etc/localtime:/etc/localtime  -v /etc/timezone:/etc/timezone  -p 80:80  --name nginx  -v /opt/app/nginx/html:/usr/share/nginx/html  -v /opt/app/nginx/logs:/var/log/nginx  -v /opt/app/nginx/cert:/etc/nginx/cert  -v /opt/app/nginx/nginx.conf:/etc/nginx/nginx.conf  -v /opt/app/nginx/conf:/etc/nginx/conf.d --link php:phpfpm nginx:1.14
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    参数解释:

    -v 添加目录映射,这里nginx容器的静态页目录最好和php容器中静态页目录映射的目录一致。但是不一定非要一样,如果不一样在配置nginx的时候需要注意

    –link 容器之间建立起来联系,此处nginx需要与php容器建立联系(写法:php容器名称:phpfpm),此参数必须写在使用镜像名称之前。

    • 为启动脚本赋予执行权限
    [root@localhost nginx]# chmod +x nginx-run.sh 
    
    [root@localhost nginx]# ll nginx-run.sh 
    -rwxr-xr-x 1 root root 393 8月   6 16:28 nginx-run.sh
    
    • 1
    • 2
    • 3
    • 4
    • 启动nginx容器
    [root@localhost nginx]# ./nginx-run.sh 
    850e27b026be83886f2deee90d8b36754eb29eeffd1e27e18a6e5724bc29f3f7
    
    • 1
    • 2
    • 查看nginx容器启动情况
    [root@localhost nginx]# docker ps -a --no-trunc --filter name=nginx$
    CONTAINER ID                                                       IMAGE               COMMAND                    CREATED             STATUS              PORTS                NAMES
    850e27b026be83886f2deee90d8b36754eb29eeffd1e27e18a6e5724bc29f3f7   nginx:1.14          "nginx -g 'daemon off;'"   30 seconds ago      Up 29 seconds       0.0.0.0:80->80/tcp   nginx
    
    • 1
    • 2
    • 3

    3.3 编写nginx虚拟主机配置文件,使其支持php

    [root@localhost conf]# pwd
    /opt/app/nginx/conf
    
    
    [root@localhost conf]# vim nginx-php.conf 
    
    server {
            listen       80;
            server_name  10.20.29.36;
            location / {
                root /usr/share/nginx/html;
                index  index.php index.html index.htm;
            }
            location ~ \.php$ {
                 root    html;
                 fastcgi_pass 10.20.29.36:9000;
                 fastcgi_index index.php;
                 fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
                 include fastcgi_params;
            }
            access_log  /var/log/nginx/access.php.log  main;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    注意!!!

    location / {
                root /usr/share/nginx/html;   //修改这里的默认路径为nginx静态页面路径
    
    • 1
    • 2
    fastcgi_pass 10.20.29.93:9000;   
    #这里可以用容器ID,容器名称,容器IP+端口或者宿主机IP+端口,都可以找到php服务
    即:fastcgi_pass 7750e24826b7/php/172.17.0.3/10.20.29.93:9000;   
    
    • 1
    • 2
    • 3
    • 进入到容器里面重新加载配置文件
    [root@localhost conf]# docker exec -it nginx bash   #进入nginx容器
    root@850e27b026be:/# nginx -t    #nginx检测配置文件是否正确
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    root@850e27b026be:/# nginx -s reload   #平滑重启nginx,不影响nginx服务使用
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.4 php测试静态页编写

    在主机/opt/app/nginx/html 目录下创建php测试页面,会直接映射到容器里面

    [root@localhost html]# pwd
    /opt/app/nginx/html
    
    [root@localhost html]# vim index.php
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、访问浏览器测试

    看到php静态测试页面说明搭建成功了!

  • 相关阅读:
    TestDeploy v3.0构思
    Redis常用命令补充和持久化
    关于Go语言的底层,Channel
    时光倒流-第12届蓝桥杯Scratch选拔赛真题精选
    MySQL占用内存过大解决方案
    电子邮件发送接收原理(附 go 语言实现发送邮件)
    单机点:Redis可以连接,但是无法写入数据
    面试中常见的算法题和其python实现
    mysql中自定义变量(浅显易懂简洁版)
    Vioovi ECRS工时分析软件:实现生产线平衡的有效工具
  • 原文地址:https://blog.csdn.net/cljdsc/article/details/134430540