暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Docker自动化部署Laravel优化篇

675

 

之前我们介绍过docker自动化部署Laravel,最近我把我的一个项目改成docker方式进行部署,也是用Laravel开发的项目,并且需要用到Laravel的定时调度功能,并且实现每次部署的时候能够进行数据库迁移的操作,为了实现这两个功能,我自己写了一个PHP+Nginx基础镜像,在这里跟大家分享部署的过程。

 

01


基础镜像

这里我选择用我自己写的镜像做基础镜像,是因为我的基础镜像默认是开启定时任务的(crontab),因为我之前上一篇教程的基础镜像,默认是不开启crontab的,所以我们要找的基础镜像最好是默认开启crontab,这样我们在构建我们的项目镜像的时候,直接映射定时调度就可以了。

 

02


准备Nginx配置文件

我们在Laravel项目目录下创建一个文件,命名为default.conf,配置如下
    server {
    listen 80;
    server_name localhost;


    #charset koi8-r;


    location {
    root var/www/html/public;
    index index.php index.html index.htm;


    # 如果没有以下4行,laravel将只能访问首页,其他页面都是404
    try_files $uri $uri/ index.php?$query_string;
    if (!-e $request_filename){
    rewrite ^/(.*) index.php last;
    }
    # 如果没有以上4行,laravel将只能访问首页,其他页面都是404


    }


    #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$ {
    root /var/www/html/public;
    index index.php index.html;
    # 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000
    fastcgi_pass 127.0.0.1:9000;
    # 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000
    fastcgi_index index.php;
    # 下面这行也改了 中间的$document_root
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    # deny all;
    #}
    }

     

    03


    准备定时调度任务

    同样在项目目录下创建一个名字为artisan的文件,用于定时调度,配置如下
      * * * * * /usr/local/bin/php /var/www/html/artisan schedule:run >> /var/www/html/public/timer.log

      这里我们放定时任务的执行命令。

       

      04


      创建镜像构建文件

      我们在项目目录下,创建一个名字为Dockerfile,用于创建我们项目的镜像
        FROM  hongzhuangxian/php7.3_nginx
        #复制Nginx配置至默认配置目录
        ADD default.conf /etc/nginx/conf.d/
        #复制项目代码至运行目录
        ADD . /var/www/html/
        #设置项目目录权限
        RUN chown www-data:www-data -R /var/www/html/storage
        RUN chown www-data:www-data -R /var/www/html/bootstrap
        RUN chown www-data:www-data -R /var/www/html/public
        #复制定时任务命令到容器
        COPY ./crontab /var/spool/cron/crontabs/root
        RUN chmod 0644 /var/spool/cron/crontabs/root
        RUN crontab /var/spool/cron/crontabs/root

        这里的基础镜像用我们自己写的镜像,大家可以参考我上一篇文章,自己动手创建一个,或者直接用我的,由于我这个是在docker hub上,速度可能会很慢,建议大家把基础镜像包上传到阿里云镜像仓库,这样会快一点。构建镜像也很简单,就是把代码放到指定Nginx指定读取目录,把我们写的Nginx配置映射到里面,同时把我们设置的调度命令映射到基础镜像的定时调度里面。

         

        05


        自动化构建

        自动化构建我们还是用原来的方式Gitea+Drone方式,上一篇跟大家介绍过,我这里就不重复描述,我们只要把Gitea跟Drone关联激活好,同时密钥配置好就可以了,下面我直接贴我的自动化构建脚本,需要在项目目录下创建一个文件名为.drone.yml的文件
          kind: pipeline
          type: docker
          name: program-manage


          #构建步骤
          steps:
          #安装composer扩展
          - name: backend
          pull: if-not-exists
          image: hongzhuangxian/php7.3-diy
          commands:
          - composer install --prefer-dist
          - php artisan migrate
          #推送镜像至镜像仓库
          - name: publish
          pull: if-not-exists
          image: plugins/docker
          mirrors: #镜像仓库加速地址,不需要加速,可以直接删除
          from_secret: registry_mirrors
          settings:
          purge: false
          registry: #镜像仓库域名
          from_secret: registry
          repo: #镜像仓库详细地址
          from_secret: repo
          use_cache: true
          tags:
          - latest
          username: #镜像仓库账户
          from_secret: registry_user_name
          password: #镜像仓库密码
          from_secret: registry_password
          #部署服务
          - name: ssh commands
          pull: if-not-exists
          image: appleboy/drone-ssh
          settings:
          host:
          from_secret: ssh_ip
          port: 22
          username:
          from_secret: ssh_user_name
          password:
          from_secret: ssh_password
          script:
          - cd /home/php/programManage
          - docker-compose pull && docker-compose up --force-recreate -d
          - docker rmi $(docker images | grep docker_diy/program-manage | grep none | awk '{print $3}')
          #通知发到telegram
          - name: telegram
          pull: if-not-exists
          image: hongzhuangxian/telegram-drone-plugin
          settings:
          proxy_url: "https://telegram.hongzhuangxian.cn"
          token:
          from_secret: telegram_token
          chat_id:
          from_secret: telegram_chat_id
          #规定master分支,触发构建
          branches: master

          通知对象,我这里选择用Telegram,大家可以选择其他通知形式,更多通知情况,大家可以到Drone的插件市场查看。

           

          06


          部署脚本

          我们在部署服务器创建一个目录/home/php/programManage,同时创建一个uploads目录,用于存放我们的图片上传,这样不会因为每次容器重建,导致数据丢失,然后创建docker-compose.yml编排文件,内容如下
            version: "3"
            services:
            program-manage:
            image: 代码镜像地址
            container_name: docker_programManage_compose
            restart: always
            volumes:
            - /home/php/programManage/uploads:/var/www/html/public/uploads
            environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai
            ports:
            - 7180:80

            这样我们基本完成自动化部署的前置工作。

             

            07


            试验自动化部署

            我们直接提交代码,看看最终效果

            通知内容

            这样就完成了Laravel自动化部署。


            【图】来源于网络

            【文】https://hongzx.cn/home/blogShow/235

            Follow

            佛布朗斯基博客

            (佛布朗斯基)我是一只热爱编程的码农,已从事后端开发5年以上,也正因此,在日常工作学习中,会遇到蛮多问题需要解决,我希望透过记录,真实地将问题以及解决方法保存下来,更为高效地解决问题是我的初衷。


             

            文章转载自程序员技术笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论