由于Hexo自带的Hexo Server默认是在前台调用,如果不小心关掉ssh连接,或者为了省事直接使用hexo server拉起静态Web服务,就需要把hexo server配置为后台服务。以下,使用系统默认的Systemd来管理hexo的web服务。

# 编辑service脚本
#vim /usr/lib/systemd/system/hexo.service

[Unit]
Description=Hexo server

[Service]
ExecStart=/usr/local/bin/hexo server
Restart=always
User=[使用文件owner用户名]
Group=[使用文件owner用户组名]
Environment=PATH=/usr/bin:/usr/local/bin
WorkingDirectory=/home/[使用文件owner]/blog/
StandarOutput=syslog
StandarError=syslog
SyslogIdentifier=hexo

[Install]
WantedBy=multi-user.target

配置完成之后,设置服务自启动,并检查服务运行状态

#systemctl daemon-reload
#systemctl enable --now hexo.service 
#systemctl status hexo.service 
● hexo.service - Hexo server
     Loaded: loaded (/usr/lib/systemd/system/hexo.service; enabled; preset: disabled)
     Active: active (running) since Tue 2023-06-27 22:32:24 CST; 7s ago
   Main PID: 30567 (hexo)
      Tasks: 12 (limit: 4288)
     Memory: 168.6M
        CPU: 9.803s
     CGroup: /system.slice/hexo.service
             └─30567 hexo

Jun 27 22:32:27 wulanchabu hexo[30567]:   ============================================================>
Jun 27 22:32:27 wulanchabu hexo[30567]:       #####  #    # ##### ##### ###### #####  ###### #      # >
Jun 27 22:32:27 wulanchabu hexo[30567]:       #    # #    #   #     #   #      #    # #      #       #>
Jun 27 22:32:27 wulanchabu hexo[30567]:       #####  #    #   #     #   #####  #    # #####  #        #
Jun 27 22:32:27 wulanchabu hexo[30567]:       #    # #    #   #     #   #      #####  #      #        #
Jun 27 22:32:27 wulanchabu hexo[30567]:       #    # #    #   #     #   #      #   #  #      #        #
Jun 27 22:32:27 wulanchabu hexo[30567]:       #####   ####    #     #   ###### #    # #      ######   #
Jun 27 22:32:27 wulanchabu hexo[30567]:                             4.9.0
Jun 27 22:32:27 wulanchabu hexo[30567]:   ============================================================>
Jun 27 22:32:28 wulanchabu hexo[30567]: INFO  Start processing
# 检查端口
#netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9484/nginx: master  
tcp6       0      0 :::4000                 :::*                    LISTEN      30567/hexo          
tcp6       0      0 :::80                   :::*                    LISTEN      9484/nginx: master