foreword
PHP is widely used, especially in the development of web programs. According to the latest Wikipedia , as of April 2013, PHP has been installed on more than 244 million websites and 2.1 million servers. , and according to W3Techs , 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 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 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 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 be necessary to add the extension to 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 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 a Linux machine with NLP installed for each development as a development and testing machine (or share a machine with multiple people)?
- If you are an ISV, outsourcing company or start-up company that provides website development and hosting, 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 are going 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?
- ...
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:
- traditional 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, an elastic 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's Lambda (indirectly supported through Custom Runtime), Tencent's SCF, etc., have launched support for the PHP language, phper Faced with the innovation practice of Serverless technology in the front-end field (see the appendix at the end of this article if you are interested), it should not be too much. Taking Alibaba Cloud Function Compute as an example, many PHP developers have had a lot of interesting practices:
- Directly use gd or ImageMagick extensions to achieve flexible and highly available pictures, 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 vendor, but can directly FaaS projects that traditionally run on LAMP or LNMP?
The answer is yes
Cloud Function Compute's 161e90f892252a 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 by default (or the Args parameter you set when you created the function) 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
Taking the deployment of a wordpress project as an example, you only need to package the following directories 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 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
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.
As can be seen from the above figure: 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 or too violent (for example, directly under the function computing platform settings, the function can pop up 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 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, such as 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.
summary
From the above discussion and statement, it is not difficult to find that PHP meets Serverless is an exciting thing, which allows phper to have more room for imagination. The concept of Serverless is also consistent with the concept of the PHP language: that is, to allow developers to focus on their own business value. The PHP language has always been the best productivity representative in the web field, and Serverless will make PHP even more powerful.
Finally, let's answer the questions raised in the preface one by one:
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?
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 just need to feel at ease Just develop the business code by heart.
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)?
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
If you are an ISV, outsourcing company or start-up company that provides website development and hosting, 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?
Generally speaking, many enterprise portal websites have little traffic, but the website is down, which 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.
If you are a student or are going 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?
Yes, just package the following files and folders into a zip package and go to the function computing console to create a function
- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- myweb
| - hello.php
A DingTalk group has been built here: 31897696. If you are interested in the implementation of Serverless in PHP, and you have opinions, ideas or want to complain, you can communicate with everyone.
PHP Framework Serverless Best Practices
Other more: https://github.com/devsapp/start-web-framework
reference
- Serverless Architectures
- Backend For Frontend(BFF)in Serverless
- Specific views on the future impact of Serverless on front-end development
- When SSR meets Serverless, it is easy to realize instant page opening
appendix
Serverless is developing in full swing in the front-end field:
Backend For Frontend (BFF) in Serverless to increase productivity
- Front-end developer full stack
- Improve development efficiency and reduce the 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.
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 fatal effects to applications. harm.
- The features of no operation and maintenance, on-demand execution, and elastic scaling greatly reduce the threshold for SSR applications for developers.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。