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?

preface

PHP has been installed on more than 244 million websites and 2.1 million servers as of April 2013, according to the latest Wikipedia statistics. According to W3Techs, 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 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
  • .
  • 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 a 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 indirect support), Tencent SCF and so on 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 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
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

For bootstrap details, see 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 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.

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). Quick code upgrades and mash-ups using Git)

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:

What if you are an enterprise user and your business volume is growing or for the stability and availability of your 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.

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

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

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?

Generally speaking, many enterprise portals do not receive much traffic, but a website failure can 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.

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?

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
Copy the code

The original link

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