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

步骤一:准备环境

您无需安装 Docker,仅安装 Funcraft 即可,最简单的方式即直接下载可执行的二进制文件。

  1. 安装 Funcraft 到本机。详情请参见安装 Funcraft
  2. 执行 fun --version 检查安装是否成功。

步骤二:迁移应用

  1. 执行以下命令创建一个 Laravel 项目,详情请参见官方文档,若已有 Laravel 项目则跳过该步骤。
    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 success

    部署成功后,您可以在日志中看到函数计算为您生成的临时域名,通过这个临时域名您可直接访问刚部署的应用。

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