1.8 php项目打包为docker包

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参数就是容器内部的端口

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容