By Xiliu (Alibaba Cloud Function Compute Expert)
PHP has a wide range of applications, especially in the development of web programs. PHP has been installed on more than 244 million websites and 2.1 million servers since April 2013. According to the W3Techs report, as of September 2021, 78.9% of websites use PHP.
In terms of technology selection, PHP mainly uses LAMP (Linux + Apache + MySQL + PHP) or LNMP (Linux + Nginx + MySQL + PHP). The mature and stable technical framework promotes the prosperous development ecology and commercial success of PHP Web.
Developers need to install and maintain various software installation, maintenance, and upgrades in the traditional development mode:
If you are an enterprise user, SLB is an inevitable option to meet the increasing business volume or the stable and available production environment.
PHP developers (or those responsible for online operation and maintenance) care about more things:
If you are an enterprise user with a large number of developers on the project team, is it possible that there is no need for each developer to deploy a Linux machine with NLP installed?
For an ISV, outsourcing company, or start-up company that provides website development and hosting, their customers are portals of some small and medium-sized enterprises. How can they improve the utilization rate of the backend machine resources and provide better customized services?
Students (or those planning to learn PHP development) may only have a Windows system locally. Can they obtain the LNP (Linux + Nginx + PHP) environment for learning for free?
Let's explore how Serverless solves these pain points.
Serverless = Function as a Service (FaaS) + Backend as a Service (BaaS). We can understand the related concepts through the two graphs below:
The Alibaba Cloud Content Delivery Network (CDN) and OSS in the figure are BaaS services, and FC is the FaaS platform with custom function logic. We can learn the FaaS features and benefits through this comparison:
Serverless discussed later in this article mainly refers to FaaS. The following diagram shows that after several lines of code are written and saved to the FaaS platform of the cloud vendor, a flexible and highly available Web API is completed.
PHP is a large language for the development group. The FaaS of major cloud vendors, such as Alibaba Cloud's Function Compute (FC), AWS's Lambda (indirect support through Custom Runtime), Tencent's SCF, etc., have supported the PHP language. Please see the appendix at the end of this article if you are interested in Serverless technology innovation practices. Let's take Alibaba Cloud Function Compute (FC) as an example. Many PHP developers have interesting practices:
FaaS solves the following problems of PHPer:
The traditional development mode or existing online business has a certain cost for developers to get started and transform. For example, the PHP Runtime programming interface of a FaaS vendor:
function handler($event, $context) {
$eventObj = json_decode($event, $assoc = true);
// do your thhings
// ....
return $eventObj['key'];
}
Can you go further? Developers do not need to implement APIs one by one according to the function entry agreed by FaaS vendors. Can developers make traditional projects running in LAMP or LEMP into FaaS?
Yes.
The Custom Runtime Function Compute of Alibaba Cloud and the minimalist programming model based on the HTTP protocol are at the forefront of all cloud vendors.
When the Function Compute starts the Custom Runtime execution environment, it calls the bootstrap file (or the Args parameter you set when you created the function) by default to start your custom HTTP server. Then, this HTTP server takes over all requests from the Function Compute system, which is all your function invocation requests.
The underlying system of the Function Compute Custom runtime execution environment is Linux and built-in nginx/1.10.3 and php-fpm7.4. You can directly use it for PHP applications.
Let's take the example of deploying a WordPress project. You only need to package the following directory into a zip package and create a function on the Function Compute platform:
- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- wordpress
The WordPress directory is the corresponding Web project. The 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
...
Please see WordPress in FC for more information about bootstrap (link attached at the end).
Therefore, after the combination of Function Compute (Serverless product) and traditional PHP development, you no longer need to worry about SLB, scale up, machine management, or downtime. You only need to develop the business code.
As shown in the figure, developers only need to develop the business codes. The only thing to consider is the expansion must not go too far in Function Compute (for example, the maximum number of instances that this function can pop up directly under the Function Compute platform setting) and not put too much pressure on the downstream MySQL database.
When migrating from the original traditional PHP web application to the Serverless Function Compute platform, data persistence may be considered in some scenarios. Since Function Compute is stateless, data persistence can be completed with NAS, Redis, and other services. Let's take NAS as an example. The following is the flow chart:
Let's take WordPress as an example. Pictures or Session functions uploaded in the background system need to be persisted to disk.
For example, the WordPress project is not part of the code package of the function but is uploaded to the NAS disk in advance. It is only necessary to set up the root in nginx.conf to know the Web project. For example, the nginx.conf and /mnt/auto indicates the mounted NAS directory, while mnt/auto/wordpress indicates the Web project on NAS.
At this time, the function does not need to change anymore. You may need to develop new business code and upload it to NAS (or use Git to operate on NAS, realize the version of the web project and commit binding on Git, and use Git to realize quick code upgrade and rolling).
However, from the perspective of safe production, it is recommended that your Web engineering changes be associated with changes in functions.
From the discussion, it is not difficult to find that PHP + Serverless is an exciting thing, giving PHPer more imagination. The concept of Serverless is also the same asPHP. It allows developers to focus on business value. The PHP language has always been the best productivity representative in the web field, and Serverless will make PHP even more powerful.
[1] Wikipedia: https://en.wikipedia.org/wiki/PHP
[2] W3Techs: https://w3techs.com/
[3] Overview of Custom Runtime: https://help.aliyun.com/document_detail/132044.html
[4] WordPress Project: https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/wordpress/src
[5] WordPress in FC: https://github.com/devsapp/start-web-framework/blob/master/web-framework/php/wordpress/src/code/bootstrap
Q1: What can an enterprise user do to deal with the increasing business volume or for a stable and available production environment?
A1: After using Function Compute and traditional PHP development, you no longer need to worry about SLB, scale up, machine management, downtime, etc. You only need to develop your business code.
Q2: If you are an enterprise user with a large number of developers on the project team, is it possible that there is no need for each developer to deploy a Linux machine with NLP installed?
A2: Yes. Each developer can create a Service/Function on Function Compute. The Service/Function configures the VPC of the development and test environment to implement intranet secure access to other downstream services, such as databases. When the function is called, Function Compute will pull an NLP execution environment to run the PHP code being developed on your branch.
Q3: For an ISV, outsourcing company, or start-up company that provides website development and hosting, their customers are portals of some small and medium-sized enterprises. How can they improve the utilization rate of the backend machine resources and better provide customized services?
A3: Generally speaking, many enterprise portals have few page views, but if the website is hung up, it will cause customer complaints. Each customer's website is distinguished by service or function. Your customers are distinguished by function name or service:
For example, you can use the call metrics of a function to see which customer has a large website page view, make a customer profile, and set different fees and VIP service levels
Q4: Students (or those planning to learn PHP development) may only have a Windows system locally. Can they obtain the LNP (Linux + Nginx + PHP) environment for learning for free?
A4: Yes. Just package the following files and folders into zip packages and create the function in the Function Compute console:
- bootstrap
- nginx.conf
- php-fpm.conf
- php.ini-production
- myweb
| - hello.php
[1] ThinkPHP:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/thinkphp/src?
[2] Laravel:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/laravel/src?
[3] WordPress:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/wordpress/src?
[4] Z-BlogPHP:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/php/zblog/src?
[5] Swoole:
https://github.com/devsapp/start-fc/tree/master/custom-function/php74?
[6] More Information:
https://github.com/devsapp/start-web-framework
Serverless Development in Frontend
Appendix 1: https://www.infoq.cn/article/0btajez51ysb_qehr526 (CN)
Appendix 2:
https://cnodejs.org/topic/5e394e311225c9423dcd9754 (CN)
https://www.alibabacloud.com/blog/open-pages-instantly-using-ssr-with-serverless_596160 (EN)
Processing a Cartoon Style Avatar Applet Based on Serverless Architecture
Detailed Asynchronous Tasks: Task Status and Lifecycle Management
100 posts | 7 followers
FollowAlibaba Cloud Community - July 1, 2022
Alibaba Cloud Serverless - June 28, 2022
Alibaba Cloud Serverless - February 17, 2023
Alibaba Clouder - December 11, 2020
Alibaba Cloud Serverless - June 13, 2022
Alibaba Cloud Native Community - June 29, 2022
100 posts | 7 followers
FollowRespond to sudden traffic spikes and minimize response time with Server Load Balancer
Learn MoreA low-code development platform to make work easier
Learn MoreHelp enterprises build high-quality, stable mobile apps
Learn MoreAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreMore Posts by Alibaba Cloud Serverless