Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does PHP have to do with Serverless?

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article, the editor introduces in detail "what is the relationship between PHP and Serverless", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the relationship between PHP and Serverless" can help you solve your doubts.

Preface

PHP has a wide range of applications, especially in the development of web programs. According to the latest Wikipedia, PHP has been installed on more than 244m websites and 2.1 million servers in April 2013, while according to W3Techs, 78.9% of websites use PHP as of September 2021. So PHP is the first language in the world, at least not jokingly in the field of web development.

In terms of technology 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 technology framework promotes the prosperity of PHP web development ecology and commercial success.

In the traditional development model, developers need to install and maintain all kinds of software installation, maintenance and upgrade:

If you are an 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, at this time, PHP developers or online OPS students care about more things:

Each additional production machine needs to reinstall the relevant 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 require human flesh per machine to add the extension.

The load balancer is upgraded with the change of the business, and the latter Worker machine is down. How to do the operation and maintenance?

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

...

If you are an enterprise user with a large number of project team developers, can you configure each developer with an installed NLP Linux machine as a development test machine (or a machine shared by multiple people)?

If you are an ISV, outsourcing company or startup that provides website development and hosting, and my customers are portals for small and medium-sized enterprises, how can I improve the resource utilization of my back-end machines and better provide customized services?

If you are a student or are going to study PHP development, and there are only Windows computers locally, can you directly access the LNP (Linux+Nginx+PHP) environment for learning in a near-free way?

...

With these questions in mind, 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 can quickly understand the related concepts through two simple diagrams:

Traditional mode

Serverless mode

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

You only need to focus on business code development and write the corresponding logic.

Extreme elastic scaling, no need to manage the server

Pay by quantity, charged per millisecond for each call

...

The Serverless discussed later in this article mainly refers to FaaS. As shown in the following diagram, a few lines of code are written and saved to the cloud vendor's FaaS platform to complete an elastic and highly available Web API.

PHP meets Serverless.

PHP is a large language for developers. FaaS of major cloud vendors, such as Aliyun's functional computing, AWS's Lambda (indirectly supported by Custom Runtime), Tencent's SCF, etc., have launched support for PHP language. Phper should be as good as the practice of Serverless technology innovation in the front-end field (see the appendix at the end of this article). Take Ali Cloud function Computing as an example, many PHP developers have a lot of interesting practices:

Directly use gd or ImageMagick extensions to achieve elastic and highly available images, watermarks and other CPU-intensive API

Directly use ffmpeg + performance instance + asynchronous stateful call to complete audio and video processing services such as video clip synthesis.

Use the function realized by HTTP trigger to bury the point to the advertising platform to quickly realize the highly available buying volume business.

Directly migrate the previously implemented WEB API based on frameworks (such as ThinkPHP) to the FaaS platform without having to worry about downtime and operation and maintenance.

...

Although FaaS solves the following problems of phper very well:

New business or develop new web API

In the stock business, some CPU-intensive or highly flexible API are extracted and FaaS separately.

However, the traditional development model or stock business has a certain cost of getting started and reforming for developers, such as the PHP Runtime programming interface example 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 follow the function entry agreed by FaaS vendors to implement API one by one, but can directly FaaS traditional projects running on LAMP or LNMP?

The answer is yes.

The Custom Runtime of Ali cloud function computing and the minimalist programming model directly based on HTTP protocol are at the forefront of all cloud vendors.

When the function calculation starts the Custom Runtime execution environment, the bootstrap file (or the Args parameter you set when you create the function) is called by default to start your custom HTTP Server, and then the HTTP Server takes over all the requests of the function computing system, that is, all your function call requests.

The underlying system of the Custom runtime execution environment is Linux, and there are already built-in nginx/1.10.3 and php-fpm7.4. For PHP applications, you can use them directly.

To deploy a wordpress project as an example, you only need to package the following directory directly into a zip package to 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 that starts nginx and php-fpm:

... echo "start php-fpm" php-fpm7.4-c / code/php.ini-production-y / code/php-fpm.confecho "start nginx" nginx-c / code/nginx.conf...

For more information on bootstrap, please see WordPress in FC.

Therefore, after using function computing to combine this Serverless product with traditional PHP development, you no longer need to think about load balancing, capacity expansion, machine management, downtime, and so on. You just need to develop the business code with peace of mind.

As can be seen from the figure above: developers only need to develop their own business code, and the only thing to consider is not to expand the capacity of the function too much (for example, directly under the function computing platform settings, the maximum number of instances that can be popped up by the function), and put too much pressure on the downstream Mysql database.

Of course, in some scenarios, data persistence may need to be considered when migrating from the original traditional php web application to the Serverless-based function computing platform, because function computing is stateless and data persistence can be saved with the help of NAS, Redis and other services. Take NAS as an example, the flow chart is as follows:

Take WordPress as an example, images uploaded by backend systems or Session functions 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 make the NAS disk persistent.

You can even put the web project directly on the NAS disk, where the function calculation is purely the LNP execution environment.

For example, if you upload the wordpress project to NAS disk instead of being part of the function code package, you only need to set the root in nginx.conf to know the web project. For example, nginx.conf above, / mnt/auto represents the mounted NAS directory, and mnt/auto/wordpress represents the web project on the NAS.

At this point, for you, the function does not need to change any more. You may just need to develop new business code and upload it to NAS (or directly use git to operate directly in NAS to achieve the binding between the version of web project and commit on git, and use git to quickly upgrade and roll the code.)

However, from the point of view of production safety, it is recommended that the web project change should be associated with the function change.

Summary

From the above discussion and presentation, it is not difficult to find that it is exciting for PHP to meet Serverless, which gives phper more room for imagination. The concept of Serverless is also consistent with that of the language PHP: to allow developers to focus on their business value as much as possible. The PHP language has always been the best productivity representative in the web field, and Serverless will give PHP a boost.

Finally, let's answer the questions raised in the preface:

What do you do if you are an enterprise user and the business volume becomes larger or for the stability and availability of the production environment?

As stated above, with the combination of function computing and traditional PHP development, you no longer need to think about load balancing, capacity expansion, machine management, downtime, and so on. You just need to develop the business code with peace of mind.

If you are an enterprise user with a large number of project team developers, can you configure each developer with an installed NLP Linux machine as a development test machine (or a machine shared by multiple people)?

Yes, each developer can create his own Service/ function on function calculation. The Service/ function configures the VPC of the development and test environment to achieve secure access to the database and other downstream services in the intranet. When the function is called, the function evaluation pulls a NLP execution environment to run the PHP code that is being developed on your branch.

Each execution environment is isolated from each other.

Billed by the number of calls, there is no need to reserve the machine, which avoids the waste of machine cost.

It is also very convenient to carry out various matters such as pressure testing.

If you are an ISV, outsourcing company or startup that provides website development and hosting, and my customers are portals for small and medium-sized enterprises, how can I improve the resource utilization of my back-end machines and better provide customized services?

Generally speaking, many enterprise portals do not have a lot of visits, but the hanging of the website will cause customer complaints. Each customer's website is distinguished by service or function, and your own customer is distinguished by function name or service: I. Easy to manage ii. Customization is convenient for iii. It is convenient to provide services with different vip levels. For example, you can quickly use the call metrics of a function to see which customer has a large number of visits to the website, can make a customer profile, and set different charges and vip service levels.

If you are a student or are going to study PHP development, and there are only Windows computers locally, can you directly access the LNP (Linux+Nginx+PHP) environment for learning in a near-free way?

Yes, just package the following files and folders into a zip package to create functions in the calculation console.

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

After reading this, the article "what is the relationship between PHP and Serverless" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report