PHP菜鸟博客_共同学习分享PHP技术心得【PHP爱好者】
如何将gitee仓库的php源码快速托管到阿里云函数计算?
2022-7-7 菜鸟站长


1仓库添加s.yml配置文件以及编写简单的php文件



edition: 1.0.0



name: compoent-test



access: '{{ access }}'



services:



  cn-beijing-phpdemo-phpdemo:



    component: devsapp/fc



    props:



      region: cn-beijing



      service:



        logConfig:



        description: 阿里云函数计算php的持续集成和交付演示demo



        name: phpdemo



      function:



        handler: index.handler



        instanceType: e1



        runtime: php7.2



        timeout: 60



        instanceConcurrency: 1



        memorySize: 512



        environmentVariables: {}



        internetAccess: true



        name: phpdemo



        asyncConfiguration: {}



        codeUri: ./code



      triggers:



        - name: http



          description: ''



          type: http



          qualifier: LATEST



          config:



            methods:



              - GET



              - POST



              - PUT



              - DELETE



            authType: anonymous



 



codeUri: ./code php脚本目录。



runtime: php7.2 运行时是php7.2,不需要启动脚本,这种模式下php脚本得按照要求写,不能是thinkphp框架这种。旧的项目迁移也需要改动很大。如果是旧项目请选择自定义运行时,这里只是为了演示cicd



index.php脚本介绍



<?php



use RingCentral\Psr7\Response;



/*



To enable the initializer feature (https://help.aliyun.com/document_detail/89029.html)



please implement the initializer function as below



function initializer($context) {



    echo 'initializing' . PHP_EOL;



}



*/



 



function handler($request, $context): Response{



    /*



    $body       = $request->getBody()->getContents();



    $queries    = $request->getQueryParams();



    $method     = $request->getMethod();



    $headers    = $request->getHeaders();



    $path       = $request->getAttribute('path');



    $requestURI = $request->getAttribute('requestURI');



    $clientIP   = $request->getAttribute('clientIP');



    */



$rt['sta']="1";



$rt['msg']=$request->getAttribute('clientIP');



$json=json_encode($rt, JSON_UNESCAPED_UNICODE);



 



 



    $respHeaders = array('Content-Type' => 'application/json');



    $respBody = $json;



    return new Response(200, $respHeaders, $respBody);



}



返回一个json,msg是客户端的ip。这只是一个最简单的php接口演示页面。



 



源码放到了gitee仓库:



https://gitee.com/wlphp/aliyun-fc-phpdemo



2. 阿里云函数计算创应用



https://fcnext.console.aliyun.com/applications



选择通过仓库导入,仓库类型gitee,第一次选择gitee需要授权下,然后选择1里面的gitee事先创建好的仓库,触发方式选择push到默认的master分支,应用名称是仓库名称,如果提示仓库不可用,请检查s.yml文件是否纯在或者有问题。然后本地把代码提交到gitee仓库的时候就能自动触发部署代码到函数计算了。











application.jpg



3绑定自己的域名



https://fcnext.console.aliyun.com/cn-beijing/domains



域名需要cname解析到函数计算提供的公网的cname,同一个地区的cnameurl是一样的。



然后配置下路由也就是这个域名指向的服务,函数,版本信息。然后就可以访问了。



domain.jpg



4.测试下能否正常访问



 



http://phpdemo.fc.wziyi.com.cn/



last.jpg



5. 总结



函数计算是事件驱动的全托管计算服务。使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码。函数计算为您准备好计算资源,弹性地、可靠地运行任务,并提供日志查询、性能监控和报警等功能。借助函数计算,您可以快速构建任何类型的应用和服务,并且只需为任务实际消耗的资源付费。整体体验下来ci/cd感觉还是很不错的。

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容