美文网首页
mac版docker一步步搭建lnmp环境

mac版docker一步步搭建lnmp环境

作者: DragonRat | 来源:发表于2023-08-30 15:43 被阅读0次

    由于网上docker搭建lnmp的教程五花八门,然而基本搭建完成会碰到各种问题,现在记录下自己搭建lnmp环境的正确流程

    有两种形式,一种形式是直接拉取lnmp的镜像,另外一种是单独拉取xginx,php,mysql然后自己构建的流程

    目前主要记录第二种流程以及配置

    1、要保证nginx、php、mysql在同一网段才能相互访问,k8s应该也有相应的设置

    #构建m1网段
    docker network create m1
    #启动nginx、php、mysql、redis的时候都要带上m1这个网段
    

    2、拉取nginx、php、mysql

    docker pull nginx
    docker pull php:7.4
    docker pull php:5.7.30
    

    3、启动nginx,并且复制出来相应的配置文件

    docker run --name nginx-test -p 80:80 -d nginx
    docker exec -it 2d3515c224ef /bin/bash
    docker cp 2d3515c224ef:/etc/nginx/nginx.conf ./
    docker cp 2d3515c224ef:/etc/nginx/conf.d/default.conf ./conf/
    docker stop 2d3515c224ef     //停止容器
    docker rm 2d3515c224ef      //删除容器
    

    4、按照顺序启动

    docker run --name lnmp_mysql --network m1 -p 3305:3306 -v /Users/guanjian/docker/mysql/data:/var/lib/mysql -v /Users/guanjian/docker/mysql/logs:/logs -v /Users/guanjian/docker/mysql/etc:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30
    
    docker run --name lnmp-php -v /Users/guanjian/docker/nginx/html:/www --link lnmp_mysql --network m1  -p 9000:9000   -d 854
    
    docker run --name nginx-config --network m1 -p 80:80 -v /Users/guanjian/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/guanjian/docker/nginx/logs:/var/log/nginx -v /Users/guanjian/docker/nginx/html:/www -v /Users/guanjian/docker/nginx/conf:/etc/nginx/conf.d \
    --privileged=true --link lnmp-php:php -d 605c77e624dd
    

    对应的docker-compose

    version: '3'
    services:
      nginx:
        image: nginx
        privileged: true
        ports:
          - 80:80
        volumes:
          - /Users/guanjian/docker/nginx/nginx.conf:/etc/nginx/nginx.conf
          - /Users/guanjian/docker/nginx/logs:/var/log/nginx
          - /Users/guanjian/docker/nginx/html:/www
          - /Users/guanjian/docker/nginx/conf:/etc/nginx/conf.d
        depends_on:
          - php:7.4-fpm
        restart: always # 设置重启策略为always
        networks:
          - m1
      php-fpm:
        image: php:7.4-fpm
        ports:
          - 9000:9000
        volumes:
          - /Users/guanjian/docker/nginx/html:/www
        depends_on:
          - mysql:5.6.50
        restart: always # 设置重启策略为always
        networks:
          - m1
      mysql:
        image: mysql:5.6.50
        ports:
          - 3305:3306
        environment:
          MYSQL_ROOT_PASSWORD: 123456
        volumes:
          - /Users/guanjian/docker/mysql/data:/var/lib/mysql
          - /Users/guanjian/docker/mysql/logs
          - /Users/guanjian/docker/mysql/etc:/etc/mysql
        networks:
          - m1
        restart: always # 设置重启策略为always
    
    networks:
      m1:
        # noinspection ComposeUnknownKeys
        name: m1 # 指定网络名称
    
    docker-compose up -d
    

    5、把项目代码移到/Users/guanjian/docker/nginx/html
    6、修改 /Users/guanjian/docker/nginx/conf/default.conf如下

    #查看m1的网段
    docker inspect network m1
    
    server
    {
        listen 80;
        server_name xinqiu.com;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/xinqiu/public;  #宝塔tp5项目
    
        #PHP-INFO-START  PHP引用配置,可以注释或修改
        location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  172.18.0.1:9000;  #m1的ip
            fastcgi_index index.php;
    
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param  QUERY_STRING       $query_string;
            fastcgi_param  REQUEST_METHOD     $request_method;
            fastcgi_param  CONTENT_TYPE       $content_type;
            fastcgi_param  CONTENT_LENGTH     $content_length;
    
            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;
            fastcgi_param  REQUEST_SCHEME     $scheme;
            fastcgi_param  HTTPS              $https if_not_empty;
    
            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
            fastcgi_param  REMOTE_ADDR        $remote_addr;
            fastcgi_param  REMOTE_PORT        $remote_port;
            fastcgi_param  SERVER_ADDR        $server_addr;
            fastcgi_param  SERVER_PORT        $server_port;
            fastcgi_param  SERVER_NAME        $server_name;
    
            # PHP only, required if PHP was built with --enable-force-cgi-redirect
            fastcgi_param  REDIRECT_STATUS    200;
    
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    set $real_script_name $1;
                    set $path_info $2;
             }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
        }
    
        #PHP-INFO-END
    
        #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
        location ~* (runtime|application)/{
            return 403;
        }
        location / {
            if (!-e $request_filename){
                rewrite  ^(.*)$  /index.php?s=$1  last;   break;
            }
        }
        #REWRITE-END
    
        #禁止访问的文件或目录
        location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
        {
            return 404;
        }
    
        #一键申请SSL证书验证目录相关设置
        location ~ \.well-known{
            allow all;
        }
    
        #禁止在证书验证目录放入敏感文件
        if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
            return 403;
        }
    
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
            error_log /dev/null;
            access_log /dev/null;
        }
    
        location ~ .*\.(js|css)?$
        {
            expires      12h;
            error_log /dev/null;
            access_log /dev/null;
        }
    }
    

    7、连接数据库,导入数据库
    8、运行项目,安装对应的php扩展(我是按照下面网址安装的)
    https://zhuanlan.zhihu.com/p/597110758?utm_id=0
    9、大功告成,项目运行

    相关文章

      网友评论

          本文标题:mac版docker一步步搭建lnmp环境

          本文链接:https://www.haomeiwen.com/subject/wocamdtx.html