When using AnQiCMS to manage your website, you may occasionally encounter a common but annoying error: 'Port is occupied'.As a content management system developed based on the Go language, known for its efficiency and stability, AnQiCMS usually runs smoothly.However, when it throws this error during startup or restart, many operators may feel at a loss.
Don't worry, this problem is not unique to AnQiCMS, but a challenge that all network service-based applications may encounter.As an experienced website operations expert, I will guide you to deeply understand this error and provide a series of practical diagnostic and solution methods to ensure that your AnQiCMS website can continue to provide stable and reliable services to the outside world.
Understanding 'port already in use': the essence of the problem
Imagine your server as a bustling port, and each application that needs to provide external services must reserve a dedicated 'berth' on the port - this is the network port.Each port can only be occupied by one application at a time.When AnQiCMS attempts to start and listen on a predefined port, if this 'berth' has already been 'berthed' by another program, it cannot bind successfully and reports the error that the 'port is already in use'.
AnQiCMS default usage8001Port, this is the default listening port for its internal service. When you see this error, it usually means one of the following situations:
- AnQiCMS process did not stop completely:The previous operation to close or restart AnQiCMS may not have terminated its old processes completely, causing the old processes to still occupy ports in the background.
- Other applications are抢占:There may be other services running on your server, unintentionally occupying the AnQiCMS preset
8001port (or any custom port you set). - Conflict in multi-instance deployment:When you try to deploy multiple AnQiCMS instances on the same server, a conflict will occur if they are all configured to use the same port.
Understood the basic reasons, we can then proceed to specific diagnosis and resolution.
Diagnose the problem: find out the 'culprit' occupying the port.
The first step in solving the problem is to find out which program is using the port. The diagnostic method varies depending on your server operating system.
Under Linux:
On a Linux server, you can uselsof(list open files) command to view the occupancy of a specified port. For example, if AnQiCMS tries to use8001Port and report an error, you can run it in the terminal:
lsof -i:8001
After executing this command, the system will list the occupation8001Port process information, including process ID (PID), user, command, etc., the most critical information isPIDFor example, the output may be like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
anqicms 12345 www 3u IPv4 0x12345 0t0 TCP *:8001 (LISTEN)
Here12345is occupied8001Port process ID. IfCOMMANDdisplayed asanqicmsit means that the old process of AnQiCMS is at work.
Under Windows environment:
For Windows users, you need to use the 'Task Manager' to find and terminate conflicting processes.
- Press
Ctrl + Shift + Escor right-click on the taskbar to open 'Task Manager'. - Switch to the "Details" or "Processes" tab (which may vary depending on the Windows version).
- Look for the name
anqicms.exeprocess. - If found, select it and click the "Finish Task" button at the bottom right.
If you are inlsofor you do not see it directly in the task manageranqicmsProcess, but the port is indeed occupied, so attention should be paid.COMMANDOr is there any suspicious service in the process name.
Solution: let AnQiCMS restart.
Once you locate the "culprit" occupying the port, you can take appropriate measures.
1. Terminate the conflicting process.
Whether on Linux or Windows, the most direct method is to terminate the process occupying the port.
Linux users:Using the one obtained in the diagnostic steps
PIDthrough.killCommand forces the process to terminate. For example:kill -9 12345This command will forcibly kill the process with ID:
12345After that, you can try to start AnQiCMS again.Windows users:Find in Task Manager
anqicms.exeProcess and click 'End Task' to terminate the process.If the port is occupied by other non-AnQiCMS programs, and you confirm that it can be safely terminated, you can also handle it in the same way.
2. Adjust the listening port of AnQiCMS
If the port is occupied by a critical service you do not want to stop, or if you need to run multiple AnQiCMS instances on a single server, it is more appropriate to adjust the listening port of AnQiCMS.
AnQiCMS allows you to modify itsconfig.jsonFiles are used to define different ports. According to the document, when you install multiple AnQiCMS on the same server, you need to assign a different port to each project and edit throughconfig.jsonDefine.
Find the installation directory of your AnQiCMS under.config.jsonFile (usually in the root directory of the project), open it with a text editor, find and modify.portThe value of the field. For example, the default8001is modified to8002:
{
"name": "Your AnQiCMS Site",
"port": "8002", // 将这里修改为其他未被占用的端口
// ... 其他配置项
}
Important reminder:After changing the port, if your AnQiCMS is accessed through Nginx, Apache, and other reverse proxy services, or deployed through Docker containers, you also need to synchronize the update of the reverse proxy configuration to point to the new port.
- For Docker deployment (such as 1Panel or aaPanel):When installing AnQiCMS in the app store of 1Panel or aaPanel, you will usually be required to set the "server port".The document clearly states: “If you install multiple AnQiCMS containers and set different ports, then fill in different ports for the server, such as 8002, 8003, etc., but still fill in 8001 for the container port.”}]This means you need to ensure that each AnQiCMS container is mapped to a unique port on the host, and the reverse proxy configuration points to this unique host port.
config.jsonThen the mapping of the Docker container also needs to be adjusted accordingly (or keep the internal port 8001 of the container unchanged, and only adjust the host mapping port).
3. Port planning for multi-site deployment
AnQiCMS supports multi-site management, but this does not mean that you can arbitrarily copy multiple AnQiCMS program files and run them simultaneously.The document clearly states: "AnQiCMS does not need to duplicate the AnQiCMS code when installing multiple sites on a single server.OneConfigure multi-site in the AnQiCMS instance backend.
If your requirement isMultiple independent AnQiCMS programs(Instead of an AnQiCMS instance managing multiple sites, each independent program instance needs:)
- Independent port:)As mentioned above, modify)
config.jsonTo a unique port. - Independent executable file name:
start.shIn the script,BINNAMEandBINPATHIt needs to point to different executable files and paths. - Independent start/stop script:Create and configure the corresponding for each instance.
start.shandstop.shScripts, ensure they manage their own processes and ports. - Reverse proxy configuration:Ensure that the Nginx/Apache reverse proxy points to each AnQiCMS instance on its own independent port.
Preventive measures: Avoid recurrence in the future.
It is crucial to develop good operational habits to avoid the repeated occurrence of the "port already in use" error:
- Use the correct shutdown script:AnQiCMS provides
stop.shscript (mentioned in the Docker/Baota installation tutorial), this script is designed to gracefully terminate the AnQiCMS process.Always prefer to stop the service using it rather than directly killing the process, as this can prevent any lingering processes. - Plan the port allocation:When deploying a new AnQiCMS instance or service, plan the port usage in advance to avoid port conflicts.
- Regularly check the service status:Even if there are no errors, use it regularly
lsofOr check the critical port with the task manager