


Tutorial on deploying ThinkPHP project on Nginx, nginxthinkphp_PHP tutorial
Tutorial on deploying ThinkPHP project on Nginx, nginxthinkphp
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
var url="system.php?m=Login&a=doLog";
$.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){
If(data=="codeerr"){
alert("Verification code is wrong!");
}else if(data=="authempty"){
alert("Please enter username or password!")
}else if(data=="autherr"){
alert("Username or password is wrong!");
}else if(data=="success"){
alert("Login successful!");
location.href="system.php?m=Index&a=index"; //Visit the homepage
}
Of course, this is the access method in normal mode. If it is pathinfo, you only need to modify the red part as follows
var url="doLog";
$.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){
If(data=="codeerr"){
alert("Verification code is wrong!");
}else if(data=="authempty"){
alert("Please enter username or password!")
}else if(data=="autherr"){
alert("Wrong username or password!");
}else if(data=="success"){
alert("Login successful!");
location.href="../Index/index"; //How to jump to the homepage and access other modules
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.
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. 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
$path="./Public/uploads/";
$path=str_replace("\","/",realpath($path)."/"); //Get the absolute path and convert the separator
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
location ~ .php/?.* {
root fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include #Define variable $path_info, used to store pathinfo information
set $path_info "";
#Define variable $real_script_name, used to store the real address
set $real_script_name $fastcgi_script_name;
#If the address matches the regular expression within quotes
If ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
#Assign the file address to the variable $real_script_name
set $real_script_name $1;
#Assign the parameters after the file address to the variable $path_info
set $path_info $2;
}
#Configure some parameters of fastcgi
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
