As an experienced security CMS website operation person, I know that technical details often determine whether the operation runs smoothly or not.Among them, "Port is occupied" is a very common initial problem when deploying applications like AnQiCMS that run on specific ports.But please be assured that this is not a difficult problem to solve. Just understand the principle and follow the steps, and you can easily solve it.
Understand port occupancy error
When you try to start the AnQiCMS service, if the system prompts the error message 'Port is already in use', this means that the port you specified or the port used by AnQiCMS by default (usually 8001) has been occupied by another program or service on the server.AnQiCMS as an enterprise-level content management system developed in Go language, needs to listen on a specific network port on the server so that users can access it through a web browser.Each network port can only be used exclusively by one process at a time.Therefore, when AnQiCMS tries to start and bind to a port that is already occupied, a conflict occurs, leading to the failure of the service to start.
The reasons for this situation are various.可能您之前启动过AnQiCMS的一个实例但未正常关闭,或者服务器上运行着其他Web服务、数据库服务,甚至是开发工具,它们可能恰好使用了相同的端口。
Identify conflicting ports and occupied processes
The first step in solving the problem is to find out which process is occupying the port you need. Under Linux server environments, we can easily complete this task with command line tools.
You can uselsofUse the command to view the port usage of a specified port. For example, if AnQiCMS uses the default port 8001, you can enter the following command in the terminal:
lsof -i:8001
Execute this command, the system will list all processes that are listening or using the 8001 port, including key data such as Process ID (PID) and process name.Through this information, you can clearly see which program caused the port conflict.
Terminate the occupying process
After identifying the process that is using the port, if you confirm that the process is not a critical system service, or that it can be temporarily stopped, you can choose to terminate it to release the port.Please operate with caution to ensure that you do not mistakenly kill important processes.
You can usekill -9Command forces the termination of the process. Where,-9Option indicates forced termination, please replace the actual process ID (PID) obtained in the previous step in the command.{PID}:
kill -9 {PID}
For example,lsof -i:8001Command shows the PID of the process using the port as 7621, so the termination command will bekill -9 7621. After terminating the process, you can uselsof -i:8001to verify if the port has been released.
Change the running port of AnQiCMS
If you do not want to terminate the process occupying the port, or find that the port is always occupied by other services, the most thorough solution is to change the running port of AnQiCMS itself.AnQiCMS allows you to specify the running port by modifying its configuration file.
AnQiCMS running port configuration is located in the root directory of the project.config.jsonIn the file. You need to open this file with a text editor (such as)vi,nanoor through an SFTP tool) to find and modify itport
{
"name": "默认模板",
"package": "default",
"version": "1.0",
"description": "系统默认模板",
// ... 其他配置项
"port": 8002, // 将此处的端口号修改为新的未占用端口
// ... 其他配置项
}
Modify and saveconfig.jsonAfter the file, please make sure you have selected a port that is not likely to be occupied by other services in the current or future.
Update the reverse proxy configuration (if used)
Many AnQiCMS deployments choose to use Nginx or Apache as a reverse proxy to forward external 80 or 443 port traffic to the ports where AnQiCMS is actually running.If you have used a reverse proxy, then after changing the running port of AnQiCMS, you must also update your reverse proxy configuration accordingly.
For example, using Nginx, you need to edit the Nginx site configuration file (usually located/etc/nginx/conf.d/or/etc/nginx/sites-available/), findproxy_passInstructions, and update the port number to the new running port of AnQiCMS. For example:)
location @AnqiCMS {
proxy_pass http://127.0.0.1:8002; # 更新为AnQiCMS的新端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
After modifying the Nginx configuration, please be sure to reload or restart the Nginx service to make the changes take effect.For Apache users, similar modifications are also required to update the target URL of the reverse proxy.
Restart the AnQiCMS service
After completing the port modification or occupying the process, you need to restart the AnQiCMS service.If you are starting manually, please execute the corresponding start command.If deployed in a visual tool like Baota panel, usually just click the 'Start' or 'Restart' button.start.shA script that can be executed:
./start.sh
After restart, AnQiCMS should be able to bind smoothly to a new or released port and provide services normally.You can try to access your website in the browser to confirm that everything is normal.
Considerations when deploying to multiple sites
When deploying multiple AnQiCMS sites on the same server, the port occupation issue becomes more prominent.In this case, assigning a unique port for each AnQiCMS instance is **practical**.config.jsonFiles, and configure differentportThe value. Additionally, the reverse proxy configuration for each site should point to the exclusive port of the respective AnQiCMS instance to ensure that traffic is routed correctly.
Common Questions and Answers (FAQ)
1. What is a port? Why does AnQiCMS deployment require a port?
2. I modified the port in config., but the website still cannot be accessed, what could be the reason?
If you have modified the running port of AnQiCMS but the website still cannot be accessed, there may be several reasons:
- Reverse proxy not updatedIf you use Nginx or Apache and other reverse proxies, you also need to update the reverse proxy configuration accordingly
proxy_passor the port number of the target URL to matchconfig.jsonThe new port is consistent, and the reverse proxy service is restarted. - AnQiCMS service has not been restarted.: While modifying
config.jsonAfter that, you must restart the AnQiCMS service to make the new port configuration take effect. - Firewall blockingThe server's firewall may be blocking access to the new port. You need to check the firewall rules to ensure that the new port is open.
- The port is still occupiedEven if you change the port, the new port may still be occupied unexpectedly by other services. You can use
lsof -i:{新端口号}the command to check again.
3. How to deal with port occupation when deploying AnQiCMS in Docker environment?
- Change the host mapping port: Run Docker commands or
docker-composeIn the file, modify the host port mapping and select an unused host port. For example, map the host port8001:8001to8002:8001to the container port 8001. - Change the listening port of AnQiCMS inside the containerThis usually involves building a custom Docker image, or modifying the AnQiCMS inside the container by passing environment variables or mounting configuration files when starting the container.
config.jsonin the fileportConfiguration.Then, you can map any unused port on the host to the new listening port of AnQiCMS inside the container.No matter which way, if you use a reverse proxy, also remember to synchronize the update of the reverse proxy configuration.