AnQiCMS, as an enterprise-level content management system developed based on the Go language, has won the favor of many users with its lightweight and efficient features.In the Linux environment, when deploying and running AnQiCMS, we usually specify a listening port for it, such as the default 8001.However, in daily operation and maintenance, we may encounter such situations: AnQiCMS cannot be started, indicating that the port is already occupied; or the system resources are abnormal, and it is necessary to find and terminate the process occupying a specific port.This is especially important to understand how to query and release ports occupied by AnQiCMS or other programs under the Linux system.This article will introduce this process in detail and help you efficiently solve related issues.
Query port occupancy status:定位问题的关键
When your AnQiCMS or other services fail to start normally and prompt port occupation, the primary task is to find out which program is "hogging" the target port.Linux system provides some powerful command line tools to complete this task.
1. Usenetstatcommand
netstat(network statistics)is a powerful tool used to display network connections, routing tables, interface statistics, and other information. CombinedgrepCommand, we can accurately query the occupancy status of a specific port.
For example, if you suspect that the default 8001 port is occupied, you can enter the following command in the terminal:
netstat -tulnp | grep :8001
Let's take a simple look at the various parts of this command:
-t: Displays the TCP protocol connection.-u: Displays the UDP protocol connection.-l:Only displays sockets that are listening (Listen), which means they are waiting for incoming connections.-n:Displays addresses and port numbers in numeric form instead of attempting to resolve service names through DNS, which makes the output clearer and faster.-p:Display the process ID (PID) and program name of the socket being used, which is the key information we need.| grep :8001:Pass through the pipe tonetstatthe output is passed togrepcommand, filter out the ones containing:8001The line indicating the port number.
After executing the above command, if port 8001 is occupied, you may see output similar to the following:
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 12345/anqicms
In this line of output,12345It is the process ID (PID) occupying port 8001anqicmsThen it is the corresponding program name. If the program name is other, for examplenginx/javaIf it is not, then it means other services are using the port.
2. Uselsofcommand
lsofThe `auto` command is a very flexible tool that can list all files opened by processes, including network files (i.e., ports).Although it is generally considered a file viewing tool, it also performs excellently in network troubleshooting.
To query the occupation status of a specific port, you can use it like thislsof:
lsof -i :8001
-i: means list all network files.:8001: specifies the port number to be queried.
After executing this command, you may get similar output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
anqicms 12345 root 3u IPv4 67890 0t0 TCP *:8001 (LISTEN)
Similarly,PIDThe column will display the process ID (for example)12345)COMMANDThe column will display the program name (for example)anqicms).lsofThe output is usually more thannetstatMore concise, especially when you only need to find the process occupying the port.
Release the occupied port: the core of solving the problem
Once you have obtained the process ID (PID) of the process occupying the target port using the above query command, you can then take action to release the port.
Terminate process:killcommand
In Linux,kill命令用于向进程发送信号,最常见的信号是终止进程。
Usage is as follows:
kill [PID]
to[PID]替换为您通过netstatorlsof命令查到的具体进程ID。例如,如果PID是12345,就执行kill 12345.
By default,killThe command will be sentTERM(Termination)signal, this is a friendly request to close the process normally.Most programs will catch this signal and perform necessary cleanup work (such as saving data, closing file connections), then exit.
If the process does not respond after receivingTERMor you need to terminate it immediately, you can useKILL(Force quit) signal:
kill -9 [PID]
-9: means to sendKILL.
Important reminder:Usekill -9Be especially careful!It will force the process to terminate without giving the program any chance to save data or perform cleanup, which may lead to data loss or system instability.Therefore, please make sure you are terminating the correct process before performing this operation, especially when it is not AnQiCMS.If the service is a critical service other than auto (such as database, web server), force termination may have serious consequences.
English translation for auto is English
If it is found that AnQiCMS itself is using the port (it may be an old or abnormal instance), and you wish to restart it with a new configuration (such as a new port), terminating its process is safe. AnQiCMS usually runs throughstart.shScript start, you can use the accompanying.stop.shScript to gracefully close it (if it exists). For example:
/path/to/your/anqicms/stop.sh
Ifstop.shNot available or cannot be stopped, then throughkill [PID]orkill -9 [PID]It is also an option to manually terminate the AnqiCMS process.
Change the listening port of AnQiCMS
If you find that a specific port is always occupied by other key services, or you want AnQiCMS to use another port, you can modify AnQiCMS's configuration file. According to the document information, AnQiCMS's listening port is usually configured in the root directory ofconfig.jsonthe file.
You can use a text editor (such asvi/nano) to open the file:
nano /path/to/your/anqicms/config.json
Find a field similar to"port": 8001and change its value to a new, unused port number, such as8002:
{
"name": "默认模板",
"package": "default",
"version": "1.0",
"description": "系统默认模板",
"author": "kandaoni.com",
"homepage": "https://www.kandaoni.com",
"created": "2022-05-10 22:29:00",
"template_type": 0,
"status": 0,
"port": 8002 // 修改为新的端口
}
After saving the changes, you need to restart the AnQiCMS service, usually by executingstart.sha script:
/path/to/your/anqicms/start.sh
After restarting, AnQiCMS will listen on the new port.
AnQiCMS Port Management and Startup Strategy Further Thoughts
AnQiCMS as a project based on the Go language, its design philosophy includes high performance and high concurrency.It usually runs as a separate process and listens on a specific port to provide services.In multi-site deployment or more complex scenarios, understanding its port management and startup strategy can make you more agile in the operation process.
Multiple Sites and Reverse Proxy: