在 Debian 上管理 Apache2 主要依靠 systemd 和 apache2 自带工具。下面按常见场景给你一个实用指南。
sudo apt update
sudo apt install apache2
sudo systemctl start apache2 # 启动
sudo systemctl stop apache2 # 停止
sudo systemctl restart apache2 # 重启(会断连接)
sudo systemctl reload apache2 # 重载配置(不中断连接)
sudo systemctl status apache2 # 查看状态
sudo systemctl enable apache2 # 开机自启
sudo systemctl disable apache2 # 关闭自启
Debian 的 Apache2 配置很规范:
/etc/apache2/
├── apache2.conf # 主配置文件
├── ports.conf # 端口配置
├── sites-available/ # 所有虚拟主机配置
├── sites-enabled/ # 已启用的虚拟主机(软链接)
├── mods-available/ # 所有模块
├── mods-enabled/ # 已启用模块
└── conf-available/ # 其他配置
sudo a2ensite example.conf
sudo systemctl reload apache2
sudo a2dissite example.conf
sudo systemctl reload apache2
sudo a2enmod rewrite
sudo systemctl reload apache2
sudo a2dismod rewrite
sudo apache2ctl configtest
输出 Syntax OK 表示没问题。
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
如果你开了 ufw:
sudo ufw allow 80
sudo ufw allow 443
| 功能 | 命令 |
|---|---|
| 启动 | systemctl start apache2 |
| 停止 | systemctl stop apache2 |
| 重载 | systemctl reload apache2 |
| 启用站点 | a2ensite |
| 禁用站点 | a2dissite |
| 启用模块 | a2enmod |
| 检查配置 | apache2ctl configtest |
如果你有具体需求(比如配置 HTTPS、反向代理、PHP、多站点),可以告诉我,我可以直接给你配置示例。