Home >Backend Development >PHP Tutorial >Tutorial on deploying ThinkPHP project on Nginx, nginxthinkphp_PHP tutorial
Some time ago, I developed a small website using Apache and TP. It was also my first practical experience using TP. I am used to using the pathinfo mode, and everything runs fine locally. However, when I deploy it to the server, because the other party uses Nginx, the pathinfo mode of ThinkPHP is not supported by default, so the path error occurs and the page jumps in a loop. So, Download nginx and test it locally.
In fact, there are two solutions to solve the problem of nginx not supporting pathinfo. One is not to use pathinfo mode, and the other is to modify the configuration file of nginx so that it supports pathinfo. In order to simplify the problem, I chose the first method, because as for the second method, I checked a lot of information and found that everyone's methods are different, and some are very different, which can easily lead to misleading, so I chose to simplify Set off and choose normal mode, although there are certain risks. After modifying the front-end code corresponding to index.php, we found that the front-end was basically normal, but the redirection problem still occurred in the back-end. After struggling for a long time, I thought of taking a look at the log file. It turned out to be a problem with the editor. It seemed that the log file was really important, and I had never paid attention to it before. The output appears on the first line of the config.php file,
Under sublime, a BOM header is usually added to the UTF-8 file. This BOM header is usually invisible under window and can be viewed through other editors. It can also be seen directly under Linux and is usually displayed. It is a garbled character, just delete this character, or simply press Enter on the first line and delete it. At this point, the backend is basically accessible.
There are a few small issues that need to be explained.
1. When logging in, I sent an Ajax request through an external js file for verification. I encountered some problems in the communication between js and the ThinkPHP module function. I never knew how to write the correct path, and I did not find any relevant information. I can only try various data. Fortunately, I found a solution. By directly bringing the entry file name, the code is as follows
Of course, this is the access method in normal mode. If it is pathinfo, you only need to modify the red part as follows
2. When downloading files, there are always a lot of html things inexplicably. The reason is that the buffer is not cleared. It can be modified by the following code, but in this way, the files actually downloaded are still in html format. I just changed the suffix to xls, so when you open it with excel, you will be prompted with a format problem, just ignore it. At the same time, you need to pay attention to using the icov() function to convert the encoding, because the default encoding format of xls is not utf-8.
3. You will encounter path problems when deleting files, because most of the projects use relative paths, that is, relative to the entry files, but deleting files requires using absolute paths. I have not found a suitable solution. Method, I had to use a more conservative approach, the code is as follows
4. In terms of configuring nginx and php, I used fastCGI, saved the following code as a cmd file, and just click to run it
"F:phpphp-cgi.exe" -b 127.0.0.1:9000 -c "F:phpphp.ini" //Following is the path of the php file
Then add a few sentences to the nginx configuration file