Get the FREE Ultimate OpenClaw Setup Guide →
npx machina-cli add skill chaterm/terminal-skills/apache --openclaw
Files (1)
SKILL.md
5.5 KB

Apache 配置

概述

Apache HTTP Server 配置、虚拟主机、模块管理等技能。

基础管理

服务控制

# CentOS/RHEL
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd

# Ubuntu/Debian
systemctl start apache2
systemctl stop apache2
systemctl restart apache2
systemctl reload apache2

# 配置测试
apachectl configtest
httpd -t

配置文件

# CentOS/RHEL
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf

# Ubuntu/Debian
/etc/apache2/apache2.conf
/etc/apache2/sites-available/
/etc/apache2/sites-enabled/

# 日志
/var/log/httpd/                     # CentOS
/var/log/apache2/                   # Ubuntu

模块管理

# Ubuntu/Debian
a2enmod rewrite                     # 启用模块
a2dismod rewrite                    # 禁用模块
a2ensite example.conf               # 启用站点
a2dissite example.conf              # 禁用站点

# CentOS/RHEL
# 编辑 /etc/httpd/conf.modules.d/
httpd -M                            # 列出已加载模块

虚拟主机

基于域名

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    
    <Directory /var/www/example>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined
</VirtualHost>

HTTPS 配置

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example
    
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.crt
    SSLCertificateKeyFile /etc/ssl/private/example.key
    SSLCertificateChainFile /etc/ssl/certs/chain.crt
    
    # SSL 优化
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    SSLHonorCipherOrder off
    
    Header always set Strict-Transport-Security "max-age=31536000"
</VirtualHost>

# HTTP 重定向
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

反向代理

基础代理

# 启用模块
# a2enmod proxy proxy_http

<VirtualHost *:80>
    ServerName api.example.com
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
    
    # 超时设置
    ProxyTimeout 300
</VirtualHost>

负载均衡

# 启用模块
# a2enmod proxy_balancer lbmethod_byrequests

<Proxy "balancer://mycluster">
    BalancerMember http://192.168.1.10:8080
    BalancerMember http://192.168.1.11:8080
    ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
    ServerName app.example.com
    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/
</VirtualHost>

URL 重写

基础重写

# 启用模块
# a2enmod rewrite

<Directory /var/www/html>
    RewriteEngine On
    
    # 强制 HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # 去除 www
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
    
    # 前端路由(SPA)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.html [L]
</Directory>

.htaccess

# /var/www/html/.htaccess
RewriteEngine On

# 隐藏 .php 扩展名
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# 防盗链
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]

安全配置

基础安全

# 隐藏版本信息
ServerTokens Prod
ServerSignature Off

# 禁用目录列表
<Directory /var/www>
    Options -Indexes
</Directory>

# 安全头
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"

访问控制

# IP 限制
<Directory /var/www/admin>
    Require ip 192.168.1.0/24
</Directory>

# 基础认证
<Directory /var/www/private>
    AuthType Basic
    AuthName "Restricted Area"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

# 创建密码文件
# htpasswd -c /etc/apache2/.htpasswd username

常见场景

场景 1:PHP 配置

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html
    
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php-fpm.sock|fcgi://localhost"
    </FilesMatch>
    
    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

场景 2:限流

# 启用模块
# a2enmod ratelimit

<Location /api>
    SetOutputFilter RATE_LIMIT
    SetEnv rate-limit 400
</Location>

场景 3:日志格式

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combined_time
CustomLog ${APACHE_LOG_DIR}/access.log combined_time

故障排查

问题排查方法
配置错误apachectl configtest
403 Forbidden检查目录权限、SELinux
500 Internal Error查看 error.log
模块未加载httpd -M 检查模块
性能问题检查 MPM 配置、连接数

Source

git clone https://github.com/chaterm/terminal-skills/blob/main/server/apache/SKILL.mdView on GitHub

Overview

本技能覆盖从基础服务管理、配置文件组织,到多域名虚拟主机、HTTPS、反向代理、URL 重写、到安全控制和故障排查的完整工作流。你可以用它在单台服务器上托管多个站点、接入后端应用、实现前端路由,以及提升站点的性能与安全性。

How This Skill Works

Apache 以一组主配置文件和站点/模块配置文件驱动行为;你通过定义 VirtualHost、加载所需模块、设置代理、重写规则与安全头来实现目标。变更后,使用 apachectl configtest 或 httpd -t 来校验配置,再重载服务以应用新设定。

When to Use It

  • 需要在同一服务器上为不同域名提供独立站点(基于域名的虚拟主机)
  • 需要为站点启用 HTTPS/SSL,并进行性能与安全优化
  • 需要将外部请求代理到后端应用(反向代理、负载均衡)
  • 需要实现 URL 重写、重定向或前端路由(SPA)支持
  • 需要提升安全性、访问控制、日志管理与故障排查能力

Quick Start

  1. 在目标系统安装并启动 Apache/httpd(CentOS/RedHat 使用 httpd,Ubuntu/Debian 使用 apache2)
  2. 启用常用模块,如 rewrite、ssl、proxy、headers,并准备站点配置目录(如 sites-available/、conf.d/)
  3. 创建一个基本的 VirtualHost(HTTP/80,或 HTTPS/443),并指向文档根,例如 /var/www/your-site
  4. 测试配置并重新加载服务:apachectl configtest 或 httpd -t;systemctl reload httpd 或 systemctl reload apache2

Best Practices

  • 使用基于域名的虚拟主机结构,避免将所有站点混在一个配置文件中
  • 仅开启需要的模块,遵循最小权限原则,提升安全性与稳定性
  • 优先强制 HTTPS,并启用 HSTS、合适的 SSL 配置(TLS 协议/密码套件)
  • 保持一致的日志格式与日志轮转,便于监控和排错
  • 在关键目录加入安全设置(禁用目录列出、设置合理的权限、启用安全头如 X-Content-Type-Options、X-Frame-Options、X-XSS-Protection)
  • 使用 Let’s Encrypt 等自动化证书管理工具,简化证书续期与部署
  • 对常见场景设置清晰的注释,便于团队协作与后续维护

Example Use Cases

  • 示例 1:在同一服务器上为 example.com 与 www.example.com 配置独立的 VirtualHost,分别指定 DocumentRoot 与日志路径
  • 示例 2:通过 ProxyPass 将 /api 请求代理到后端 Node.js 服务,同时保留原 Host 头信息(ProxyPreserveHost On)
  • 示例 3:为单页应用(SPA)配置前端路由的重写,将所有非静态资源请求转发到 index.html
  • 示例 4:将 80 端口请求重定向到 443,开启 HTTPS 与强加密设置,并添加 HSTS 头
  • 示例 5:实现简单的前端负载均衡,使用 proxy_balancer 将请求分发到多台后端实例

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers