First, a few important concepts emerge from this article:

Function Compute: Function Compute is an event-driven service that lets the user write code and upload it instead of managing the server. Function computing prepares computing resources and runs user code on an elastic scale, with users paying only for the resources consumed by the actual code running. Function calculation refer to more information. Fun: Fun is a tool to support Serverless application deployment, helping you easily manage functions, API gateways, logging services, and more. It helps you develop, build, and deploy through a resource configuration file (template.yml). More documentation references for Fun.

Note: The tips 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, designed for agile WEB application development and simplify enterprise application development. ThinkPHP has been designed for simplicity and usability since its inception, while maintaining excellent performance and minimal code. Publishing under the Apache2 open source license means you can use ThinkPHP for free, and even distribute/sell your applications based on ThinkPHP as open source or commercial products.

Results the preview

Click on the link to preview the two examples of deployed applications covered in this article:

  1. ThinkPHP official example: 13492727-1986114430573743.test.functioncompute.com
  2. Example: ThinkPHP blog 13500180-1986114430573743.test.functioncompute.com
  3. ThinkPHP blog example management background: 13500180-1986114430573743.test.functioncompute.com/admin

Environment to prepare

First, install Fun on the machine as described in Fun’s installation documentation.

PS: In this article, you don’t need to install Docker, just install Fun. The easiest way is to download executable binaries directly.

After the installation is complete, you can run fun –version to check whether fun was successfully installed.

First example: Quickly initialize and deploy a Sample ThinkPHP application

Initialize a ThinkPHP example:

composer create-project topthink/think tp
Copy the code

Run the test locally with this example:

php think run
Copy the code

You can see the effect locally:

The traditional development approach is to deploy a PHP application after the local development is complete. Usually we deploy a PHP application in the following steps:

  1. Purchase a physical machine, such as an ECS
  2. Bind the public IP address on the physical machine and install PHP, nginx, and php-fpm
  3. Configure nginx, phP-FPM, and upload applications to the machine
  4. Run the test

Among them, the most complex is the number of steps 2 and 3, because the configuration method may be different in different environments, there will be more pits. Let’s show you how to deploy an application to function computation.

fun deploy
Copy the code

With a single command, Fun automatically enters the deployment process, where the user simply presses a series of carriage returns. The process details are as follows:

  1. Fun detects that this is not a Fun project and prompts to help create it (just press Enter or type y)
  2. The Fun project is automatically created. Do you want to deploy it? You can press Enter or type y to confirm
  3. Fun will then deploy the application directly online

Deployment is completed, we can see, according to the successful deployment of log function calculation for our 13492727-1986114430573743 generates a temporary domain name.test.functioncompute.com, we can through the temporary domain directly access we’ve just deployed application.

Note: temporary domain name is used for demonstration and development only, it is valid, if used for production, please bind the registered domain name.

Second example: Quick migration of an existing ThinkPHP application

Here’s an example of an open source ThinkPHP blog: github.com/wolf-leo/Wo…

First we need to clone the blog:

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

Install dependencies using Composer:

composer install
Copy the code

Create a new database called blog_test on the mysql database you want to use. Then edit the config/database. PHP file to change the database address, user name, and password.

/ /... .
return [
    / /... .
    // Server address
    'hostname'= >'192.168.17.104'.// Database name Do not change the name of the test database. Otherwise, the default installation will fail
    'database'= >'blog_test'.// The user name needs to be changed
    'username'= >'root'.// The password needs to be changed
    'password'= >'root'./ /... .
];
Copy the code

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

$ php think run

ThinkPHP Development server is started On <http://127.0.0.1:8000/>
You can exit with `CTRL-C`
Document root is: /examples/php/Wolf-Blog/public
Copy the code

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

Once the local tests are clear, it’s time to deploy. Before deployment, we need to know that since the Runtime code directory itself cannot be modified, ThinkPHP will write some cache files in the Runtime directory. We need to put this cache file under/TMP. In our example is the thinkphp/library/think/App. PHP this file line 174 to do the following changes:

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

After the modification is complete, you can use the following command to deploy:

fun deploy
Copy the code

After the same step as the first example, we can get a temporary domain can access test: 13500180-1986114430573743.test.functioncompute.com. If you open the temporary domain name, you can preview the same effect as the local domain name.

You can also access 13500180-1986114430573743.test.functioncompute.com/admin open the application management background:

conclusion

This article focuses on deploying ThinkPHP applications to functional calculations. Compared to the traditional deployment method, not only is it not more complex, but also eliminates the purchase of machines, installation and configuration of Nginx, installation and configuration of PHP-FPM and other steps. Can be realized, the traditional ThinkPHP application in the local development, one click deployment to the remote directly used in production, and has elastic scaling, payment by volume, free operation and maintenance and other features.

If you have problems migrating your application, please join our dingding group 11721331 for feedback.

“Alibaba Cloud originators pay close attention to technical fields such as microservice, Serverless, container and Service Mesh, focus on cloud native popular technology trends and large-scale implementation of cloud native, and become the technical circle that knows most about cloud native developers.”