Home > Article > Backend Development > PHP meets Serverless to help you solve these pain points!
PHP has a wide range of applications, especially in the development of web programs. According to the latest Wikipedia statistics, in April 2013, PHP has been installed There are more than 244 million websites and 2.1 million servers, and according to W3Techs report, as of September 2021, 78.9% of websites use PHP. So PHP is the number one language in the world, at least in the field of web development, which is not a joke.
In terms of technology selection, PHP mainly uses LAMP (the full name is Linux apache mysql php) or LNMP (the full name is Linux nginx mysql php). This mature and stable technical framework promotes the PHP web development ecosystem prosperity and business success.
In the traditional development model, developers themselves need to install, maintain and upgrade various software:
If you are an enterprise user, if the business volume increases or for the stability and availability of the production environment, using load balancing is an inevitable option:
At this time, PHP developers or online operation and maintenance students are concerned about more things:
Every additional production machine needs to be reinstalled Related software, make the same nginx configuration and php-fpm configuration, and maintain security updates for each production machine
If the developed application requires a new extension, you may need to manually update it every time Add and expand each machine
The load balancer is upgraded as the business changes, and the next Worker machine hangs up. How to perform operation and maintenance
How to deal with the peaks and troughs of business in order to improve resource utilization
...
If you are an enterprise user with a large number of development members in the project team, is it not necessary to configure a Linux machine with NLP installed for each development as a development and testing machine (or multiple people share a machine)?
If you are an ISV, outsourcing company or startup that provides website development and hosting, and my customers are portals of small and medium-sized enterprises, how can I improve my back-end machine resources? utilization and better provide customized services?
If you are a student or preparing to learn PHP development, and you only have a Windows computer locally, can you directly obtain the LNP (Linux Nginx PHP) environment for learning for almost free?
...
With these questions, let’s explore how Serverless solves these pain points.
What is Serverless?
Serverless = Faas (Function as a service) Baas (Backend as a service), we can quickly understand the related concepts through two diagrams:
Traditional Mode
Serverless Mode
#The CDN and OSS in the picture are BaaS services, and FC is the FaaS platform with custom function logic. Through this comparison, we can quickly get the features and benefits of FaaS:
PHP is a big language for the development community, FaaS of major cloud vendors, such as Alibaba Cloud’s Function Compute, AWS’s Lambda (through Custom Runtime Indirect support), Tencent's SCF, etc. have all launched support for the PHP language. Facing the serverless technology innovation practice in the front-end field (if you are interested, see the appendix at the end of this article), phper should not be less generous. Taking Alibaba Cloud Function Computing as an example, there are many PHP developers who have many interesting practices:
Directly use gd or ImageMagick extensions to achieve flexible and highly available images, watermarks, etc. A CPU-intensive API
Directly use the ffmpeg performance instance to make asynchronous stateful calls to complete audio and video processing services such as video editing and synthesis
Use HTTP The function implemented by the trigger is embedded in the advertising platform to quickly realize high-availability purchase business
Directly migrate the WEB API previously implemented based on the framework (such as ThinkPHP) to the FaaS platform , no longer have to worry about downtime and operation and maintenance issues
...
function handler($event, $context) { $eventObj = json_decode($event, $assoc = true); // do your thhings // .... return $eventObj['key']; }But can we go one step further so that developers do not need to implement APIs one by one according to the function entrances agreed by FaaS vendors, but can directly convert projects that traditionally run on LAMP or LNMP into FaaS? The answer is yesAlibaba Cloud Function Compute’s Custom Runtime and its minimalist programming model based directly on the HTTP protocol are at the forefront of all cloud vendors. When Function Compute starts the Custom Runtime execution environment, it will call the bootstrap file by default (or the Args parameter you set when you created the function) to start your customized HTTP Server, and then This HTTP Server takes over all requests from the function computing system, that is, all your function call requests. The underlying system of Function Compute Custom runtime execution environment is Linux, and has built-in nginx/1.10.3 and php-fpm7.4. For PHP applications, you can use it directly to deploy Taking a wordpress project as an example, you only need to package the following directory directly into a zip package to create a function on the function computing platform:
- bootstrap - nginx.conf - php-fpm.conf - php.ini-production - wordpressThe wordpress directory is the corresponding web project, and bootstrap is to start nginx and php- fpm script can be used:
... echo "start php-fpm" php-fpm7.4 -c /code/php.ini-production -y /code/php-fpm.conf echo "start nginx" nginx -c /code/nginx.conf ...bootstrap For details, please refer to WordPress in FCSo, after using Function Compute, a Serverless product, and traditional PHP development, you no longer need to consider load balancing. There is no need to think about expansion and contraction, no need to manage machines, no need to worry about downtime, etc. You only need to develop the business code with peace of mind. As can be seen from the above picture: developers only need to develop their own business code. The only thing that needs to be considered is that the function computing side should not expand too much. If it is too strong (for example, directly setting the maximum number of instances that the function can pop up under the function computing platform settings), it will put too much pressure on the downstream MySQL database. Of course, when completely migrating from the original traditional PHP web application to a serverless function computing platform, data persistence issues may need to be considered in some scenarios, because function computing is stateless. , Data persistence can be completed with the help of NAS, Redis and other services. Taking NAS as an example, the flow chart is as follows: Take WordPress as an example, backend Images uploaded by the system or Session functions need to be persisted to disk.
比如将 wordpress 工程不作为函数的代码包的一部分, 而已提前上传到 NAS 盘, 只需要设置好 nginx.conf 中的 root 能知道 web 工程即可, 如上面的 nginx.conf, /mnt/auto 表示挂载的 NAS 目录,mnt/auto/wordpress 则表示在 NAS 上的 web 工程。
此时对您来说, 函数再也不用变了, 您可能只是需要开发新的业务代码, 然后上传到 NAS 上即可(或者直接使用 git 直接在 NAS 操作,实现 web 工程的版本和 git 上的 commit 绑定, 使用 git 实现代码的快速升级和混滚)
但是从安全生产的角度来说, 还是建议您 web 工程变更最好和函数的变更相关联
小结
从上面的讨论和陈述中, 我们不难发现, PHP 遇见 Serverless 是一件令人兴奋的事情, 让 phper 有了更大的想象空间。 Serverless 的理念和 PHP 这个语言出现的理念也是一致的: 即让开发者最大精力集中在自己的业务价值。 PHP 语言一直是 web 领域最好的生产力代表, 而 Serverless 将会让 PHP 如虎添翼。
我们最后来一一解答下前言中提出的问题:
如果您是一个企业用户, 业务体量变大或者为了生产环境的稳定和可用性, 如何做?
如上面陈述, 使用函数计算和传统的 PHP 开发相结合后, 您再也不用考虑负载均衡的事情, 不用考虑扩缩容的事情, 不用管理机器、担心宕机的事情等等, 只需要安安心心把业务代码开发好即可。
如果您是项目组开发成员比较多的企业用户,能不能不需要给每个开发配置一个安装的 NLP 的 Linux 机器作为开发测试机器(或者多人共享一个机器)?
是的, 每个开发者在函数计算上创建一个自己的 Service/函数即可, Service/函数配置开发测试环境的 VPC,实现内网安全访问数据库等其他下游服务。 函数调用的时候, 函数计算会拉一个 NLP 的执行环境来运行您分支上正在开发的 PHP 代码。
每个执行环境是相互隔离的
按调用次数计费, 不需要预留机器, 免除了机器成本上的浪费
也可以很方便进行压测等各种事宜
如果您是一个提供网站开发和托管的 ISV 、外包公司或者创业公司, 我的客户都是一些中小企业的门户网站, 我怎么提高我后端机器资源利用率以及更好提供定制化服务?
通常来说, 很多企业门户网站访问量不大, 但是网站挂掉了会引起客户投诉。每个客户的网站通过service 或者函数区分, 通过函数名或者service去区分您自己的客户: i. 管理方便 ii. 做定制化方便 iii. 做不同vip等级服务方便。 举个例子, 您可以快速通过某个函数的调用指标情况, 可以看出哪个客户的网站访问量大,可以做出客户画像以及制定不同的收费和 vip 服务级别。
如果您是一个学生或者准备学习 PHP 开发,本地只有 Windows 电脑, 能不能直接近乎免费的方式获取 LNP(Linux+Nginx+PHP) 的环境用来学习呢?
是的, 只要将如下的文件和文件夹打包成 zip 包去函数计算控制台创建函数即可
- bootstrap - nginx.conf - php-fpm.conf - php.ini-production - myweb | - hello.php
这里构建了一个钉钉群: 31897696, 如果您对 PHP 落地 Serverless 感兴趣,您有观点、想法或者想吐槽的, 可以和大家一起交流。
本文作者:罗松(西流)
阿里云函数计算技术专家
负责阿里云函数计算产品功能开发(runtime 开发、事件源集成以及企业级 Serverless 解决方案落地等),目前专注在 Serverless 开发者工具链的建设,是云原生 Serverless Dev Tools 研发负责人,主导了 S/fc 等组件的开发工作,关注 Serverless 最新技术动态以及企业级解决方案的落地,致力于推动 Serverless 在开发者群体的流行。
The above is the detailed content of PHP meets Serverless to help you solve these pain points!. For more information, please follow other related articles on the PHP Chinese website!