1

Author: Luo Song (West Flow)

foreword

PHP is widely used, especially in the development of web programs. According to the latest Wikipedia [1] , in April 2013, PHP has been installed on more than 244 million websites and 2.1 million server, and according to W3Techs [2] report, as of September 2021, 78.9% of websites use PHP. So PHP is the world's first language at least in the field of web development is not a joke.

In terms of technical selection, PHP mainly uses LAMP (full name is Linux + apache + mysql + php) or LNMP (full name is Linux + nginx + mysql + php), this mature and stable technical framework promotes PHP web development ecology prosperity and business success.

In the traditional development mode, developers themselves need to install and maintain various software installations, maintenance and upgrades:

  • If you are a enterprise user , if the business volume becomes larger or for the stability and availability of the production environment, using load balancing is an inevitable option:

That is to say, at this time, PHP developers or online operation and maintenance students are concerned about more things:

  • Each additional production machine needs to reinstall related software, do the same nginx configuration and php-fpm configuration, and maintain security updates for each production machine;
  • If the developed application requires a new extension, it may be necessary to increase the extension for each machine;
  • The load balancer is upgraded with the change of the business, and the next Worker machine hangs up. How to do the operation and maintenance?
  • How to deal with the peaks and valleys of the business to improve the utilization of resources?

If you are an enterprise user with a large number of development members in the project team, can you not need to configure an installed NLP Linux machine for each development as a development and testing machine (or share a machine with multiple people)?

If you are an providing website development and hosting, outsourcing company or a start-up company, and my clients are portal websites of some small and medium-sized enterprises, how can I improve the utilization of my back-end machine resources and provide better customized services?

If you are a student or prepare to learn PHP development , and you only have Windows computers locally, can you get the LNP (Linux+Nginx+PHP) environment for learning in a nearly free way?

With these questions, let's explore how Serverless solves these pain points.

PHP meets Serverless

What is Serverless?

Serverless = Faas (Function as a service) + Baas (Backend as a service), we simply use two diagrams to quickly understand related concepts:

  • Legacy Mode

  • Serverless mode

CDN and OSS in the figure are BaaS services, and FC is a FaaS platform for custom function logic. Through this comparison, we can quickly get the features and benefits of FaaS:

  • Just focus on business code development and write the corresponding logic
  • Extremely elastic scaling, no need to manage servers
  • Pay-as-you-go, billed by milliseconds per call
  • ...

Serverless discussed later in this article mainly refers to FaaS, as shown in the following diagram, after writing a few lines of code and saving it to the cloud vendor's FaaS platform, a flexible and highly available Web API is completed.

PHP meets Serverless

PHP is a large language for development groups. FaaS of major cloud vendors, such as Alibaba Cloud's function computing, AWS' Lambda (indirectly supported through Custom Runtime [3] ), Tencent's SCF, etc. With language support, phper should be able to face the serverless technology innovation practice in the front-end field (see the appendix at the end of this article if you are interested). Taking Alibaba Cloud Function Compute as an example, many PHP developers have had many interesting practices:

  • Directly use gd or ImageMagick extensions to achieve flexible and highly available images, watermarks and other CPU-intensive APIs
  • Directly use ffmpeg + performance instance + asynchronous stateful call to complete audio and video processing services such as video clip synthesis
  • Use the functions implemented by HTTP triggers to bury points in the advertising platform to quickly realize high-availability purchase business
  • Directly migrate the WEB API implemented based on the framework (such as ThinkPHP) to the FaaS platform directly, no longer need to worry about downtime and operation and maintenance problems

Although FaaS solves the following problems of phper very well:

  • New business or develop new web API
  • In the stock business, some APIs that are CPU-intensive or have high flexibility requirements are separated out separately and become FaaS.

However, the traditional development model or stock business has a certain cost for developers to start and transform. For example, an example of the PHP Runtime programming interface of a Faas manufacturer:

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

But can we go further, developers do not need to implement APIs one by one according to the function entry agreed by the FaaS manufacturer, but can directly convert the projects traditionally running on LAMP or LNMP into FaaS? The answer is yes.

Alibaba Cloud Function Computing's Custom Runtime and the minimalist programming model directly based 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 (or the Args parameter you set when you created the function) by default to start your custom HTTP Server, and then this HTTP Server takes over all requests from the Function Compute system, that is, all your function call request.

The underlying system of the function computing 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

Take the deployment of a wordpress [3] project as an example, just package the following directory into a zip package and create a function on the function computing platform:

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

The wordpress directory is the corresponding web project, and 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
...
For details of bootstrap, please refer to WordPress in FC [5] (link at the end of the article)

Therefore, after combining the serverless product of function computing with traditional PHP development, you no longer need to think about load balancing, scaling, managing machines, worrying about downtime, etc., you only need to You can develop the business code with peace of mind.

It can be seen from the above figure that developers only need to develop their own business code. The only thing to consider is that the expansion of function computing should not be too much (for example, the function can pop up directly under the function computing platform settings) The maximum number of instances is enough), and it is enough to put too much pressure on the downstream Mysql database.

Of course, to completely migrate from the original traditional php web application to the serverless function computing platform, some scenarios may need to consider the problem of data persistence, because function computing is stateless, and data persistence can be saved with the help of NAS, Redis and other services Complete, take NAS as an example, the flow chart is as follows:

Taking WordPress as an example, the images uploaded by the background system or the Session function need to be persisted to the disk.

  • Set the file upload directory or session directory of the web project to a directory on the NAS disk, and the NAS disk is persistent.
  • You can even put the web project directly on the NAS disk. At this time, the function calculation is purely the LNP execution environment.

For example, if the wordpress project is not part of the code package of the function, but has been uploaded to the NAS disk in advance, you only need to set the root in nginx.conf to know the web project. For example, in the above nginx.conf, /mnt/auto means hanging NAS directory on the NAS, mnt/auto/wordpress represents the web project on the NAS.

At this point, for you, the function does not need to change anymore, you may just need to develop new business code, and then upload it to the NAS (or use git to operate directly on the NAS to realize the version of the web project and the code on git) commit binding, use git to achieve rapid code upgrade and shuffle)

However, from the perspective of safety production, it is recommended that your web project changes are best associated with function changes.

Summarize

From the above discussion and statement, it is not difficult to find that PHP meets Serverless is an exciting thing, which gives phper more room for imagination. The concept of Serverless is also consistent with the concept of the language PHP: allows developers to focus on their own business value. PHP language has always been the best productivity representative in the web field, and Serverless will make PHP even more powerful.

Related Links​

[1] Wikipedia:

[](https://zh.wikipedia.org/wiki/PHP?)

https://zh.wikipedia.org/wiki/PHP?

[2]W3Techs:

[](https://w3techs.com/?)

https://w3techs.com/?

[3] Introduction to Custom Runtime:

[](https://help.aliyun.com/document_detail/132044.html?)

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?)

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, we will answer the questions raised in the preface one by one:

Q1: If you are an enterprise user and the business volume becomes larger or for the stability and availability of the production environment, what should you do?

A1: As stated above, after combining function computing with traditional PHP development, you no longer need to think about load balancing, scaling, managing machines, worrying about downtime, etc., you only need to You can develop the business code with peace of mind.

Q1: If you are an enterprise user with a large number of development members in the project team, can you not need to configure a Linux machine with NLP installed for each development as a development and testing machine (or share a machine with multiple people)?

A1: Yes, each developer can create their own service/function on Function Compute. The Service/function configures the VPC of the development and test environment, and enables secure access to the database and other downstream services on the intranet. When a function is called, Function Compute pulls an NLP execution environment to run the PHP code being developed on your branch.

  • Each execution environment is isolated from each other
  • Billing according to the number of calls, no need to reserve machines, eliminating the waste of machine costs
  • It can also be very convenient for various matters such as pressure testing
Q1: If you are an ISV, outsourcing company or start-up company that provides website development and hosting, and my customers are portal websites of some small and medium-sized enterprises, how can I improve the utilization of my back-end machine resources and provide better customized services?

A1: Generally speaking, many enterprise portal websites do not receive much traffic, but if the website goes down, it will cause customer complaints. Each customer's website is distinguished by service or function, and your own customers can be distinguished by function name or service: i. easy to manage ii. easy to customize iii. easy to do different VIP level services. For example, you can quickly pass the call indicators of a function, you can see which customer's website has a large number of visits, you can make customer portraits, and formulate different charges and VIP service levels.

Q1: If you are a student or planning to learn PHP development, and you only have Windows computers locally, can you obtain an LNP (Linux+Nginx+PHP) environment for learning in a nearly free way?

A1: Yes, just package the following files and folders into a zip package and go to the Function Compute console to create a function:

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

If you are interested in the implementation of serverless in PHP, and you have opinions, ideas or want to complain, you can search with Dingding: 31897696, welcome to join the group to communicate.

PHP Framework Serverless Best Practices:

[1]ThinkPHP:

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

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?)

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?)

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?)

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?)

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

[6] Other more:

[]()

[ https://github.com/devsapp/start-web-framework]()

reference

Serverless Architectures:

[](https://martinfowler.com/articles/serverless.html?)

https://martinfowler.com/articles/serverless.html?

Backend For Frontend(BFF)in Serverless:

[](https://www.infoq.cn/article/0btajez51ysb_qehr526?)

https://www.infoq.cn/article/0btajez51ysb_qehr526?

Specific views on the future impact of Serverless on front-end development:

[](https://developer.aliyun.com/article/793492?)

https://developer.aliyun.com/article/793492?

When SSR meets Serverless, it is easy to open pages instantly:

[](https://cnodejs.org/topic/5e394e311225c9423dcd9754?)

https://cnodejs.org/topic/5e394e311225c9423dcd9754?

appendix

Serverless is in full swing in the front-end field

Appendix 1

Backend For Frontend (BFF) in Serverless to increase productivity

• Full stack for front-end developers

• Improve development efficiency and reduce communication and joint debugging time between front-end and back-end interface students. Back-end students only need to do the stability and reliability of atomic interfaces. Data aggregation is directly realized by front-end students through BFF.

Appendix 2

When SSR meets Serverless, it is easy to realize instant page opening

• With the help of Function as a Service (FaaS) capabilities, there is no need to build traditional Node applications, a function can be turned into a service, and developers can focus more purely on business logic.

• In the form of functions and elastic mechanisms, FaaS brings natural isolation and dynamic repair capabilities to SSR applications, which can better avoid cross-contamination between pages, or some abnormal border scenarios bring fatalities to applications s damage.

• Features such as no operation and maintenance, on-demand execution, and elastic scaling greatly reduce the threshold for SSR applications for developers.

Appendix 1:

[](https://www.infoq.cn/article/0btajez51ysb_qehr526?)

https://www.infoq.cn/article/0btajez51ysb_qehr526?

Appendix 2:

​https://cnodejs.org/topic/5e394e311225c9423dcd9754?​


阿里云云原生
1k 声望302 粉丝