In the daily operation and maintenance of AnQi CMS, we often deal with various configuration files. Among them,config.jsonThe file is undoubtedly one of the 'lifelines' of AnQiCMS operation, carrying the core parameters for system startup. Today, as an old soldier who has been deeply involved in website operation for many years, I will delve into it with everyone.config.jsonThe configuration of ports in the file, and its inseparable association with the start and stop of the AnQiCMS service.

The 'lifeblood' of AnQiCMS running:config.jsonport configuration in Chinese

In the world of AnQiCMS,config.jsonThe file is the starting point of the 'ID card' and 'code of conduct' for each site instance. It defines critical information such as database connections, cache settings, and the most fundamental and directly impacting the service startup is the embedded port (port)configuration.

Generally, in the root directory where you install AnQiCMS, you will find a file likeconfig.json. Open it, and you will find a field namedportwith a default value often being8001. This number is not arbitrarily set; it specifies the specific 'address number' that the AnQiCMS service listens for external connections on your server.Imagine that your server is a building, AnQiCMS is a shop inside the building, and this port number is the only entrance that this shop opens to the public.All requests that wish to access this AnQiCMS site must be received and processed by the AnQiCMS service through this specified port.

Port association with AnQiCMS service "Breath": start and stop

This seemingly simple port number is actually directly and profoundly related to the 'breath' (i.e., start and stop) of AnQiCMS services.

When the service starts: the "listening" port

When you start the AnQiCMS service, whether by executingstart.shScript, or start the "Go project" in visualization tools like Baota panel, the first step of the AnQiCMS program is to try to occupy and proceedconfig.jsonThe port configured. If all goes well, AnQiCMS will successfully bind to this port and start listening for requests from clients (such as browsers).At this moment, this port is like the only 'window' opened by AnQiCMS to the outside world, ready to receive and process all HTTP traffic.

However, if this port has been occupied by another program on the server (possibly another AnQiCMS instance, or another web service), the AnQiCMS service will not be able to start successfully.The system will throw an error message indicating that the port is already occupied because the same 'door number' cannot be used by two stores at the same time.This requires us to promptly identify and resolve port conflicts to ensure that each AnQiCMS instance has its exclusive 'address'.

Port release when the service stops

When you decide to stop the AnQiCMS service, whether by executingstop.shscript, or by ending it in the Windows Task Manageranqicms.exeProcess, the system will try to close the AnQiCMS program gracefully.In normal shutdown, AnQiCMS will "release" the port it previously occupied.This port is now free and can be used by other programs, or it can be reused by itself when AnQiCMS starts up next time.

A clean shutdown process is crucial. If the AnQiCMS service terminates abnormally for some reason (such as a sudden power outage or being forcibly killed with 'kill -9'), it may not have the opportunity to release the port normally.Although programs developed in Go language usually handle such situations well, theoretically, a port accidentally occupied may be in the "TIME_WAIT" state for a short period of time, causing AnQiCMS or other services to be unable to use the port immediately within a short period of time, thereby affecting the rapid recovery of the service.Therefore, following the startup/shutdown script provided by the official, performing an elegant shutdown is a practice to ensure the smooth release of ports and stable operation of the system.

The actual application of port configuration: the art of multiple sites and reverse proxy

After understanding the relationship between ports and service start/stop, we can better utilize this mechanism for more advanced deployment and operation strategies.

Deployment of multiple sites on a single server: ports are crucial

One of AnQiCMS' highlights is the support for multi-site management.Imagine you need to manage multiple content sites on the same server, each running independently.At this moment, simply through one8001Ports are not enough. In order for each AnQiCMS instance to work independently, you need to prepare an independent AnQiCMS program file (or a logically independent running environment) for each instance on the server, and modify the root directory of each instance'sconfig.jsonFiles, set theirportparameters to different port numbers, for example8001/8002/8003And so, each AnQiCMS site can run on its independent 'door number' without interference.

Reverse proxy: the 'behind-the-scenes hero' of ports.

However, let the user directly pass throughhttp://您的域名:8001This way of accessing the website is not common in actual production environments and is not professional enough.At this point, reverse proxy (such as Nginx, Apache) has become an indispensable 'behind-the-scenes hero'.

The reverse proxy server listens to standard HTTP (port 80) or HTTPS (port 443), and when users access, the request first reaches the reverse proxy. The reverse proxy then forwards the request to the backend AnQiCMS service that listens on a non-standard port (such as127.0.0.1:8001)

This deployment method has many advantages:

  1. Standardized access: Users do not need to remember non-standard port numbers.
  2. Enhanced securityHide the real port of AnQiCMS service to reduce the direct attack surface.
  3. SSL/TLS encryption: Reverse proxy can handle SSL certificates uniformly, implement HTTPS access, and AnQiCMS itself does not need to be directly configured.
  4. Load balancingIf multiple AnQiCMS instances are deployed, the reverse proxy can distribute traffic and achieve load balancing.
  5. Site reuseOn a single 80/443 port, Nginx can distribute requests to different AnQiCMS instances (i.e., different internal ports) based on domain names.

Therefore, in a multi-site deployment, you will see that each AnQiCMS instance is configured with a unique internal port, and all external traffic is routed intelligently through a reverse proxy to the correct internal port, finally presented to the user.

Summary

config.jsonThe port configuration is the core element for starting, running, and stopping AnQiCMS.It not only determines how AnQiCMS service listens and responds to network requests, but also plays a crucial role in multi-site deployment and reverse proxy architecture.A deep understanding of this configuration and its relationship with the service lifecycle can help us operate and manage the AnQiCMS website more efficiently and securely, providing users with a smooth and stable access experience.


Frequently Asked Questions (FAQ)

  1. Q: How to deal with the server prompt 'Port is already in use'?A: When you try to start AnQiCMS and encounter the error 'Port is already in use', it usually means that youconfig.jsonThe port configured is already occupied by another process on the server (it may be another AnQiCMS instance, or other web services, databases, etc.). Under Linux, you can uselsof -i:{端口号}Use the command to check which process is occupying the port (for examplelsof -i:8001). After finding the PID (process ID) of the occupying process, you can usekill -9 {PID}Command to force terminate the process. After termination, you can try to restart the AnQiCMS service. In Windows, you can find it in the Task Manager.anqicms.exeProcess and end it, or check if other applications are using the port. Always ensure that the port required by AnQiCMS is exclusive each time it starts.

  2. Q: How to change the default port of AnQiCMS after installation?A: Modifying the AnQiCMS runtime port is very simple. You just need to find the AnQiCMS installation directory andconfig.jsonfile, open it with a text editor. Find the following in it."port": 8001(or the port number you are currently configured) this line will8001modify it to the other port number you want to use, such as8002). After modifying and saving the file, please make sure to stop the AnQiCMS service that is currently running, and then restart it so that the new port configuration can take effect.If your website uses Nginx or Apache and other reverse proxies, don't forget to synchronize the update of the reverse proxy configuration, and itsproxy_passThe port number in the target address must also be changed to the new value, otherwise the user will not be able to access the website normally.

  3. Q: Since AnQiCMS has its own port, why do we still need to set up a reverse proxy?A: Using a reverse proxy (such as Nginx, Apache) to forward access to AnQiCMS is recommended in a production environment, as it brings many benefits:

    • Standardized port accessUsers are accustomed to accessing through standard 80 (HTTP) or 443 (HTTPS)