When developing ThinkPHP applications, sometimes we encounter the problem that the port number cannot be accessed. This is probably due to the port number being occupied. This article will introduce how to view and release the occupied port number, and how to correctly configure the port number in the ThinkPHP application.
1. View the processes occupying port numbers
In Windows systems, you can use the command line tool netstat to view the occupied port numbers and their corresponding processes.
Take viewing the 8888 port number as an example. Open the command line tool and enter:
netstat -ano|findstr "8888"
Among them, -a means to display all connections and listening ports, and -n means to use numbers to represent the address and port number. , -o means to display the process ID occupying the connection, |findstr "8888" means to find the line containing "8888".
The process with process ID 10332 occupies port number 8888. Next we need to find the process occupying the port number through the process ID.
Enter in the command line tool:
tasklist|findstr "10332"
Among them, the tasklist command is used to view the process list in the system, and |findstr "10332" is used to find the process containing "10332".
It can be seen from this information that the process occupying port number 8888 is php.exe.
2. Release the process occupying the port number
We have found the process occupying the port number 8888, and then we need to release the process.
A simple way is to end the process directly. Just enter the following command in the command line tool:
taskkill /pid 10332 /f
Among them, the /pid parameter is used to specify the process ID of the process to be terminated, and the /f parameter indicates the forced termination of the process.
After executing this command, you can use the netstat command again to check whether the port number has been released.
3. Correctly configure the port number of the ThinkPHP application
When developing a ThinkPHP application, the PHP built-in server is usually used to run the application. At this time, we need to configure the port number in the application's entry file index.php.
Take the configuration port number as 8888 as an example, add the following code at the beginning of the index.php file:
// 指定端口号 $port = 8888; // 启动PHP内置服务器 exec("start php -S 0.0.0.0:$port -t public");
The above code specifies the port number as 8888, and uses the PHP exec function to start the PHP built-in server.
Note that in actual development, since port numbers are often occupied, it is recommended to use random port numbers. The code that can use random port numbers is as follows:
// 获取随机端口号 $port = rand(10000, 20000); // 启动PHP内置服务器 exec("start php -S 0.0.0.0:$port -t public");
IV. Summary
When we encounter the problem that the ThinkPHP port number cannot be accessed, we can solve it through the following steps:
I hope this article can help everyone solve the problem.
The above is the detailed content of What should I do if the thinkphp port number cannot be accessed?. For more information, please follow other related articles on the PHP Chinese website!