zoukankan      html  css  js  c++  java
  • docker compose 一键部署LNMP环境

    1. 创建docker compose目录

    [root@docker ~]# mkdir -p /compose_lnmp
    

    2. 编写nginx的dockerfile

    2.1 创建目录

    [root@docker ~]# cd /compose_lnmp/
    [root@docker compose_lnmp]# ll
    总用量 0
    [root@docker compose_lnmp]# mkdir -p nginx
    [root@docker compose_lnmp]# cd nginx/
    

    2.2 编写nginx配置文件

    [root@docker nginx]# vim nginx.conf 
    [root@docker nginx]# cat nginx.conf 
    #user nobody ;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    error_log  logs/error.log  info;
    
    pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        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  logs/access.log  main;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   /var/www/html;
                index  index.php index.html index.htm;
            }
            
            location ~ .php$ {
                root           /var/www/html;
                fastcgi_pass   lnmp_php:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    
        }
    }
    
    

    2.3 编写nginx的Dockerfile文件

    [root@docker nginx]# vim Dockerfile 
    [root@docker nginx]# cat Dockerfile 
    #this docker file
    #VERSION 1
    #author:shichao@scajy.cn
    
    FROM centos:7
    MAINTAINER shichao@scajy.cn
    RUN yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel  pcre-davel gd-devel iproute net-tools telnet wget curl && yum clean  all && rm -rf /var/cache/yum/*
    RUN useradd -M -s /sbin/nologin nginx
    RUN  wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar zxf  nginx-1.17.6.tar.gz && 
    	cd nginx-1.17.6  && 
    	./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module  --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module && 
    	make && make install
    ENV PATH /usr/local/nginx/sbin:$PATH
    
    EXPOSE 80
    
    ENTRYPOINT ["nginx"]
    
    CMD ["-g","daemon off;"]
    
    

    3. 编写php的dockerfile文件

    3.1 创建php目录

    [root@docker nginx]# cd /compose_lnmp/
    [root@docker compose_lnmp]# ll
    总用量 0
    drwxr-xr-x. 2 root root 42 10月 19 11:32 nginx
    [root@docker compose_lnmp]# mkdir -p php
    [root@docker compose_lnmp]# cd php/
    

    3.2 下载php包

    • 官网下载地址: https://www.php.net/

      [root@docker php]# wget https://www.php.net/distributions/php-7.2.34.tar.gz
      [root@docker php]# ll -a
      总用量 19472
      drwxr-xr-x. 2 root root       49 10月 13 20:38 .
      drwxr-xr-x. 4 root root       30 10月 13 20:21 ..
      -rw-r--r--. 1 root root 19936114 10月 13 20:37 php-7.2.34.tar.gz
      

    3.4 编写php的dockerfile文件

    [root@docker php]# vim Dockerfile 
    [root@docker php]# cat Dockerfile 
    #this docker file
    #VERSION 1
    #author:shichao@scajy.cn
    FROM centos:7
    MAINTAINER  shichao@scajy.cn
    RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel &&
    	yum clean all && 
    	rm -rf /var/cache/yum/*
    ADD php-7.2.34.tar.gz /tmp/
    
    RUN cd /tmp/php-7.2.34 && 
        ./configure --prefix=/usr/local/php 
        --with-config-file-path=/usr/local/php/etc 
        --enable-mysqlnd --with-mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd 
        --with-openssl --with-zlib --with-curl --with-gd 
        --with-jpeg-dir --with-png-dir --with-iconv 
        --enable-fpm --enable-zip --enable-mbstring && 
        make -j 4 && 
        make install && 
        cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && 
        cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && 
        sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf && 
        sed -i "21a daemonize = no" /usr/local/php/etc/php-fpm.conf && 
        cp  php.ini-production /usr/local/php/etc/php.ini
    
    RUN rm -rf /tmp/php-7.2.34* 
    
    WORKDIR /usr/local/php
    EXPOSE 9000
    CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]
    
    

    4. 使用docker compose编译lnmp环境

    4.1 切换到docker compose目录下

    [root@docker php]# cd /compose_lnmp/
    

    4.2 编写docker compose配置文件

    [root@docker compose_lnmp]# vim docker-compose.yml 
    [root@docker compose_lnmp]# cat docker-compose.yml 
    version: '3'
    services:
      lnmp_nginx:
        hostname: lnmp_nginx
        build:
          context: ./nginx
          dockerfile: Dockerfile
        ports:
          - 80:80
        networks:
          - lnmp
        volumes:
          - ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
          - /var/www/html:/var/www/html
    
      lnmp_php:
        hostname: lnmp_php
        build:
          context: ./php
          dockerfile: Dockerfile
        networks:
          - lnmp
        volumes:
          - /var/www/html:/var/www/html
    
      mysql:
        hostname: mysql
        image: mysql:5.7
        ports:
          - 3306:3306
        networks:
          - lnmp
        volumes:
          - ./mysql/conf:/etc/mysql/conf.d
          - ./mysql/data:/var/lib/mysql
        command: --character-set-server=utf8
        environment:
          MYSQL_ROOT_PASSWORD: 123456
          MYSQL_DATABASE: lnmp
          MYSQL_USER: lnmp
          MYSQL_PASSWORD: lnmp123
    
    networks:
      lnmp: 
    

    5. docker compose执行构建lnmp环境

    [root@docker compose_lnmp]# docker-compose -f docker-compose.yml up -d
    Starting compose_lnmp_php_1   ... done
    Starting compose_lnmp_nginx_1 ... done
    Starting compose_lnmp_mysql_1 ... done
    
    # 注释:
    	docker-compose    			执行命令
    	-f						   连接配置文件
    	docker-compose.yml   		编码的配置文件
    	up						   启动,只是up启动,是前台启动
    	-d						   指定后台启动
    

    6. 查看容器是否运行正常

    [root@docker compose_lnmp]# docker-compose ps -a
              Name                         Command               State                 Ports              
    ------------------------------------------------------------------------------------------------------
    compose_lnmp_lnmp_nginx_1   nginx -g daemon off;             Up      0.0.0.0:80->80/tcp               
    compose_lnmp_lnmp_php_1     ./sbin/php-fpm -c /usr/loc ...   Up      9000/tcp                         
    compose_lnmp_mysql_1        docker-entrypoint.sh --cha ...   Up      0.0.0.0:3306->3306/tcp, 33060/tcp
    
    

    7. 浏览器访问测试

  • 相关阅读:
    MaaS系统概述
    流处理认识
    事务补偿
    Hystrix原理与实战
    RPC概念和框架
    git remote: error: hook declined to update
    Unity CombineTexture
    Windows Powershell统计代码行数
    unity面试题二
    unity面试题一
  • 原文地址:https://www.cnblogs.com/scajy/p/13846813.html
Copyright © 2011-2022 走看看