As an experienced CMS website operation personnel of an enterprise security company, I am well aware of the importance of stable operation of the website, especially after the server is unexpectedly restarted, the system can automatically restore the service, which is crucial for ensuring user access experience and avoiding operation interruption.AnQiCMS (AnQiCMS) is an efficient and concise content management system developed in Go language, with flexible deployment methods, but how to ensure it starts up automatically on a Linux server is a concern for many operators.CrontabService, set AnQiCMS to start up automatically at boot.
Understand the necessity of starting up automatically at boot.
During the operation of the website, the server may be restarted due to maintenance, system updates, hardware failures, or unexpected events.If AnQiCMS is not configured to start up automatically, you will need to manually log in to the server and execute the start-up command every time the server is restarted, which will waste valuable time and may cause service interruptions.CrontabWe can ensure that AnQiCMS runs automatically after the server starts, greatly enhancing the stability and operation and maintenance efficiency of the website.
Overview of AnQiCMS's self-starting mechanism
AnQiCMS takes ease of startup and shutdown into consideration during design. Its installation package usually includes astart.shscript.This script is the core of the auto-startup, it is responsible for checking whether the AnQiCMS process is running, if not running, it will start the AnQiCMS service in the background.CrontabThe function is to execute this at regular intervals (usually set to every minute)start.shscript to achieve the purpose of 'monitoring' and 'auto-starting' AnQiCMS. This method is more efficient compared to the traditionalsystemdService configuration, more lightweight and flexible, especially suitable for scenarios with limited resources or those who wish to quickly deploy.
Set Crontab task
To set AnQiCMS to start up automatically at boot, we mainly use the services of the Linux system'sCrontabto complete.Crontabis a service used to set periodic task execution.
First, you need to log in to your Linux server and openCrontabthe configuration file for editing. This is usually done by executing the following command in the terminal:
crontab -e
After executing this command, the system will open a text editor (usually Vim or Nano), where you can add or modify scheduled tasks. Make sure you are familiar with the basic operations of the editor you are using, such as pressingiEnter edit mode, press after editing is completeEscExit edit mode and then enter:wqSave and exit.
InCrontabAt the end of the file, you need to add a new task to execute the AnQiCMS startup script regularly. Here is the recommended configuration example:
*/1 * * * * /www/wwwroot/anqicms.com/start.sh
The meaning of this configuration is:
*/1 * * * *Indicates that the task will be executed every minute./www/wwwroot/anqicms.com/start.shThis isstart.shThe complete path of the script. Please make sure to replace this path with the actual installation directory of your AnQiCMS.start.shScript path. For example, if your AnQiCMS is installed in/opt/anqicmsdirectory, then the path should be/opt/anqicms/start.sh.
save and exitCrontabafter the file,CrontabThe service will automatically load the new configuration, and your task will start taking effect.
Deep understandingstart.shscript
To better understand the self-startup process, it is necessary to know.start.shHow scripts work. Provided by AnQiCMS.start.shScripts usually include the following logic:
#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms.com # 这里的路径需要根据您的实际安装路径修改
# check the pid if exists
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME PID check: $exists" >> $BINPATH/check.log
echo "PID $BINNAME check: $exists"
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi
BINNAME=anqicms: Defines the executable file name of AnQiCMS.BINPATH=/www/wwwroot/anqicms.com: Defined the installation path of AnQiCMS.This is the place you need to check and modify during actual operation, ensuring it is consistent with your AnQiCMS deployment path.ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l: A series of pipeline commands to check if there is a namedanqicmsare currently running.ps -ef: List all running processes.grep '\<anqicms\>': Filter lines containing the string "anqicms".\<and\>Is a word boundary, make sure to match the executable file name accurately.grep -v grepExclude:grepIts own process to avoid misjudgment.wc -lCount the number of lines matched, that is, the number of running AnQiCMS processes.
if [ $exists -eq 0 ]: If the process count is 0 (i.e., AnQiCMS is not running), then execute the subsequent startup command.cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &: This is the core startup command for AnQiCMS.cd $BINPATHSwitch to the AnQiCMS installation directory to ensure the program runs in the correct environment.nohup $BINPATH/$BINNAME: Use.nohupCommand to start the AnQiCMS executable file.nohupEnsure that the program continues to run in the background even if the terminal is closed.>> $BINPATH/running.log 2>&1Redirect the standard output and standard error of the program torunning.loga file for subsequent viewing of runtime logs and troubleshooting.&Run the program in the background.
Execute manually for the first time.start.sh
After you have configured it.CrontabConfirm the task.start.shAfter confirming that the script path is correct, it is recommended that you execute it manually once.start.shScript to immediately start the AnQiCMS service and verify if the script works properly:
/www/wwwroot/anqicms.com/start.sh # 替换为您的实际路径
After executing, you can check if AnQiCMS has started, for example by visiting your website, or usingps -ef | grep anqicmsthe command again to view the process list.
Nginx/Apache Reverse Proxy Configuration
Although this does not directly belong to the category of boot-up self-startup, but in order for AnQiCMS to access through standard HTTP/HTTPS ports (such as 80 or 443), it is usually necessary to configure Nginx or Apache as a reverse proxy.AnQiCMS runs by default on port 8001, the reverse proxy will forward external requests to this port.You can refer to the AnQiCMS document for configuration guidelines on Nginx or Apache reverse proxy to ensure your website can be accessed normally.
location @AnqiCMS {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Make sure your Nginx or Apache service is also configured to start on boot (this is usually the default setting, throughsystemctl enable nginxorsystemctl enable apache2Implementation).
Precautions for multi-site deployment
If you deploy multiple AnQiCMS instances on a single server, each instance requires an independentstart.shscript, executable file (or at least a unique process name) and the running port. The document mentions that it can:
- Copy multiple copies of AnQiCMS code to different directories.
- Edit each instance
config.jsonofportChange to a new port (such as 8002, 8003). - Rename the AnQiCMS executable file to a new, different filenameFor example, rename according to the domain name (such as
taobaokeReplaceanqicms) - Modify each accordingly
start.shin the scriptBINNAMEVariable, as well asgrepThe name matched in the command to match the new executable file name. - For each instance in
CrontabAdd an independent task that points to the correspondingstart.shscript.
For example, if you have a second AnQiCMS instance, the executable file name ismyanqicms, the path is/www/wwwroot/myanqicms.com, and the port is 8002, then itsstart.shThe script should be modified to:
#!/bin/bash
BINNAME=myanqicms
BINPATH=/www/wwwroot/myanqicms.com
exists=`ps -ef | grep '\<myanqicms\>' |grep -v grep |wc -l`
# ... 其他保持不变 ...
and thenCrontabAdd:
*/1 * * * * /www/wwwroot/myanqicms.com/start.sh
By following these steps, you can ensure that AnQiCMS can be reliably started up automatically on a Linux server, making your website operation more worry-free.
Frequently Asked Questions (FAQ)
1. I have configured Crontab, but AnQiCMS did not start after the server restart. How should I troubleshoot?
First, please check whether the CDN link you have included in the file is correct and accurate, including the URL address,Crontabconfiguredstart.shThe script path is correct and the script has executable permissions(chmod +x /path/to/start.sh). Next, checkstart.shThe defined in the scriptBINPATHandBINNAMEDoes the variable match the actual installation path and executable file name of your AnQiCMS? You can also checkrunning.logandcheck.logFiles (usually in the AnQiCMS installation directory), these log files record the execution status of the scripts and the startup output of AnQiCMS, from which specific error information can be found. In addition, please ensure that no other process is using the port required by AnQiCMS (default is 8001), you can uselsof -i:8001Check the command.
2. Crontab runs every minute.start.shWill this cause AnQiCMS to be started multiple times?
No. AnQiCMS'sstart.shThe script includes process check logic.It will first check if the executable file of AnQiCMS is already running.If the AnQiCMS process is detected running, the script will exit directly without attempting to start again.The script will execute the actual startup command only when the AnQiCMS process is not running.start.shThe purpose of the script is to "guard" the AnQiCMS process, ensuring it always runs instead of restarting repeatedly.
3. If my AnQiCMS executable file name has been changed,start.shhow should the script be adjusted?
if the AnQiCMS executable file name is no longer the default oneanqicmsFor example, you have changed it tomywebsite_cmsThen you need to editstart.shthe script, andBINNAMEThe value of the variable is fromanqicmsis modified tomywebsite_cms. Meanwhile, the script internally uses `grep '<anq'