Summary: PHP is a very large language for the development community with a wide range of applications. Serverless’s philosophy and the philosophy of the PHP language are to let developers focus on their business value. So what happens when PHP meets Serverless?

Hey, phpers, Serverless

The author | west flow

PHP has a wide range of applications, especially for web applications, installed on more than 244 million websites and 2.1 million servers, according to the most recent Wikipedia statistics, as of April 2013. As of September 2021, 78.9 percent of websites use PHP, according to W3Techs. 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:

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

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

  • Each additional production machine needs 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
  • .

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

3, if you are a student or want to learn PHP development, the local only Windows PC, can directly access LNP(Linux+Nginx+PHP) environment for almost free way to learn?

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

PHP met Serverless

1. What is Serverless

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

1) Traditional model

2) Serverless mode

In the figure, 1,2 are third-party BaaS services, and 4,5 are FaaS of 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 meets Serverless

Before we get to PHP, let’s take a look at Serverless’s growth in the front-end space:

1) Backend For Frontend (BFF) in Serverless to improve productivity

  • Front-end developer full stack
  • 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.

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.
  • Features such as no operation and maintenance, on-demand execution and elastic scaling greatly reduce the threshold of SSR application for developers.

PHP is a very big language for the development group. FaaS of major cloud manufacturers (such as Ali Cloud’s function computing, AWS’s Lambda, Tencent’s SCF, etc.) have all launched support for PHP language. Phper’s Serverless technology innovation practice in the front end field. 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:

return $eventObj[‘key’];

}

use RingCentral\Psr7\Response;

function handler(
r e q u e s t , request,
context): Response {

/ *


b o d y = body =
request->getBody()->getContents();


q u e r i e s = queries =
request->getQueryParams();


m e t h o d = method =
request->getMethod();


h e a d e r s = headers =
request->getHeaders();


p a t h = path =
request->getAttribute(“path”);

* /

return new Response(

In 200,

array(

“custom_header1” => “v1”,

“custom_header2” => [“v2”, “v3”],

“Set-Cookie” => urlencode(“test php”) . ‘=’ . urlencode(‘test; more’)

),

“hello world”

);

}

But can we go one step further, and instead of implementing API after API according to the conventions of THE FaaS vendors, we can directly FaaS traditional projects running on LAMP or LNMP?

The answer is yes.

Ali Cloud function calculation Custom Runtime and direct HTTP protocol based on the minimalist programming model in the forefront of all vendors.

The bootstrap file is invoked by default to start your Custom HTTP Server when the Custom Runtime execution environment is evaluated. This HTTP Server then takes over all the requests from the function computation system, that is, all your function call requests.

Nginx and phP-FPM7.4 have been built into the nginx and PHP-FPM7.4, PHP applications, you can directly use it.

To deploy a wordpress project, 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

Bootstrap nginx and phP-fpm bootstrap nginx and 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

.

(Bootstrap details can be found in WordPress in FC)

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 a Serverless or container form of cloud products, there may be such a problem, take WordPress for example, before the background system upload images or use the Session function, All are directly saved to disk, the function calculates the popup instance, may not be called for a long time will be recycled, but I want to have disk persistence function, at this time, the flow chart is as follows:

(Function calculation requires only a simple configuration to ensure VPC network security and NAS mounting.)

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

You may just need to develop new business code and upload it to the NAS (or use Git directly on the NAS to implement the web project version and commit binding on Git). Use Git for quick code upgrades and mash-ups.

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

summary

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.

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

1. What if you are an enterprise user and your business volume increases or for the stability and availability of the production environment?

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, and so on, just worry about developing your business code.

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

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

3, if you are a student or want to learn PHP development, the local only Windows PC, can directly access LNP(Linux+Nginx+PHP) environment for almost free way to learn?

Yes, just pack the following files and folders into a ZIP package and go to the function calculation console to create the function

– bootstrap

– nginx.conf

– php-fpm.conf

– php.ini-production

– myweb

| – hello.php

The original link

This article is the original content of Aliyun and shall not be reproduced without permission.