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

Easy to build ThinkPHP applications based on Serverless

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

First, let me introduce some of the more important concepts that appear in this article:

Function Compute: Function Compute is an event-driven service. Through function calculation, users do not need to manage the operation of the server, just write code and upload it. Function calculations prepare compute resources and run user code in an Auto Scaling fashion, while users pay only for the resources consumed by the actual code run. For more information, see.

Fun: Fun is a tool to support Serverless application deployment, helping you to easily manage resources such as function calculations, API gateways, logging services, and more. It assists you with development, build, and deployment through a resource profile (template.yml). More documentation for Fun.

Note: The techniques described in this article require Fun version 3.6.3 or greater.

What is ThinkPHP?

ThinkPHP is a free, open source, fast, simple, object-oriented lightweight PHP development framework that was born for agile WEB application development and simplified enterprise application development. ThinkPHP has been designed with simplicity and practicality since its birth, focusing on ease of use while maintaining excellent performance and simplicity of code. Distributing under the Apache2 Open Source License means you can use ThinkPHP for free and even open source or distribute/sell your ThinkPHP-based applications commercially.

effect preview

This article involves two application examples after deployment links, click to preview the effect:

ThinkPHP official example: http://13492727-1986114430573743.test.functioncompute.comThinkPHP Blog example: http://13500180-1986114430573743.test.functioncompute.comThinkPHP Blog example Admin background: http://13500180-1986114430573743.test.functioncompute.com/admin Environment preparation

First install Fun to your computer as described in the Fun installation documentation.

PS: This article describes the method, do not need to install Docker, just install Fun, the easiest way is to download the executable binary file directly.

After the installation is complete, you can run fun -version to check if Fun was installed successfully.

First example: Quickly initialize and deploy a ThinkPHP sample application

Initialize a thinkphp example:

composer create-project topthink/think tp

Run the example locally:

php think run

You can see the effect locally:

The traditional way of development is that after the local development is completed, it is necessary to deploy the work. Usually, we deploy a PHP application in the following steps:

Buy a physical machine, such as ECS bind public ip on the physical machine, install php, nginx, php-fpm configure nginx, php-fpm and upload the application to run tests on the machine

Among them, the most complicated ones are the second and third steps, because the configuration methods of different environments may be different, and there will be more pits. Let's demonstrate how to deploy an application to a function computation.

fun deploy

With just one command, Fun automatically enters the deployment process, where the user simply presses a series of Enter. Details of the process are as follows:

Fun detects that this is not a Fun project and prompts for assistance in creating it (enter directly or enter y)

Fun project automatically created successfully, prompt whether to deploy? You can enter or you can enter y to confirm.

Fun will then deploy the app directly online

After the deployment is complete, we can see from the log of successful deployment that the function calculation generates a temporary domain name 13492727-1986114430573743.test.functioncompute.com for us, through which we can directly access the application we just deployed.

Note: temporary domain name is only used for demonstration and development, is time-limited, if used for production, please bind the domain name has been filed.

Second example: Quickly migrate an existing ThinkPHP application

Here's an example of an open source ThinkPHP blog: https://github.com/wolf-leo/Wolf-Blog

First we need to clone the blog:

git clone https://github.com/wolf-leo/Wolf-Blog.git

Using composer to install dependencies:

composer install

Create a new database named blog_test on the mysql database you want to use. Then edit the config/database.php file and modify the database address, username, and password information.

// ... ... return [ // ... ... //Server address 'hostname' => '192.168.17.104', //database name test database name do not modify otherwise default installation will error 'database' => 'blog_test', //User name needs to be modified 'username' => 'root', //Password Where changes are needed 'password' => 'root', // ... ...];

After the modification is completed, you can start to view the effect locally:

$ php think runThinkPHP Development server is started On You can exit with `CTRL-C`Document root is: /examples/php/Wolf-Blog/public

Then open http://localhost:8000 to see the effect directly.

Once the local tests are OK, it's time to deploy. Before deployment, we need to know that since the function calculation runtime code directory itself cannot be modified, and ThinkPHP will write some cache files in the Runtime directory under the code directory, we need to put this cache file under/tmp. In our example, we modify line 174 of thinkphp/library/think/App.php as follows:

- $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;+ $this->runtimePath = DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;

Once the modification is complete, you can deploy it directly with the following command:

fun deploy

After going through the same steps as in the first example, we can get an accessible temporary domain name to test: 13500180 - 1986114430573743.test.functioncompute.com. Open the temporary domain name, you can preview the same effect as the local run.

You can also visit 13500180-1986114430573743.test.functioncompute.com/admin to open the app's admin backend:

summary

This article focuses on how to deploy ThinkPHP applications to functional computing. Compared with the traditional deployment method, not only is it not more complicated, but it also omits the steps of purchasing a machine, installing Nginx, installing php-fpm, etc. It can realize that after the traditional ThinkPHP application is developed locally, it can be deployed to the remote end directly for production with one click, and has the characteristics of Auto Scaling, Pay-by-Amount, OPM-free, etc.

If you are having problems migrating your app, please join our dingding group 11721331 Feedback.

"Alibaba Cloud focuses on microservices, Serverless, containers, Service Mesh and other technical fields, focuses on cloud native popular technology trends, cloud native large-scale landing practices, and is the technical circle that understands cloud native developers best. "

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

Servers

Wechat

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

12
Report