{"componentChunkName":"component---src-templates-best-practice-detail-tsx","path":"/best-practice/2020-01-13-LARAVEL-with-serverless","result":{"data":{"currentBlog":{"id":"4f8cc7f1-71e2-5cac-a5fe-1bfa55e4b012","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/20191226/1577353083364-laravel.png","authors":["Tabor"],"categories":["best-practice"],"date":"2020-01-13T00:00:00.000Z","title":"Laravel + Serverless Framework 快速创建 CMS 内容管理系统","description":"通过 Serverless Laravel 组件快速构建一个 Serverless CMS 内容管理系统","authorslink":["https://canmeng.net"],"translators":null,"translatorslink":null,"tags":["Wintersmith","Laravel"],"keywords":"Serverless Laravel,CMS 内容管理系统,构建Serverless CMS内容管理系统","outdated":true},"wordCount":{"words":154,"sentences":38,"paragraphs":38},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2020-01-13-LARAVEL-with-serverless.md","fields":{"slug":"/best-practice/2020-01-13-LARAVEL-with-serverless/","keywords":["php","serverless","云函数","composer","serverless","php","Laravel","Serverless","phar"]},"html":"<p>今天，为大家带来一篇 Laravel + Serverless Framework 的综合实战，里面信息量有点多，大家仔细看哦～</p>\n<p>首先，我来介绍下主要的本地环境吧：</p>\n<ul>\n<li>Git：不多说，只要会敲代码就应该知道</li>\n<li>Node.js：由于腾讯云 <a href=\"https://cloud.tencent.com/product/sf\">Serverless Framework</a> 主编程语言是 Node.js，所以本地也请大家搞一个吧</li>\n<li>NPM：NPM 是随同 Node.js 一起安装的包管理工具，能解决 Node.js 代码部署上的很多问题，我们的 sls 也是通过这玩意下载的</li>\n<li>PHP：不多说，最好的语言</li>\n<li>Composer：PHP 世界的包管理工具 Composer ，类似 npm</li>\n<li>Laravel：Laravel 是一套简洁、优雅的 PHP Web 开发框架 (PHP Web Framework)</li>\n</ul>\n<p>好像有点多，咱们一步步往下看。</p>\n<h2 id=\"1-安装-serverless\"><a href=\"#1-%E5%AE%89%E8%A3%85-serverless\" aria-label=\"1 安装 serverless permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. 安装 Serverless</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ npm install -g serverless</code></pre></div>\n<p>我们最重要，而且最简单的一步就完成了。</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020113/1578915292994-89i51tt2m4%5B1%5D.png\" alt=\"安装 Serverless\"></p>\n<h2 id=\"2-配置-composer\"><a href=\"#2-%E9%85%8D%E7%BD%AE-composer\" aria-label=\"2 配置 composer permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. 配置 Composer</h2>\n<p>有两种方式，我们在这里使用第一种为大家示范</p>\n<p>第一种下载 phar 文件：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ wget https://getcomposer.org/download/1.9.1/composer.phar</code></pre></div>\n<p>第二种直接安装：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">php -r &quot;copy(&#39;https://getcomposer.org/installer&#39;, &#39;composer-setup.php&#39;);&quot;\nphp -r &quot;if (hash_file(&#39;sha384&#39;, &#39;composer-setup.php&#39;) === &#39;baf1608c33254d00611ac1705c1d9958c817a1a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182&#39;) { echo &#39;Installer verified&#39;; } else { echo &#39;Installer corrupt&#39;; unlink(&#39;composer-setup.php&#39;); } echo PHP_EOL;&quot;\nphp composer-setup.phpphp -r &quot;unlink(&#39;composer-setup.php&#39;);&quot;</code></pre></div>\n<p>为了方便，我这里直接下载了 composer.phar 文件，具体用法如下：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ php composer.phar</code></pre></div>\n<p><img src=\"https://img.serverlesscloud.cn/2020113/1578915318015-pa2uz2y1n4%5B1%5D.png\" alt=\"安装 composer\"></p>\n<h2 id=\"3-创建-coastercms\"><a href=\"#3-%E5%88%9B%E5%BB%BA-coastercms\" aria-label=\"3 创建 coastercms permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. 创建 coastercms</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$php composer.phar create-project web-feet/coastercms</code></pre></div>\n<p>首先我们使用安装好的 composer 来创建我们的项目，如果直接在环境变量安装 composer 请自行将 php composer.phar 替换。然后，我们需要修改 Laravel 项目</p>\n<p>由于云函数在执行时，只有 /tmp 可读写的，所以我们需要将 laravel 框架运行时的 storage 目录写到该目录下，为此需要修改 bootstrap/app.php 文件，在 $app = new Illuminate\\Foundation\\Application 后添加：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$app-&gt;useStoragePath($_ENV[&#39;APP_STORAGE&#39;] ?? $app-&gt;storagePath());</code></pre></div>\n<p>完成样式：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">/*\n|--------------------------------------------------------------------------\n| Create The Application\n|--------------------------------------------------------------------------\n|\n| The first thing we will do is create a new Laravel application instance\n| which serves as the &quot;glue&quot; for all the components of Laravel, and is\n| the IoC container for the system binding all of the various parts.\n|\n*/\n$app = new Illuminate\\Foundation\\Application(\nrealpath(__DIR__.&#39;/../&#39;)\n);\n$app-&gt;useStoragePath($_ENV[&#39;APP_STORAGE&#39;] ?? $app-&gt;storagePath());\n/*\n/*\n|--------------------------------------------------------------------------\n| Create The Application\n|--------------------------------------------------------------------------\n|\n| The first thing we will do is create a new Laravel application instance\n| which serves as the &quot;glue&quot; for all the components of Laravel, and is\n| the IoC container for the system binding all of the various parts.\n|\n*/\n$app = new Illuminate\\Foundation\\Application(\nrealpath(__DIR__.&#39;/../&#39;)\n);\n$app-&gt;useStoragePath($_ENV[&#39;APP_STORAGE&#39;] ?? $app-&gt;storagePath());\n/*</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># 视图文件编译路径\nVIEW_COMPILED_PATH=/tmp/storage/framework/views\n\n# 由于是无服务函数，所以没法存储 session 在硬盘上，如果不需要 sessions，可以使用 array\n# 如果需要你可以将 session 存储到 cookie 或者数据库中\nSESSION_DRIVER=array\n\n# 建议将错误日志输出到控制台，方便云端去查看\nLOG_CHANNEL=stderr\n\n# 应用的 storage 目录必须为 /tmp\nAPP_STORAGE=/tmp\n\n# 视图文件编译路径\nVIEW_COMPILED_PATH=/tmp/storage/framework/views\n\n# 由于是无服务函数，所以没法存储 session 在硬盘上，如果不需要 sessions，可以使用 array\n# 如果需要你可以将 session 存储到 cookie 或者数据库中\nSESSION_DRIVER=array\n\n# 建议将错误日志输出到控制台，方便云端去查看\nLOG_CHANNEL=stderr\n\n# 应用的 storage 目录必须为 /tmp\nAPP_STORAGE=/tmp</code></pre></div>\n<h2 id=\"4-配置-mysql-数据库\"><a href=\"#4-%E9%85%8D%E7%BD%AE-mysql-%E6%95%B0%E6%8D%AE%E5%BA%93\" aria-label=\"4 配置 mysql 数据库 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. 配置 MySQL 数据库</h2>\n<p>这里我们建议使用腾讯云的 CDB 云数据库：</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020113/1578915337328-lu3mep4x02%5B1%5D.png\" alt=\"数据库\"></p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">DB_CONNECTION=&quot;mysql&quot;\nDB_HOST=&quot;gz-cdb-qla00XXX.sql.tencentcdb.com&quot;\nDB_PORT=639XX\nDB_DATABASE=&quot;coaster_db&quot;\nDB_USERNAME=&quot;coaster_usr&quot;\nDB_PASSWORD=&quot;secret&quot;\nDB_PREFIX=</code></pre></div>\n<p>然后将 <code class=\"language-text\">.env.example</code> 重命名为 <code class=\"language-text\">.env</code>。至此，我们的 Laravel 配置即可大功告成。</p>\n<h2 id=\"5-部署-serverless\"><a href=\"#5-%E9%83%A8%E7%BD%B2-serverless\" aria-label=\"5 部署 serverless permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. 部署 Serverless</h2>\n<p>接下来，我们在项目根目录，创建 serverless.yml 文件，在其中进行如下配置</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ touch serverless.yml</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># serverless.yml\n\nMyComponent:\ncomponent: &quot;@serverless/tencent-laravel&quot;\ninputs:\nregion: ap-guangzhou\nfunctionName: laravel-function\ncode: ./\nfunctionConf:\ntimeout: 10\nmemorySize: 128\nenvironment:\nvariables:\nTEST: vale\nvpcConfig:\nsubnetId: &#39;&#39;\nvpcId: &#39;&#39;\napigatewayConf:\nprotocol: https\nenvironment: release</code></pre></div>\n<p>完成后，我们在当前目录下运行：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">serverless --debug</code></pre></div>\n<p>引导安装后，便可进入管理后台：</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020113/1578915365153-kru2vmr2j2%5B1%5D.png\" alt=\"完成效果\"></p>\n<hr>\n<div id='scf-deploy-iframe-or-md'></div>\n<hr>\n<blockquote>\n<p><strong>传送门：</strong></p>\n<ul>\n<li>GitHub: <a href=\"https://github.com/serverless/serverless/blob/master/README_CN.md\">github.com/serverless</a></li>\n<li>官网：<a href=\"https://serverless.com/\">serverless.com</a></li>\n</ul>\n</blockquote>\n<p>欢迎访问：<a href=\"https://serverlesscloud.cn/\">Serverless 中文网</a>，您可以在 <a href=\"https://serverlesscloud.cn/best-practice\">最佳实践</a> 里体验更多关于 Serverless 应用的开发！</p>","tableOfContents":"<ul>\n<li><a href=\"/best-practice/2020-01-13-LARAVEL-with-serverless/#1-%E5%AE%89%E8%A3%85-serverless\">1. 安装 Serverless</a></li>\n<li><a href=\"/best-practice/2020-01-13-LARAVEL-with-serverless/#2-%E9%85%8D%E7%BD%AE-composer\">2. 配置 Composer</a></li>\n<li><a href=\"/best-practice/2020-01-13-LARAVEL-with-serverless/#3-%E5%88%9B%E5%BB%BA-coastercms\">3. 创建 coastercms</a></li>\n<li><a href=\"/best-practice/2020-01-13-LARAVEL-with-serverless/#4-%E9%85%8D%E7%BD%AE-mysql-%E6%95%B0%E6%8D%AE%E5%BA%93\">4. 配置 MySQL 数据库</a></li>\n<li><a href=\"/best-practice/2020-01-13-LARAVEL-with-serverless/#5-%E9%83%A8%E7%BD%B2-serverless\">5. 部署 Serverless</a></li>\n</ul>"},"previousBlog":{"id":"1e3cc3dc-4176-54ac-99bf-c9f2d7edd372","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020113/1578918119146-WINTERS.png","authors":["Tabor"],"categories":["best-practice"],"date":"2020-01-13T00:00:00.000Z","title":"使用 Wintersmith + Serverless Framework 快速创建个人站点","description":"通过 Serverless Website 组件快速构建一个 Serverless Wintersmith 个人站点。","authorslink":["https://canmeng.net"],"translators":null,"translatorslink":null,"tags":["Serverless","Wintersmith"],"keywords":"Wintersmith,Wintersmith 站点,Serverless Framework 快速创建个人站点","outdated":true},"wordCount":{"words":118,"sentences":30,"paragraphs":30},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2020-01-13-Wintersmith-with-serverless.md","fields":{"slug":"/best-practice/2020-01-13-Wintersmith-with-serverless/","keywords":["serverless","website","无服务器","serverless","Serverless","yml","wintersmith","Framework","部署","tencent"]}},"nextBlog":{"id":"8d3e0a32-3028-5f67-b38e-9088a83b37ab","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020115/1579078112543-1577769064015-joshua-aragon-FGXqbqbGt5o-unsplash.jpg","authors":["Aceyclee"],"categories":["guides-and-tutorials"],"date":"2019-12-17T00:00:00.000Z","title":"使用 Serverless Framework，结合云函数 SCF、API 网关和云数据库 MySQL 构建 REST API","description":"本文介绍如何创建一个进行自动化面试评估的 Serverless 应用！","authorslink":["https://www.zhihu.com/people/Aceyclee"],"translators":null,"translatorslink":null,"tags":["Node.js","API 网关"],"keywords":"Serverless 自动化面试评估,Serverless 云数据库 MySQL,云函数 SCF","outdated":null},"wordCount":{"words":259,"sentences":62,"paragraphs":62},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-12-17-node-rest-api-with-serverless-scf-and-mongodb.md","fields":{"slug":"/blog/2019-12-17-node-rest-api-with-serverless-scf-and-mongodb/","keywords":["java","nodejs","serverless","无服务器","云函数","candidate","Serverless","serverless","dev","candidateSubmission","service"]}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"4f8cc7f1-71e2-5cac-a5fe-1bfa55e4b012","previousBlogId":"1e3cc3dc-4176-54ac-99bf-c9f2d7edd372","nextBlogId":"8d3e0a32-3028-5f67-b38e-9088a83b37ab"}}}