Apache 基本命令 #

服务管理命令 #

systemctl 命令(推荐) #

bash
# ============================================
# Ubuntu/Debian
# ============================================

# 启动 Apache
sudo systemctl start apache2

# 停止 Apache
sudo systemctl stop apache2

# 重启 Apache
sudo systemctl restart apache2

# 优雅重启(不中断现有连接)
sudo systemctl reload apache2

# 查看状态
sudo systemctl status apache2

# 设置开机自启
sudo systemctl enable apache2

# 取消开机自启
sudo systemctl disable apache2

# 查看是否开机自启
sudo systemctl is-enabled apache2

# ============================================
# CentOS/RHEL
# ============================================

# 启动 Apache
sudo systemctl start httpd

# 停止 Apache
sudo systemctl stop httpd

# 重启 Apache
sudo systemctl restart httpd

# 优雅重启
sudo systemctl reload httpd

# 查看状态
sudo systemctl status httpd

# 设置开机自启
sudo systemctl enable httpd

# 取消开机自启
sudo systemctl disable httpd

service 命令(旧系统) #

bash
# ============================================
# Ubuntu/Debian
# ============================================

sudo service apache2 start
sudo service apache2 stop
sudo service apache2 restart
sudo service apache2 reload
sudo service apache2 status

# ============================================
# CentOS/RHEL
# ============================================

sudo service httpd start
sudo service httpd stop
sudo service httpd restart
sudo service httpd reload
sudo service httpd status

apachectl 命令 #

bash
# 启动 Apache
sudo apachectl start

# 停止 Apache
sudo apachectl stop

# 重启 Apache
sudo apachectl restart

# 优雅重启(推荐用于配置更新)
sudo apachectl graceful

# 测试配置语法
sudo apachectl configtest

# 显示版本
apachectl -v

# 显示编译参数
apachectl -V

# 显示已加载模块
apachectl -M

# 显示虚拟主机配置
apachectl -S

# 完整状态信息
sudo apachectl fullstatus

Ubuntu/Debian 专用命令 #

bash
# apache2ctl 命令
sudo apache2ctl start
sudo apache2ctl stop
sudo apache2ctl restart
sudo apache2ctl graceful
sudo apache2ctl configtest

# 模块管理
sudo a2enmod rewrite      # 启用模块
sudo a2dismod rewrite     # 禁用模块

# 站点管理
sudo a2ensite example.com.conf    # 启用站点
sudo a2dissite 000-default.conf   # 禁用站点

# 配置管理
sudo a2enconf security    # 启用配置
sudo a2disconf security   # 禁用配置

启动、停止、重启的区别 #

text
┌─────────────────────────────────────────────────────────────┐
│                    服务操作对比                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  start(启动)                                              │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  - 启动 Apache 服务                                  │   │
│  │  - 如果服务已运行,则报错                            │   │
│  │  - 所有工作进程重新创建                              │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  stop(停止)                                               │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  - 完全停止 Apache 服务                              │   │
│  │  - 中断所有现有连接                                  │   │
│  │  - 所有工作进程终止                                  │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  restart(重启)                                            │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  = stop + start                                      │   │
│  │  - 先停止再启动                                      │   │
│  │  - 中断所有现有连接                                  │   │
│  │  - 适合重大配置更改                                  │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  reload / graceful(优雅重启)                              │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  - 不中断现有连接                                    │   │
│  │  - 重新加载配置文件                                  │   │
│  │  - 新请求使用新配置                                  │   │
│  │  - 旧请求完成后旧进程退出                            │   │
│  │  - 推荐用于配置更新                                  │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

使用场景 #

text
┌─────────────────────────────────────────────────────────────┐
│                    操作选择指南                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  使用 restart 的场景:                                       │
│  - 更新了 MPM 配置                                          │
│  - 更新了 Listen 端口                                       │
│  - 加载/卸载了模块                                          │
│  - Apache 升级后                                            │
│                                                             │
│  使用 reload/graceful 的场景:                               │
│  - 修改了虚拟主机配置                                       │
│  - 修改了 URL 重写规则                                      │
│  - 修改了访问控制规则                                       │
│  - 修改了日志配置                                           │
│  - 生产环境配置更新(推荐)                                  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

配置测试命令 #

语法检查 #

bash
# 测试配置语法
sudo apachectl configtest
# 或
sudo apache2ctl configtest    # Ubuntu/Debian
# 或
sudo httpd -t                 # CentOS/RHEL

# 成功输出
# Syntax OK

# 错误输出示例
# AH00526: Syntax error on line 20 of /etc/apache2/sites-enabled/000-default.conf:
# Invalid command 'ServerNmae', perhaps misspelled or defined by a module not included in the server configuration

详细配置检查 #

bash
# 显示虚拟主机配置
sudo apachectl -S

# 输出示例
# VirtualHost configuration:
# *:80                   is a NameVirtualHost
#          default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
#          port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
#          port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
#                  alias www.example.com

# 显示已加载模块
sudo apachectl -M

# 输出示例
# Loaded Modules:
#  core_module (static)
#  so_module (static)
#  watchdog_module (static)
#  http_module (static)
#  ...
#  rewrite_module (shared)
#  ssl_module (shared)

# 显示编译参数
sudo apachectl -V

# 显示版本
sudo apachectl -v
# Server version: Apache/2.4.52 (Ubuntu)
# Server built:   2023-01-17T16:56:15

日志查看命令 #

日志文件位置 #

text
┌─────────────────────────────────────────────────────────────┐
│                    Apache 日志位置                           │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Ubuntu/Debian:                                             │
│  /var/log/apache2/access.log    # 访问日志                 │
│  /var/log/apache2/error.log     # 错误日志                 │
│  /var/log/apache2/other_vhosts_access.log  # 其他虚拟主机   │
│                                                             │
│  CentOS/RHEL:                                               │
│  /var/log/httpd/access_log      # 访问日志                 │
│  /var/log/httpd/error_log       # 错误日志                 │
│                                                             │
│  macOS (Homebrew):                                          │
│  /opt/homebrew/var/log/httpd/access_log                    │
│  /opt/homebrew/var/log/httpd/error_log                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

实时查看日志 #

bash
# 实时查看访问日志
sudo tail -f /var/log/apache2/access.log

# 实时查看错误日志
sudo tail -f /var/log/apache2/error.log

# 同时查看两个日志
sudo tail -f /var/log/apache2/*.log

日志分析命令 #

bash
# 查看最近 100 行访问日志
sudo tail -n 100 /var/log/apache2/access.log

# 查看最近 100 行错误日志
sudo tail -n 100 /var/log/apache2/error.log

# 搜索特定 IP 的访问记录
sudo grep "192.168.1.100" /var/log/apache2/access.log

# 搜索特定状态码
sudo grep " 404 " /var/log/apache2/access.log
sudo grep " 500 " /var/log/apache2/access.log

# 统计访问量前 10 的 IP
sudo awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head -10

# 统计访问量前 10 的 URL
sudo awk '{print $7}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head -10

# 统计各状态码数量
sudo awk '{print $9}' /var/log/apache2/access.log | sort | uniq -c | sort -nr

# 统计某天的访问量
sudo grep "29/Mar/2026" /var/log/apache2/access.log | wc -l

# 查看错误日志中的错误类型
sudo grep -E "\[error\]|\[crit\]|\[alert\]" /var/log/apache2/error.log

状态查看命令 #

systemctl 状态 #

bash
# 查看详细状态
sudo systemctl status apache2

# 输出示例
# ● apache2.service - The Apache HTTP Server
#      Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
#      Active: active (running) since Sat 2026-03-29 12:00:00 UTC; 1h ago
#        Docs: https://httpd.apache.org/docs/2.4/
#    Main PID: 1234 (apache2)
#       Tasks: 55 (limit: 4915)
#      Memory: 15.2M
#      CGroup: /system.slice/apache2.service
#              ├─1234 /usr/sbin/apache2 -k start
#              ├─1235 /usr/sbin/apache2 -k start
#              └─1236 /usr/sbin/apache2 -k start

进程查看 #

bash
# 查看 Apache 进程
ps aux | grep apache2
# 或
ps aux | grep httpd

# 输出示例
# root      1234  0.0  0.5 123456  5678 ?        Ss   12:00   0:00 /usr/sbin/apache2 -k start
# www-data  1235  0.0  0.3 123456  3456 ?        S    12:00   0:00 /usr/sbin/apache2 -k start
# www-data  1236  0.0  0.3 123456  3456 ?        S    12:00   0:00 /usr/sbin/apache2 -k start

# 查看 Apache 进程数量
ps aux | grep apache2 | wc -l

# 查看连接数
sudo netstat -an | grep :80 | wc -l

# 查看各状态连接数
sudo netstat -an | grep :80 | awk '{print $6}' | sort | uniq -c

端口监听查看 #

bash
# 查看 Apache 监听的端口
sudo netstat -tlnp | grep apache2
# 或
sudo ss -tlnp | grep apache2

# 输出示例
# tcp6  0  0 :::80   :::*  LISTEN  1234/apache2
# tcp6  0  0 :::443  :::*  LISTEN  1234/apache2

服务器状态模块 #

apache
# 启用状态模块
sudo a2enmod status

# 配置访问权限
# /etc/apache2/mods-available/status.conf
<Location /server-status>
    SetHandler server-status
    Require local
    # Require ip 192.168.1.0/24  # 允许特定 IP
</Location>

# 重启 Apache
sudo systemctl restart apache2
bash
# 命令行查看状态
sudo apachectl fullstatus

# 或访问
curl http://localhost/server-status
curl http://localhost/server-status?auto  # 机器可读格式

模块管理命令 #

Ubuntu/Debian 模块管理 #

bash
# 列出可用模块
ls /etc/apache2/mods-available/

# 列出已启用模块
ls /etc/apache2/mods-enabled/

# 启用模块
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod expires
sudo a2enmod deflate
sudo a2enmod proxy
sudo a2enmod proxy_http

# 禁用模块
sudo a2dismod rewrite
sudo a2dismod autoindex

# 启用后需要重启
sudo systemctl restart apache2

CentOS/RHEL 模块管理 #

bash
# 查看已加载模块
httpd -M

# 启用模块(编辑配置文件)
# 在 /etc/httpd/conf.modules.d/ 目录创建配置文件

# 例如启用 rewrite 模块
echo "LoadModule rewrite_module modules/mod_rewrite.so" | \
  sudo tee /etc/httpd/conf.modules.d/00-rewrite.conf

# 重启服务
sudo systemctl restart httpd

站点管理命令 #

Ubuntu/Debian 站点管理 #

bash
# 列出可用站点
ls /etc/apache2/sites-available/

# 列出已启用站点
ls /etc/apache2/sites-enabled/

# 启用站点
sudo a2ensite example.com.conf

# 禁用站点
sudo a2dissite example.com.conf

# 启用后需要重启
sudo systemctl reload apache2

创建新站点 #

bash
# 创建站点配置文件
sudo nano /etc/apache2/sites-available/example.com.conf

# 启用站点
sudo a2ensite example.com.conf

# 测试配置
sudo apache2ctl configtest

# 重载配置
sudo systemctl reload apache2

常见操作示例 #

更新配置后重载 #

bash
# 1. 修改配置文件
sudo nano /etc/apache2/sites-available/example.com.conf

# 2. 测试配置语法
sudo apache2ctl configtest

# 3. 如果输出 Syntax OK,重载配置
sudo systemctl reload apache2

# 4. 查看是否生效
sudo systemctl status apache2

启用 HTTPS #

bash
# 1. 启用 SSL 模块
sudo a2enmod ssl

# 2. 启用 SSL 站点配置
sudo a2ensite default-ssl

# 3. 测试配置
sudo apache2ctl configtest

# 4. 重启 Apache
sudo systemctl restart apache2

启用 URL 重写 #

bash
# 1. 启用 rewrite 模块
sudo a2enmod rewrite

# 2. 修改虚拟主机配置
sudo nano /etc/apache2/sites-available/example.com.conf

# 添加:
# <Directory /var/www/html>
#     AllowOverride All
# </Directory>

# 3. 重启 Apache
sudo systemctl restart apache2

排查 500 错误 #

bash
# 1. 查看错误日志
sudo tail -n 50 /var/log/apache2/error.log

# 2. 检查配置语法
sudo apache2ctl configtest

# 3. 检查文件权限
ls -la /var/www/html/

# 4. 检查 SELinux(CentOS)
getenforce
ls -Z /var/www/html/

# 5. 增加日志级别调试
# 在配置文件中设置 LogLevel debug
sudo systemctl reload apache2

命令速查表 #

text
┌─────────────────────────────────────────────────────────────┐
│                    Apache 命令速查表                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  服务管理                                                   │
│  ├── systemctl start apache2/httpd    # 启动               │
│  ├── systemctl stop apache2/httpd     # 停止               │
│  ├── systemctl restart apache2/httpd  # 重启               │
│  ├── systemctl reload apache2/httpd   # 优雅重载           │
│  └── systemctl status apache2/httpd   # 查看状态           │
│                                                             │
│  配置测试                                                   │
│  ├── apachectl configtest             # 测试语法           │
│  ├── apachectl -S                     # 虚拟主机配置       │
│  ├── apachectl -M                     # 已加载模块         │
│  └── apachectl -V                     # 编译参数           │
│                                                             │
│  模块管理(Ubuntu/Debian)                                  │
│  ├── a2enmod <module>                 # 启用模块           │
│  └── a2dismod <module>                # 禁用模块           │
│                                                             │
│  站点管理(Ubuntu/Debian)                                  │
│  ├── a2ensite <site>                  # 启用站点           │
│  └── a2dissite <site>                 # 禁用站点           │
│                                                             │
│  日志查看                                                   │
│  ├── tail -f /var/log/apache2/access.log                    │
│  └── tail -f /var/log/apache2/error.log                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

下一步 #

掌握了基本命令后,继续学习 静态资源服务,了解如何配置 Apache 提供静态文件服务!

最后更新:2026-03-29