Luo Song (Xiliu)

preface

PHP is widely used, especially in the development of web programs. According to the latest Wikipedia [1], PHP has been installed on more than 244 million websites and 2.1 million servers as of April 2013. According to W3Techs [2] report, As of September 2021, PHP was used by 78.9% of websites. So PHP is the no. 1 language in the world at least in the world of Web development.

In terms of technology selection, PHP mainly adopts LAMP(full name is Linux + Apache + mysql + PHP) or LNMP(full name is Linux + nginx + mysql + PHP). This mature and stable technical framework has contributed to the flourishing and commercial success of the PHP Web development ecosystem.

In traditional development mode, developers need to install and maintain various software installation, maintenance and upgrade by themselves:

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

At this point, PHP developers or online operations students are concerned about more things:

  • Each additional production machine will need to be re-installed with the relevant software, do the same nginx configuration and PHP-FPM configuration, and maintain security updates for each production machine;

  • If you develop an application that requires a new extension, you may need to add extensions on each machine.

  • As the load balancer is upgraded with the business change, a Worker machine behind it is down. How to handle the operation and maintenance?

  • How to deal with the peaks and troughs of the business in order to improve the utilization of resources?

If you are an enterprise user with a large number of developers on the project team, can you not configure an installed NLP Linux machine for each development as a development test machine (or share one machine with multiple people)?

If you are an ISV, outsourcing company or startup that provides website development and hosting, and my clients are some small and medium enterprise portals, how can I improve my backend machine resource utilization and better provide customized services?

If you are a student or want to learn PHP development and only have a Windows PC, can you directly access LNP(Linux+Nginx+PHP) environment for almost free?

With these questions in mind, let’s explore how Serverless solves these pain points.

PHP met Serverless

What is Serverless?

Serverless = Faas (Function as a service) + Baas (Backend as a service). We can learn about related concepts by using two diagrams:

  • The traditional model

  • Serverless mode

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

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

Serverless, discussed later in this article, mainly refers to FaaS, as shown in the diagram below. After a few lines of code are written and saved to the CLOUD vendor’s FaaS platform, you have an elastic and highly available Web API.

PHP met Serverless

PHP as a development group of a large language, the FaaS of major cloud manufacturers, such as Ali Cloud function computing, AWS Lambda (through Custom Runtime [3] indirect support), Tencent SCF have launched support for PHP language, Phper’s Serverless technology innovation practices in the front-end domain (see the appendix at the end of this article for those of interest) should be no less impressive. Take Ali Cloud function calculation as an example, there are many PHP developers have many interesting practices:

  • Use gd or ImageMagick extensions directly to implement flexible, highly available images, watermarks and other CPU-intensive apis

  • Directly use FFMPEG + performance instance + asynchronous stateful call to complete video clip synthesis and other audio and video processing services

  • Use HTTP triggers to implement functions, buried in the advertising platform, quickly achieve high availability of buy volume business

  • Migrate WEB apis that were previously implemented based on frameworks such as ThinkPHP directly to the FaaS platform without worrying about downtime and maintenance issues

While FaaS does a good job of solving the following problems with PHper:

  • New business or developing new Web apis

  • Some CPU – intensive or elastic – demanding apis are separated from storage services for FaaS

However, the traditional development mode or the existing business, developers have a certain start and transformation cost, for example, a Faas manufacturer PHP Runtime programming interface example:

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

But can we go one step further, and instead of implementing API after API according to the convention of FaaS vendors, developers can FaaS traditional projects running on LAMP or LNMP directly? The answer is yes.

Ali Cloud’s Custom Runtime function calculation and the minimalist programming model directly based on HTTP protocol walk in the forefront of all cloud vendors.

By default, the bootstrap file (or the Args parameter you set when you created the function) is called to start your Custom HTTP Server. The HTTP Server then takes over all requests from the function computing system. That’s all of your function call requests.

Nginx /1.10.3 and PHP-FPM7.4 have been built into the Linux system. For PHP applications, you can directly use it

To deploy a wordpress [3] project as an example, simply package the following directories into a ZIP package to create a function in the function computing platform:

- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- wordpress
Copy the code

Bootstrap nginx and phP-fpm bootstrap nginx and FPM

. Ini -production -y /code/php-fpm.conf echo "start nginx" nginx -c /code/nginx.conf ...Copy the code

Bootstrap details can be found in WordPress in FC [5] (link at the end of this article)

So, using function computing to combine this Serverless product with traditional PHP development, you don’t have to worry about load balancing, scaling, managing machines, downtime, etc., just worry about developing your business code.

Can be seen from above: a developer you just need to develop their business code as well, the only things to consider, is a function expansion and calculated here don’t too much, too fierce directly in the function (such as computing platform setting the function can pop up the largest number of instances), to the downstream own Mysql database too much pressure.

Of course, from the original traditional PHP Web application to the Serverless form of functional computing platform, some scenarios may need to consider the problem of data persistence, because function calculation is stateless, data persistence can be completed by NAS, Redis and other services. Take NAS as an example. The flow chart is as follows:

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

  • Set the file upload directory or session directory of the Web project to a directory on the NAS disk. The NAS disk implements persistence

  • You can even put the Web project directly on the NAS disk, where the function calculation is purely an LNP execution environment

For example, if the wordpress project is not part of the code package of the function, it is uploaded to the NAS disk in advance, just need to set the root of the nginx.conf to know the web project, as shown in the nginx.conf. / MNT /auto indicates the mounted NAS directory, and MNT /auto/wordpress indicates the Web project on the NAS.

At this point, you don’t need to change the function, you just need to develop new business code and upload it to NAS (or use Git directly on NAS to implement web project version and commit binding on Git, and use Git to implement fast code upgrade and roll).

However, from a production safety perspective, it is recommended that your Web engineering changes be associated with function changes.

conclusion

From the discussion and presentation above, it’s not hard to see how PHP meets Serverless is an exciting event that gives Phper a lot more room to imagine. Serverless’s philosophy is the same as the philosophy behind the emergence of PHP as a language: to let developers focus on their business value. The PHP language has long been the most productive proxy for the Web, and Serverless will make PHP even more powerful.

A link to the

[1] Wikipedia:

zh.wikipedia.org/wiki/PHP?

[2] W3Techs:

w3techs.com/?

[3]Custom Runtime

Help.aliyun.com/document_de…

[4] the wordpress project:

Github.com/devsapp/sta…

[5] in FC WordPress:

​https://github.com/devsapp/start-web-framework/blob/master/web-framework/php/wordpress/src/code/bootstrap?​​

Let’s finish by answering the questions raised in the introduction:

Q1: What should you do if you are an enterprise user and your business volume increases or for the stability and availability of your production environment?

A1: As stated above, with functional computation combined with traditional PHP development, you don’t have to worry about load balancing, scaling, managing machines, worrying about downtime, etc., just worry about developing your business code.

Q1: If you are an enterprise user with a large number of developers in the project team, do you need to configure an installed NLP Linux machine for each developer as a development test machine (or share one machine with several people)?

A1: Yes, each developer can create his/her own Service/ function on the function calculation. The Service/ function configures the VPC of the development test environment, and realizes the secure access of the Intranet to the database and other downstream services. When the function is called, the function evaluation pulls an NLP execution environment to run the PHP code you are developing on your branch.

  • Each execution environment is isolated from the other
  • Charging according to the number of calls, there is no need to reserve the machine, eliminating the waste on the machine cost
  • Can also be very convenient for pressure measurement and other matters
Q1: If you are an ISV, outsourcing company or start-up company providing website development and hosting, and my clients are some small and medium-sized enterprise portals, how can I improve the utilization of my back-end machine resources and provide better customized services?

A1: Generally speaking, many enterprise portals have little traffic, but the failure of the website will cause customer complaints. Each customer’s website is distinguished by service or function, and your own customers are distinguished by function name or service: I. Convenient for management ii. Convenient for customization III. Convenient for different VIP level services. For example, you can quickly see which customers have the most visits to a website through a function call indicator, make customer profiles, and specify different rates and VIP service levels.

Q1: If you are a student or want to learn PHP development, can you get LNP(Linux+Nginx+PHP) environment almost free of charge?

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

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

If you are interested in PHP Serverless, if you have views, ideas or want to make fun of, you can contact Dingdingsearch: 31897696, welcome to join the group to communicate with us.

PHP Framework Serverless best practices:

[1] ThinkPHP:

Github.com/devsapp/sta…

[2] Laravel:

Github.com/devsapp/sta…

[3] WordPress:

Github.com/devsapp/sta…

[4] Z – BlogPHP:

Github.com/devsapp/sta…

[5] Swoole:

Github.com/devsapp/sta…

[6] More:

https://github.com/devsapp/start-web-framework

Refer to the reference

Serverless Architectures:

Martinfowler.com/articles/se…

Backend For Frontend (BFF) in Serverless:

www.infoq.cn/article/0bt…

Specific thoughts on Serverless’s future impact on front-end development:

Developer.aliyun.com/article/793…

When SSR meets Serverless, easily realize page instant opening:

Cnodejs.org/topic/5e394…

The appendix

Serverless is in full swing in the front end

Appendix 1

Backend For Frontend (BFF) in Serverless to improve productivity

• Full stack for front-end developers

• Improve the development efficiency, reduce the communication and coordination time between front-end and back-end interface students, back-end students only need to do a good job in the stability and reliability of atomic interface, data aggregation is directly realized by the front-end students through BFF

Appendix 2

When SSR meets Serverless, easily realize page instant opening

• With function-as-a-service (FaaS) capabilities, a function can become a service without having to build a traditional Node application, allowing developers to focus more purely on business logic.

• FaaS, in the form of function unit and elastic mechanism, brings natural isolation and dynamic repair ability to SSR applications, which can better avoid cross-contamination between pages or fatal damage to applications caused by abnormal scenes at the boundary.

• No need for operation and maintenance, on-demand execution and flexible scaling greatly reduce the threshold of SSR applications for developers.

Appendix 1:

www.infoq.cn/article/0bt…

Appendix 2:

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

Release the latest information of cloud native technology, collect the most complete content of cloud native technology, hold cloud native activities and live broadcast regularly, and release ali products and user best practices. Explore the cloud native technology with you and share the cloud native content you need.

Pay attention to [Alibaba Cloud native] public account, get more cloud native real-time information!