apache2-utils 是 Apache 服务器的配套工具集,包含 ab(Apache Bench)、htpasswd 等实用工具,常用于 Web 性能测试、密码生成等场景。
执行以下命令安装,自动解决依赖:
sudo apt update && sudo apt install apache2-utils -y
验证安装:
ab -V
出现版本号即安装成功。
CentOS 中工具包名为 httpd-tools,执行命令:
sudo yum install httpd-tools -y
验证安装:
htpasswd -h
核心功能:测试网站的并发处理能力、响应时间等性能指标。
基础语法:
ab [参数] [目标URL]
常用参数:
-n:总请求数;-c:并发数;-t:测试时长(秒)
示例:
ab -n 1000 -c 100 https://www.example.com/
核心功能:创建和更新用于 HTTP 基本认证的密码文件。
基础语法:
htpasswd [参数] [密码文件] [用户名]
常用参数:
-c:创建新密码文件(首次使用);-b:直接输入密码(非交互模式)
示例:
sudo htpasswd -c /etc/apache2/.htpasswd admin
核心功能:启动、停止、重启 Apache 服务器,查看配置状态。
常用命令:
sudo apache2ctl start # 启动服务 sudo apache2ctl stop # 停止服务 sudo apache2ctl configtest # 检查配置语法
需求:测试目标网站在 100 并发下处理 5000 个请求的性能
ab -n 5000 -c 100 https://your-website.com/index.html
结果解读:关注 Requests per second(每秒请求数)、Time per request(平均请求时间)等指标。
步骤1:创建密码文件
sudo htpasswd -c /var/www/.htpasswd testuser
步骤2:配置 Apache 虚拟主机,添加认证规则
<Directory /var/www/html>
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /var/www/.htpasswd
Require valid-user
</Directory>
步骤3:重启 Apache 生效
sudo apache2ctl restart
仅可对自己拥有权限的网站/服务器进行压力测试,未经授权测试他人网站属于违法行为,可能面临法律责任。
使用 ab 压测时,避免一次性设置过高并发数(如 -c 1000),防止目标服务器宕机或本地网络阻塞;建议逐步提升并发数。
问题1:ab: invalid URL → 检查目标 URL 是否包含协议(http/https),是否拼写正确
问题2:htpasswd: command not found → 未安装工具包,参考安装步骤重新安装