本文介绍如何将Laravel应用部署到函数计算。与传统的部署方法相比,您可以跳过购买机器等步骤,将传统的Laravel应用一键部署至云端直接用于生产,并且拥有弹性伸缩、按量付费及免运维等特性。

前提条件

您已完成以下操作:
注意

本文介绍的如何使用Funcraft迁移传统框架的相关内容,后期将不再维护,建议您使用Serverless Devs迁移相关框架到函数计算。关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs

关于如何使用Serverless Devs迁移Web框架的详细操作,请参见使用Serverless Devs迁移Web框架

由此带来的不便,敬请谅解!

操作步骤

  1. 通过以下不同的方案创建Laravel项目。详细信息,请参见官方文档。若已有Laravel项目则跳过该步骤。
    • 方案一
      composer create-project "laravel/laravel=5.8.*" --prefer-dist mylaravel
    • 方案二
      composer create-project laravel/laravel --prefer-dist mylaravel "5.8.*"
    • 方案三
      composer create-project "laravel/laravel:5.8.*" --prefer-dist mylaravel
  2. 执行以下命令进入刚创建的示例项目或您已有的项目。
    cd mylaravel
  3. 在本地打开bootstrap目录下的app.php文件,添加以下代码,实现将Laravel项目中的storage目录放到具有读写权限的/tmp目录下,以获取读写权限。
    $app->useStoragePath(env('STORAGE_PATH', dirname(__DIR__) . '/storage'));
    modify-config
  4. 执行以下命令运行本地项目。
    composer install
  5. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y
    预期输出。
    current folder is not a fun project.
    downloading nginx and php7.2 zip from https://gosspublic.alicdn.com/fun/frameworks/support/fun-support-custom-php-d73a6bd6.zip to /private/var/folders/wl/_2ngtj291wx1cj55xlnn290w0000gn/T/fun-support-custom-php-d73a6bd6.zip...
    extract nginx and php7.2 zip to custom runtime...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/php/7.2/fpm/php-fpm.conf...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/php/7.2/fpm/pool.d/www.conf...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/nginx/nginx.conf...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/logrotate.d/nginx...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/logrotate.d/php7.2-fpm...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/usr/lib/php/7.2/php.ini-production...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.fun/root/etc/nginx/sites-enabled/laravel.conf...
    Generating /Users/txd123/Desktop/demo123/mylaravel/.funignore...
    File /Users/txd123/Desktop/demo123/mylaravel/.funignore already exists, Fun will rename to /Users/txd123/Desktop/demo123/mylaravel/.funignore.bak
    Generating /Users/txd123/Desktop/demo123/mylaravel/laravel_bootstrap...
    Generating template.yml...
    Generate Fun project successfully!
    
    ========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
    using region: cn-beijing
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    ...  ...  ...
    
    Waiting for service mylaravel to be deployed...
           Waiting for function mylaravel to be deployed...
                   Waiting for packaging function mylaravel code...
                   The function mylaravel has been packaged. A total of 7987 files were compressed and the final size was 32.34 MB
                   Waiting for HTTP trigger httpTrigger to be deployed...
                   triggerName: httpTrigger
                   methods: [ 'GET', 'POST', 'PUT' ]
                   trigger httpTrigger deploy success
           function mylaravel deploy success
    service mylaravel deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Fun will reuse the temporary domain http://19247408-XXX.test.functioncompute.com, expired at 2020-05-22 09:36:48, limited by 1000 per day.
    
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy succes
  6. 查看./mylaravel/.fun/root/etc/nginx/sites-enabled路径下的laravel.conf文件中是否存在keepalive_timeout字段。
    • 若不存在该字段,您可以从步骤5的预期输出中查看到系统为您生成的临时域名,通过该域名您可直接访问刚部署的应用。
    • 若存在该字段,您需要先删除该字段,然后执行以下命令。
      fun deploy -y

      再次成功部署该函数后,您可以在预期输出中查看到系统为您生成的临时域名,通过该域名您可以直接访问刚部署的函数。

    说明 临时域名仅用作演示及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名