基本操作

1
2
3
4
5
systemctl start nginx        # 启动服务
systemctl stop nginx # 停止服务
systemctl restart nginx # 重启服务
systemctl reload nginx # 重新加载配置
systemctl status nginx # 查看状态

开机自启

1
2
3
systemctl enable nginx       # 设置开机启动
systemctl disable nginx # 取消开机启动
systemctl is-enabled nginx # 检查是否已启用

创建自定义服务

创建 /etc/systemd/system/myapp.service

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node app.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

查看日志

1
2
3
journalctl -u nginx          # 查看服务日志
journalctl -u nginx -f # 实时查看
journalctl -u nginx --since "2026-01-01" # 按时间过滤