×
Community Blog Use PHP + Serverless to Help You Solve These Pain Points!

Use PHP + Serverless to Help You Solve These Pain Points!

This article discusses the best practice of PHP application in Serverless and its value in detail.

By Xiliu (Alibaba Cloud Function Compute Expert)

Overview

PHP has a wide range of applications, especially in the development of web programs. PHP has been installed on more than 244 million websites and 2.1 million servers since April 2013. According to the W3Techs report, as of September 2021, 78.9% of websites use PHP.

In terms of technology selection, PHP mainly uses LAMP (Linux + Apache + MySQL + PHP) or LNMP (Linux + Nginx + MySQL + PHP). The mature and stable technical framework promotes the prosperous development ecology and commercial success of PHP Web.

1

Developers need to install and maintain various software installation, maintenance, and upgrades in the traditional development mode:

If you are an enterprise user, SLB is an inevitable option to meet the increasing business volume or the stable and available production environment.

2

PHP developers (or those responsible for online operation and maintenance) care about more things:

  • Each additional production machine needs to reinstall the relevant software, make the same Nginx and php-fpm configuration, and maintain the security update of each production machine.
  • If the developed application needs a new extension, it may need to be extended to each machine manually.
  • As the business changes and upgrades, how can O&M be done if the next Worker machine after the server load balancer hangs up?
  • How can you deal with the peaks and troughs of the business to improve the utilization rate of resources?

If you are an enterprise user with a large number of developers on the project team, is it possible that there is no need for each developer to deploy a Linux machine with NLP installed?

For an ISV, outsourcing company, or start-up company that provides website development and hosting, their customers are portals of some small and medium-sized enterprises. How can they improve the utilization rate of the backend machine resources and provide better customized services?

Students (or those planning to learn PHP development) may only have a Windows system locally. Can they obtain the LNP (Linux + Nginx + PHP) environment for learning for free?

Let's explore how Serverless solves these pain points.

When PHP Meets Serverless

What Is Serverless?

Serverless = Function as a Service (FaaS) + Backend as a Service (BaaS). We can understand the related concepts through the two graphs below:

  • Traditional Mode

3

  • Serverless Mode

4

The Alibaba Cloud Content Delivery Network (CDN) and OSS in the figure are BaaS services, and FC is the FaaS platform with custom function logic. We can learn the FaaS features and benefits through this comparison:

  • Focus on business code development and write the corresponding logic
  • Extreme auto scaling and no need to manage servers
  • Pay-as-you-go and billed in milliseconds for each call

Serverless discussed later in this article mainly refers to FaaS. The following diagram shows that after several lines of code are written and saved to the FaaS platform of the cloud vendor, a flexible and highly available Web API is completed.

5

When PHP Meets Serverless

PHP is a large language for the development group. The FaaS of major cloud vendors, such as Alibaba Cloud's Function Compute (FC), AWS's Lambda (indirect support through Custom Runtime), Tencent's SCF, etc., have supported the PHP language. Please see the appendix at the end of this article if you are interested in Serverless technology innovation practices. Let's take Alibaba Cloud Function Compute (FC) as an example. Many PHP developers have interesting practices:

  • Use gd or ImageMagick extensions to implement various CPU-intensive APIs, such as elastic and highly available images and watermarks
  • Use ffmpeg + performance instance + asynchronous stateful call to complete audio and video processing services, such as video clip synthesis
  • Use functions implemented by HTTP triggers to perform event tracking to the advertising platform to implement high-availability buying services quickly
  • Migrate the WEB API previously implemented based on a framework (such as ThinkPHP) to the FaaS platform without worrying about downtime and operation and maintenance problems.

FaaS solves the following problems of PHPer:

  • Developing new business or a new web API
  • Some CPU-intensive or highly flexible APIs are pulled out for FaaS in the existing online business.

The traditional development mode or existing online business has a certain cost for developers to get started and transform. For example, the PHP Runtime programming interface of a FaaS vendor:

function handler($event, $context) {
     $eventObj = json_decode($event, $assoc = true);
    // do your thhings
     // ....
     return $eventObj['key'];
}

Can you go further? Developers do not need to implement APIs one by one according to the function entry agreed by FaaS vendors. Can developers make traditional projects running in LAMP or LEMP into FaaS?

Yes.

The Custom Runtime Function Compute of Alibaba Cloud and the minimalist programming model based on the HTTP protocol are at the forefront of all cloud vendors.

When the Function Compute starts the Custom Runtime execution environment, it calls the bootstrap file (or the Args parameter you set when you created the function) by default to start your custom HTTP server. Then, this HTTP server takes over all requests from the Function Compute system, which is all your function invocation requests.

The underlying system of the Function Compute Custom runtime execution environment is Linux and built-in nginx/1.10.3 and php-fpm7.4. You can directly use it for PHP applications.

Let's take the example of deploying a WordPress project. You only need to package the following directory into a zip package and create a function on the Function Compute platform:

- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- wordpress

The WordPress directory is the corresponding Web project. The bootstrap is the script to start Nginx and php-fpm:

...
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
...

Please see WordPress in FC for more information about bootstrap (link attached at the end).

Therefore, after the combination of Function Compute (Serverless product) and traditional PHP development, you no longer need to worry about SLB, scale up, machine management, or downtime. You only need to develop the business code.

6

As shown in the figure, developers only need to develop the business codes. The only thing to consider is the expansion must not go too far in Function Compute (for example, the maximum number of instances that this function can pop up directly under the Function Compute platform setting) and not put too much pressure on the downstream MySQL database.

When migrating from the original traditional PHP web application to the Serverless Function Compute platform, data persistence may be considered in some scenarios. Since Function Compute is stateless, data persistence can be completed with NAS, Redis, and other services. Let's take NAS as an example. The following is the flow chart:

7

Let's take WordPress as an example. Pictures or Session functions uploaded in the background system need to be persisted to disk.

  • Set the file upload directory or session directory of the Web project to a directory of the NAS disk, and the NAS disk is persistent.
  • Even the Web project can be placed on the NAS disk. At this time, Function Compute is the LNP execution environment.

8

For example, the WordPress project is not part of the code package of the function but is uploaded to the NAS disk in advance. It is only necessary to set up the root in nginx.conf to know the Web project. For example, the nginx.conf and /mnt/auto indicates the mounted NAS directory, while mnt/auto/wordpress indicates the Web project on NAS.

At this time, the function does not need to change anymore. You may need to develop new business code and upload it to NAS (or use Git to operate on NAS, realize the version of the web project and commit binding on Git, and use Git to realize quick code upgrade and rolling).

However, from the perspective of safe production, it is recommended that your Web engineering changes be associated with changes in functions.

Summary

From the discussion, it is not difficult to find that PHP + Serverless is an exciting thing, giving PHPer more imagination. The concept of Serverless is also the same asPHP. It allows developers to focus on business value. The PHP language has always been the best productivity representative in the web field, and Serverless will make PHP even more powerful.

Links in This Article

[1] Wikipedia: https://en.wikipedia.org/wiki/PHP

[2] W3Techs: https://w3techs.com/

[3] Overview of Custom Runtime: https://help.aliyun.com/document_detail/132044.html

[4] WordPress Project: https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/wordpress/src

[5] WordPress in FC: https://github.com/devsapp/start-web-framework/blob/master/web-framework/php/wordpress/src/code/bootstrap

Finally, Let's Answer the Question Raised in the Overview

Q1: What can an enterprise user do to deal with the increasing business volume or for a stable and available production environment?

A1: After using Function Compute and traditional PHP development, you no longer need to worry about SLB, scale up, machine management, downtime, etc. You only need to develop your business code.

Q2: If you are an enterprise user with a large number of developers on the project team, is it possible that there is no need for each developer to deploy a Linux machine with NLP installed?

A2: Yes. Each developer can create a Service/Function on Function Compute. The Service/Function configures the VPC of the development and test environment to implement intranet secure access to other downstream services, such as databases. When the function is called, Function Compute will pull an NLP execution environment to run the PHP code being developed on your branch.

  • Each execution environment is isolated from the other.
  • It bills based on the number of calls. There is no need to reserve machines, eliminating waste on machine costs.
  • It is also convenient to carry out various matters such as stress testing.

Q3: For an ISV, outsourcing company, or start-up company that provides website development and hosting, their customers are portals of some small and medium-sized enterprises. How can they improve the utilization rate of the backend machine resources and better provide customized services?

A3: Generally speaking, many enterprise portals have few page views, but if the website is hung up, it will cause customer complaints. Each customer's website is distinguished by service or function. Your customers are distinguished by function name or service:

  1. Convenient for management
  2. Convenient for customization
  3. Convenient for service at different VIP levels

For example, you can use the call metrics of a function to see which customer has a large website page view, make a customer profile, and set different fees and VIP service levels

Q4: Students (or those planning to learn PHP development) may only have a Windows system locally. Can they obtain the LNP (Linux + Nginx + PHP) environment for learning for free?

A4: Yes. Just package the following files and folders into zip packages and create the function in the Function Compute console:

- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- myweb
  | - hello.php

Serverless Best Practices for the PHP Framework

[1] ThinkPHP:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/thinkphp/src?

[2] Laravel:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/laravel/src?

[3] WordPress:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/wordpress/src?

[4] Z-BlogPHP:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/zblog/src?

[5] Swoole:
https://github.com/devsapp/start-fc/tree/master/custom-function/php74?

[6] More Information:
https://github.com/devsapp/start-web-framework

References

Appendix

Serverless Development in Frontend

Appendix 1Backend For Frontend (BFF) in Serverless to Improve Productivity

  • Frontend Developer Full Stack
  • The backend one only ensures the stability and reliability of the atomic interface to improve the development efficiency and reduce the communication time between the frontend and backend interface people. The data aggregation is realized by the frontend one through BFF.

9

Appendix 2 When SSR Meets Serverless, It Is Easy to Realize Page Instantaneous Opening

  • With the capability of Function as a Service (FaaS), a function can become a service without building a traditional Node application. Developers can focus on business logic.
  • FaaS is in the form of functions and elastic mechanisms, bringing natural isolation and dynamic repair capabilities to SSR applications. It can avoid cross-contamination between pages or some boundary abnormal scenarios that cause fatal damage to applications.
  • Features like no O&M, on-demand execution, and Auto Scaling reduce the threshold for developers of SSR applications.

Appendix 1: https://www.infoq.cn/article/0btajez51ysb_qehr526 (CN)

Appendix 2:
https://cnodejs.org/topic/5e394e311225c9423dcd9754 (CN)
https://www.alibabacloud.com/blog/open-pages-instantly-using-ssr-with-serverless_596160 (EN)

1 0 0
Share on

Alibaba Cloud Serverless

49 posts | 6 followers

You may also like

Comments

hack3r June 30, 2022 at 5:42 am

">