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.When deploying and running AnQiCMS in a Linux environment, 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 may fail, and it is necessary to find and terminate the process occupying a specific port.It is particularly important to understand how to query and release ports occupied by AnQiCMS or other programs on a Linux system at this time.This article will introduce this process in detail and help you solve relevant issues efficiently.


Check port occupancy: Identify the key issue

When your AnQiCMS or other services fail to start normally and prompt port occupation, the first task is to find out which program is occupying the target port.The 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 occupation status of specific ports.

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 us simply interpret the various parts of this command:

  • -t: Display the TCP protocol connection.
  • -u: Display the UDP protocol connection.
  • -l: Only displays sockets that are listening (Listen), which means they are waiting for incoming connections.
  • -n: Display the address and port number in numeric form instead of trying to resolve the service name through DNS, which will make the output clearer and faster.
  • -p:Display the process ID (PID) and program name for this socket, which is the critical information we need.
  • | grep :8001:Pipe through.netstatPass the output to the.grepcommand, filter out including: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 output,12345This is the process ID (PID) occupying the port 8001, andanqicmsthen it corresponds to the program name. If the program name is other, for examplenginx/javaIf so, it means that another service is using the port.

2. Uselsofcommand

lsofThe (list open files) command is a very flexible tool, which can list all files opened by processes, including network files (i.e., ports).Although it is usually considered a file viewing tool, it also performs well in network troubleshooting.

To query the occupation status of a specific port, you can use it like this.lsof:

lsof -i :8001
  • -iMeans listing all network files.
  • :8001Specifies the port number to be queried.

After executing this command, you may get a 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 thannetstatSimpler, 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 through the above query command, you can then take action to release the port.

Terminate the process:killcommand

In Linux,killcommands are used to send signals to processes, the most common signal is to terminate the process.

The usage is as follows:

kill [PID]

to[PID]replace it with the one you pass throughnetstatorlsofThe specific process ID found by the command. For example, if the PID is 12345, executekill 12345.

By default,killThe command will sendTERMTermination signal, this is a friendly request to close the process normally.Most programs will catch this signal, perform necessary cleanup work (such as saving data, closing file connections), and then exit.

If the process receivesTERMAfter the signal has no response, or you need to terminate it immediately, you can useKILL(Force terminate) Signal:

kill -9 [PID]
  • -9: means to sendKILLSignal.

Important reminder:Usekill -9Be particularly cautious! 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, before performing this operation, please make sure that you want to terminate the process, especially when it is not AnQiCMS.If terminating other critical services (such as databases, web servers) may cause serious consequences.

Port management for AnQiCMS

If it is found that AnQiCMS itself is occupying the port (possibly 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 is usually accessed throughstart.shThe script starts, you can use the accompanyingstop.shscript to gracefully close it (if it exists). For example:

/path/to/your/anqicms/stop.sh

Ifstop.shIf it is not available or cannot be stopped, then throughkill [PID]orkill -9 [PID]To manually terminate the AnqiCMS process is also an option.

Change the listening port of AnQiCMS

If you find that a specific port is always occupied by other critical services, or if you want AnQiCMS to use a different port, you can modify AnQiCMS's configuration file. According to the documentation, AnQiCMS's listening port is usually configured under its root directory.config.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 a new port.


Further Thoughts on AnQiCMS Port Management and Start-up Strategy

AnQiCMS is a project based on the Go language, its design philosophy includes high performance and high concurrency.It usually runs as an independent process and listens on a specific port to provide services.Understand the port management and startup strategy in multi-site deployment or more complex scenarios, it can make you more agile in the operation process.

Multi-site and Reverse Proxy:It is worth mentioning that AnQiCMS supports multi-site management, but it does not need to duplicate a set of code or occupy different physical ports for each site.In most cases, through Nginx or Apache and other reverse proxy services, you can point multiple domains to different sites of the same AnQiCMS instance, or forward requests to multiple AnQiCMS instances running on different ports.For example, your AnQi