Detailed explanation of the steps of deploying thinkphp5 project on cloud virtual host

  • 2021-08-28 19:29:52
  • OfStack

Preface

Thinkphp, as an open source PHP framework, is undoubtedly the most suitable framework for PHP students to learn and use in China. Besides being simple and easy to use, Thinkphp has the greater advantages of perfect development documents and rich plug-ins. This article will give you a detailed introduction about the deployment of thinkphp5 project in the cloud virtual host, and share it for your reference and study. I won't say much below, let's take a look at the detailed introduction.

Refer to the ThinkPHP 5.0 Full Development Manual- > Deploy- > Virtual host environment, after understanding the situation, began to modify.

Environment

thinkphp 5.0.11 Alibaba Cloud Virtual System CentOS

Step 1

Modify the location of the mobile entry file and move it to the root directory (that is, htdocs).

Modify the contents of index. php entry file


//  Define the application directory 
define('APP_PATH', __DIR__ . '/application/');
//  Load the framework boot file 
require __DIR__ . '/thinkphp/start.php';

This can be accessed, but click on the inside page to see that the path rewriting will not take effect. Because the. htaccess file is missing.

Step 2

Move the. htaccess file to the root directory as well.

At this point, you may have solved the problem. If it is not solved, continue to look.

Step 3

Security!

Because you moved the entry file to the following directory, all the directories under the framework have been exposed.

If the apache server does not process. Then you can access important files directly.

Regardless of whether he has done processing or not, directly follow each directory under the directory to. htaccess. Then write deny from all.


deny from all

All right.

Step 4

Solution: css, js, pictures and other static resources 404.

The file was not found because it was moved out of the public directory.

All directory paths need to be replaced.

If you are writing with __PUBLIC__, __STATIC__, __CSS__, __JS__, etc

Then congratulations, just change the configuration to view_replace_str


'view_replace_str'  => [
 '__PUBLIC__' => '/public',
 '__STATIC__' => '/public/static',
 '__CSS__' => '/public/static',
 '__JS__'  => '/public/static',
],

If not, replace the directory with all the view files and all the places where the path is used


/public/static

Particularly easy to overlook is the background path inside css, and custom upload path, please note.

Step 5

File paths under Linux are case sensitive.

For example, yours:

The controller method name is: public function userCenter()

The corresponding view file is: userCenter. html,

Then when you visit, you find that the view file can't be found, and you report an error.

Solution:

Method 1, change the template file name to lowercase;

Method 2, $this- > fetch ('Template Filename');

Method 3, $this- > fetch (__FUNCTION__), but requires the view file name to be the same as the method name 1.

Test all the functions of the website, ok is no problem, and the deployment and configuration are completed.

Summarize


Related articles: