美文网首页
1.8 php项目打包为docker包

1.8 php项目打包为docker包

作者: 回眸淡然笑 | 来源:发表于2023-10-18 17:45 被阅读0次

    1首先在linux或者windows上安装docker环境

    安装方法见其他章节,如果是宝塔直接点击安装即可

    2 从github拉取dnmp环境

    $ git clone https://github.com/shmilylbelva/dnmp.git //建议使用https://github.com/yeszao/dnmp版本,功能更强大
    $ cd dnmp
    

    按照php项目需要的环境进行配置docker-compose.yaml和Dockerfile以及.env。
    这里以thinkphp为例 假如需要的php版本为7.1.33,redis为7.0.0,iamp扩展,

    2.1

    那么需要在.env文件中修改php版本为7.1.33 redis修改为7.0.0-alpine
    在docker-compose.yaml中打开redis,php,nginx,其他包全部关闭,需要的时候再重新编译即可。

    修改services/nginx/conf.d/localhost.conf

    server {
        listen       80;
        server_name  migo.tik-im.com;
        root   /www/api/public/;
        index  index.php index.html index.htm;
        #charset koi8-r;
        
        access_log /dev/null;
        #access_log  /var/log/nginx/nginx.localhost.access.log  main;
        error_log  /var/log/nginx/nginx.localhost.error.log  warn;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        #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   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$ {
           fastcgi_pass   php:9000;
           fastcgi_index  index.php;
           #下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action的url访问模式
           fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           #下面两句才能真正支持 index.php/index/index/index的pathinfo模式
           fastcgi_param  PATH_INFO  $fastcgi_path_info;
           fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
           include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    server {
        listen 443 ssl http2;
        server_name  migo.tik-im.com;
        root   /www/api/public;
        index  index.php index.html index.htm;
        #charset koi8-r;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        access_log /dev/null;
        #access_log  /var/log/nginx/nginx.localhost.access.log  main;
        error_log  /var/log/nginx/nginx.localhost.error.log  warn;
    
        #error_page  404              /404.html;
    
        ssl_certificate /ssl/jobs/jobs.pem;
        ssl_certificate_key /ssl/jobs/jobs.key;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
    
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ [^/]\.php(/|$) {
            fastcgi_pass   php:9000;
            include        fastcgi-php.conf;
            include        fastcgi_params;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    

    如果宿主机已启动了nginx,那么需要对.env文件修改,以保证端口不冲突

    NGINX_HTTP_HOST_PORT=8083
    NGINX_HTTPS_HOST_PORT=445
    
    REDIS_HOST_PORT=6380
    

    在宿主机的nginx中增加vhost配置,使正常访问域名能代理到docker中的nginx

    server
    {
        listen 80;
        server_name migo.tik-im.com;
        index index.php index.html index.htm default.php default.htm default.html;
        location / {
            client_max_body_size    20m;
            # 将客户端的 Host 和 IP 信息一并转发到对应节点
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            # 转发Cookie,设置 SameSite
            proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
    
            # 执行代理访问真实服务器
            proxy_pass http://127.0.0.1:8083;
            
        }
    }
    server
    {
        listen 443 ssl http2;
        server_name migo.tik-im.com;
        index index.php index.html index.htm default.php default.htm default.html;
        ssl_certificate /www/wwwroot/dnmp_migo/services/nginx/ssl/migo/migo.pem;
        ssl_certificate_key /www/wwwroot/dnmp_migo/services/nginx/ssl/migo/migo.key;
        location / {
            client_max_body_size    20m;
            # 将客户端的 Host 和 IP 信息一并转发到对应节点
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            # 转发Cookie,设置 SameSite
            proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
    
            # 执行代理访问真实服务器
            proxy_pass http://127.0.0.1:8083;
            
        }
    }
    

    数据库使用宿主机的mysql,mysql不建议在docker中运行,并发高了影响性能。

    配置database.php
    mysql主机地址修改为服务器内网ip,redis的主机地址改为migo-redis,因为容器与容器是expose端口联通的,而且在同一个networks下,所以连接的host参数直接用容器名称,port参数就是容器内部的端口

    相关文章

      网友评论

          本文标题:1.8 php项目打包为docker包

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