Deploy ThinkPHP on Nginx

Source: Internet
Author: User
This article mainly introduces how to deploy ThinkPHP on Nginx. it is very detailed. if you need it, you can refer to a small website developed with Apache and TP some time ago, this is also my first practice of using TP. I am used to the pathinfo mode, and the local operation is normal. However, when deployed on the server, because the other side uses Nginx, the pathinfo mode of ThinkPHP is not supported by default, resulting in path errors, when the page jumps cyclically, nginx is downloaded and tested locally.

In fact, to solve the problem that nginx does not support pathinfo, there are two solutions: one is not to use the pathinfo mode, and the other is to modify the nginx configuration file so that it supports pathinfo. In order to simplify the problem, I chose the first method, because I checked a lot of information and found that the methods were different, and some were quite different, it is easy to mislead, so I chose to start from simple mode, although there is a certain risk. After the front-end code corresponding to index. php is modified, it is found that the front-end is basically normal, but there is still a redirection problem in the background. After a long time, I thought about viewing the log file. it turned out to be an editor problem. it seems that the log file is really important and I have never paid much attention to it before. Output is displayed in the first line of the config. php file,

Under sublime, the BOM header is usually added to the UTF-8 file, this BOM header is usually invisible under the window, you can view it through other editors, Linux can also be seen directly, it is usually displayed as a garbled character. just delete it, or simply press enter in the first line and then delete it. At this point, the background is basically accessible.

There are several minor issues that need to be explained.

1. when I log on, I sent an Ajax request through an external js file for verification. I encountered some problems in the function communication between js and the ThinkPHP module, and I never knew how to write the correct path, I have not found any relevant information and can only try it. Fortunately, I found a solution by directly attaching the entry file name. The code is as follows:

The code is as follows:


Var url = "system. php? M = Login & a = doLog ";
$. Post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data ){
If (data = "codeerr "){
Alert ("verification code error! ");
} Else if (data = "authempty "){
Alert ("Enter the user name or password! ")
} Else if (data = "autherr "){
Alert ("incorrect user name or password! ");
} Else if (data = "success "){
Alert ("logon successful! ");
Location. href = "system. php? M = Index & a = index "; // access the home page
}

Of course, this is the access method in normal mode. if it is pathinfo, you only need to modify the red part as follows.

The code is as follows:


Var url = "doLog ";
$. Post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data ){
If (data = "codeerr "){
Alert ("verification code error! ");
} Else if (data = "authempty "){
Alert ("Enter the user name or password! ")
} Else if (data = "autherr "){
Alert ("incorrect user name or password! ");
} Else if (data = "success "){
Alert ("logon successful! ");
Location. href = "../Index/index"; // method for redirecting to the home page to access other modules

2. when downloading files, there is always a lot of html content, because the buffer zone is not cleared and can be modified using the following code. However, this method is actually to download html files, the suffix is changed to xls. Therefore, when you open the file in excel, a format error is Prompted. ignore the format. At the same time, you must use the icov () function to convert the encoding because the default xls encoding format is not UTF-8.

The code is as follows:


Ob_start ();
Ob_end_clean ();
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Content-type: application/vnd. ms-excel; charset = gb2312 ");
Header ("Content-Disposition: attachment?filename=#filename=.xls ");

3. when you delete a file, you may encounter a path problem because the project uses a lot of relative paths, that is, relative to the entry file, but the absolute path is required to delete the file, I didn't find a proper solution, so I had to use a conservative method. the code is as follows:

The code is as follows:


$ Path = "./Public/uploads /";
$ Path = str_replace ("\", "/", realpath ($ path). "/"); // Obtain the absolute path and convert the separator

4. for nginx and php configuration, I used fastCGI to save the following code as a cmd file and click Run.

"F: \ php \ php-cgi.exe"-B 127.0.0.1: 9000-c "F: \ php. ini" // The path of the php file is
Then add a few words to the nginx configuration file.

The code is as follows:


Location ~ \. Php /?. *{
Root myapplications;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
# Define the variable $ path_info to store pathinfo information.
Set $ path_info "";
# Define the variable $ real_script_name to store the real address.
Set $ real_script_name $ fastcgi_script_name;
# If the address matches the regular expression in quotation marks
If ($ fastcgi_script_name ~ "^ (. +? \. Php) (/. +) $ "){
# Assign the file address to the variable $ real_script_name
Set $ real_script_name $1;
# Assign the parameter after the file address to the variable $ path_info
Set $ path_info $2;
}
# Configure fastcgi parameters
Fastcgi_param SCRIPT_FILENAME $ document_root $ real_script_name;
Fastcgi_param SCRIPT_NAME $ real_script_name;
Fastcgi_param PATH_INFO $ path_info;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.