说明

  • Docker-compose搭建lnmp(NGINX+MySQL+PHP+Redis)环境
  • Docker-compose搭建lnmp(NGINX+PHP+Redis)环境
  • Docker-compose网络

操作步骤

》lnmp环境(NGINX+MySQL+PHP+Redis)搭建

  • 创建目录和文件

    [admin@192 lnmp]$ tree
    .
    ├── docker-compose.yml
    ├── mysql57
    │   └── Dockerfile
    ├── nginx
    │   ├── conf
    │   │   ├── default.conf
    │   │   └── test.local.conf
    │   ├── Dockerfile
    │   └── www
    │       ├── default
    │       │   └── index.php
    │       ├── html
    │       └── test.local
    │           └── index.php
    ├── php74
    │   └── Dockerfile
    └── redis
        ├── conf
        │   └── redis.conf
        └── Dockerfile
    
    10 directories, 10 files
    
  • 配置docker-compose.yml

    version: '3.8'
    services:
      nginx:
        build: ./nginx
        ports:
          - "80:80"
        links:
          - "php74"
        volumes:
          - /data/lnmp/nginx/conf:/etc/nginx/conf.d
          - /data/lnmp/nginx/www:/var/www
      php74:
        build: ./php74
        ports:
          - "9000:9000"
        links:
          - "mysql57"
          - "redis"
        volumes:
          - /data/lnmp/nginx/www:/var/www
      mysql57:
        build: ./mysql57
        ports:
          - "3306:3306"
        volumes:
          - /data/lnmp/mysql57/data/:/var/lib/mysql/
        environment:
          MYSQL_ROOT_PASSWORD: 123456
      redis:
        build: ./redis
        ports:
          - "6379:6379"
        volumes:
          - /data/lnmp/redis/conf:/usr/local/etc/redis
          - /data/lnmp/redis/data:/data
    
  • 配置NGINX

    • Dockerfile

      FROM nginx:1.20.2
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      
    • conf/default.conf

      server {
          listen 80;
          server_name localhost;
      
          location / {
              root /var/www/default;
              index index.html index.htm index.php;
          }
      
          #error_page 404 /404.html;
      
          # redirect server error pages to the static page /50x.html
          #
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /var/www/html;
          }
          location ~ \.php$ {
              root /var/www/default;
              fastcgi_pass php74:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
      
    • conf/test.local.conf

      server {
          listen 80;
          server_name test.local;
      
          location / {
              root /var/www/test.local;
              index index.html index.htm index.php;
          }
      
          #error_page 404 /404.html;
      
          # redirect server error pages to the static page /50x.html
          #
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /var/www/html;
          }
          location ~ \.php$ {
              root /var/www/test.local;
              fastcgi_pass php74:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
      
    • www/default/index.php

      <?php
      //连接 MySQL 服务
      $dbms='mysql';     //数据库类型
      $host='mysql57';   //数据库主机名
      $dbName='mysql';   //使用的数据库
      $user='root';      //数据库连接用户名
      $pass='123456';    //对应的密码
      $dsn="$dbms:host=$host;dbname=$dbName";
      try {
          $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
          echo "MySQL连接成功<br/>";
          /*你还可以进行一次搜索操作
          foreach ($dbh->query('SELECT * from FOO') as $row) {
              print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
          }
          */
          $dbh = null;
      } catch (PDOException $e) {
          die ("Error!: " . $e->getMessage() . "<br/>");
      }
      //默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
      //$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
      
      echo "————————————————————\n";
      
      //连接 Redis 服务
      $redis = new Redis();
      $redis->connect('redis', 6379);
      echo "Connection to Redis server successfully";
      //查看服务是否运行
      echo "Server is running: " . $redis->ping();
      
    • www/test.local/index.php

      <?php
      echo "test.local...";
      
  • 配置PHP

    • Dockerfile

      FROM php:7.4-fpm
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      RUN apt-get update && apt-get install -y \
              libfreetype6-dev \
              libjpeg62-turbo-dev \
              libpng-dev \
          && docker-php-ext-configure gd --with-freetype --with-jpeg \
          && docker-php-ext-install -j$(nproc) gd pdo_mysql \
          && pecl install redis-5.1.1 \
          && pecl install xdebug-2.8.1 \
          && docker-php-ext-enable redis xdebug
      
  • 配置MySQL

    • Dockerfile

      FROM mysql:5.7
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      
  • 配置Redis

    • Dockerfile

      FROM redis:6.2.6
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      
    • conf/redis.conf

      port 6379 
      bind 0.0.0.0
      
  • 创建并启动服务

    sudo docker-compose up    # 前台启动
    sudo docker-compose up -d # 先前台启动没报错的话,可以后台启动
    
    sudo docker-compose ps    # 查看运行中的服务
    

》lnmp环境(NGINX+PHP+Redis)搭建

  • 创建目录和文件

    [admin@192 lnmp]$ tree
    .
    ├── docker-compose.yml
    ├── nginx
    │   ├── conf
    │   │   ├── default.conf
    │   │   └── test.local.conf
    │   ├── Dockerfile
    │   └── www
    │       ├── default
    │       │   └── index.php
    │       ├── html
    │       └── test.local
    │           └── index.php
    ├── php74
    │   └── Dockerfile
    └── redis
        ├── conf
        │   └── redis.conf
        └── Dockerfile
    
    9 directories, 9 files
    
  • 配置docker-compose.yml

    version: '3.8'
    services:
      nginx:
        build: ./nginx
        ports:
          - "80:80"
        links:
          - "php74"
        volumes:
          - /data/lnmp/nginx/conf:/etc/nginx/conf.d
          - /data/lnmp/nginx/www:/var/www
      php74:
        build: ./php74
        ports:
          - "9000:9000"
        links:
          - "redis"
        volumes:
          - /data/lnmp/nginx/www:/var/www
      redis:
        build: ./redis
        ports:
          - "6379:6379"
        volumes:
          - /data/lnmp/redis/conf:/usr/local/etc/redis
          - /data/lnmp/redis/data:/data
    
  • 配置NGINX

    • Dockerfile

      FROM nginx:1.20.2
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      
    • conf/default.conf

      server {
          listen 80;
          server_name localhost;
      
          location / {
              root /var/www/default;
              index index.html index.htm index.php;
          }
      
          #error_page 404 /404.html;
      
          # redirect server error pages to the static page /50x.html
          #
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /var/www/html;
          }
          location ~ \.php$ {
              root /var/www/default;
              fastcgi_pass php74:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
      
    • conf/test.local.conf

      server {
          listen 80;
          server_name test.local;
      
          location / {
              root /var/www/test.local;
              index index.html index.htm index.php;
          }
      
          #error_page 404 /404.html;
      
          # redirect server error pages to the static page /50x.html
          #
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root /var/www/html;
          }
          location ~ \.php$ {
              root /var/www/test.local;
              fastcgi_pass php74:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
      
    • www/default/index.php

      <?php
      //连接 MySQL 服务
      $dbms='mysql';       //数据库类型
      $host='192.168.3.1'; //数据库主机名
      $dbName='test';      //使用的数据库
      $user='root';        //数据库连接用户名
      $pass='123456';      //对应的密码
      $dsn="$dbms:host=$host;dbname=$dbName";
      try {
          $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
          echo "MySQL连接成功<br/>";
          /*你还可以进行一次搜索操作
          foreach ($dbh->query('SELECT * from FOO') as $row) {
              print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
          }
          */
          $dbh = null;
      } catch (PDOException $e) {
          die ("Error!: " . $e->getMessage() . "<br/>");
      }
      //默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
      //$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
      
      echo "————————————————————\n";
      
      //连接 Redis 服务
      $redis = new Redis();
      $redis->connect('redis', 6379);
      echo "Connection to Redis server successfully";
      //查看服务是否运行
      echo "Server is running: " . $redis->ping();
      
    • www/test.local/index.php

      <?php
      echo "test.local...";
      
  • 配置PHP

    • Dockerfile

      FROM php:7.4-fpm
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      RUN apt-get update && apt-get install -y \
              libfreetype6-dev \
              libjpeg62-turbo-dev \
              libpng-dev \
          && docker-php-ext-configure gd --with-freetype --with-jpeg \
          && docker-php-ext-install -j$(nproc) gd pdo_mysql \
          && pecl install redis-5.1.1 \
          && pecl install xdebug-2.8.1 \
          && docker-php-ext-enable redis xdebug
      
  • 配置Redis

    • Dockerfile

      FROM redis:6.2.6
      MAINTAINER yasin<cn.yasinyang@gmail.com>
      
    • conf/redis.conf

      port 6379 
      bind 0.0.0.0
      
  • 创建并启动服务

    sudo docker-compose up    # 前台启动
    sudo docker-compose up -d # 先前台启动没报错的话,可以后台启动
    
    sudo docker-compose ps    # 查看运行中的服务
    

》docker-compose网络推荐用法

--link标志是 Docker 的遗留功能。它最终可能会被删除。除非您绝对需要继续使用它,否则我们建议您使用 用户定义的网络 来促进两个容器之间的通信,而不是使用--link.

  • docker-compose.yml升级版01

    version: '3.8'
    services:
      nginx:
        build: ./nginx
        ports:
          - "80:80"
        volumes:
          - /data/lnmp/nginx/conf:/etc/nginx/conf.d
          - /data/lnmp/nginx/www:/var/www
        networks:
          - net01
      php74:
        build: ./php74
        ports:
          - "9000:9000"
        volumes:
          - /data/lnmp/nginx/www:/var/www
        networks:
          - net01
      redis:
        build: ./redis
        ports:
          - "6379:6379"
        volumes:
          - /data/lnmp/redis/conf:/usr/local/etc/redis
          - /data/lnmp/redis/data:/data
        networks:
          - net01
    networks:
      net01:
    
  • docker-compose.yml升级版02

    version: '3.8'
    services:
      nginx:
        build: ./nginx
        ports:
          - "80:80"
        volumes:
          - /data/lnmp/nginx/conf:/etc/nginx/conf.d
          - /data/lnmp/nginx/www:/var/www
        networks:
          net01:
            ipv4_address: 172.16.1.101
      php74:
        build: ./php74
        ports:
          - "9000:9000"
        volumes:
          - /data/lnmp/nginx/www:/var/www
        networks:
          net01:
            ipv4_address: 172.16.1.102
      redis:
        build: ./redis
        ports:
          - "6379:6379"
        volumes:
          - /data/lnmp/redis/conf:/usr/local/etc/redis
          - /data/lnmp/redis/data:/data
        networks:
          net01:
            ipv4_address: 172.16.1.103
    networks:
      net01:
        ipam:
          driver: default
          config:
            - subnet: "172.16.1.0/24"
    
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐