Debian服务器给编译安装的Apache设置系统服务
1. 创建 systemd 服务文件
使用文本编辑器创建 Apache 的 systemd 服务文件:
1 |
vim /etc/systemd/system/compile-apache2.service |
将以下内容粘贴到文件中(根据你的路径调整):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=The Apache HTTP Server After=network.target [Service] Type=forking ExecStart=/usr/local/lib/apache2/bin/apachectl start ExecStop=/usr/local/lib/apache2/bin/apachectl stop ExecReload=/usr/local/lib/apache2/bin/apachectl graceful PrivateTmp=true Restart=on-abort [Install] WantedBy=multi-user.target |
2. 重新加载 systemd 配置
让 systemd 识别新创建的服务:
1 |
systemctl daemon-reload |
3. 管理 Apache 服务
现在你可以使用以下命令管理 Apache:
启动 Apache:
1 |
systemctl start compile-apache2.service |
停止 Apache:
1 |
systemctl stop compile-apache2.service |
重启 Apache:
1 |
systemctl restart compile-apache2.service |
查看状态:
1 |
systemctl status compile-apache2.service |
4. 设置开机启动
启用 Apache 开机自动启动:
1 |
systemctl enable compile-apache2.service |
验证 Apache 服务是否开机启动:
1 2 |
# 返回 enabled 是开机启动状态 返回 disable 是开机不启动 systemctl is-enabled compile-apache2.service |