$npx -y skills add chaterm/terminal-skills --skill apacheApache HTTP Server 配置
| 1 | # Apache 配置 |
| 2 | |
| 3 | ## 概述 |
| 4 | Apache HTTP Server 配置、虚拟主机、模块管理等技能。 |
| 5 | |
| 6 | ## 基础管理 |
| 7 | |
| 8 | ### 服务控制 |
| 9 | ```bash |
| 10 | # CentOS/RHEL |
| 11 | systemctl start httpd |
| 12 | systemctl stop httpd |
| 13 | systemctl restart httpd |
| 14 | systemctl reload httpd |
| 15 | |
| 16 | # Ubuntu/Debian |
| 17 | systemctl start apache2 |
| 18 | systemctl stop apache2 |
| 19 | systemctl restart apache2 |
| 20 | systemctl reload apache2 |
| 21 | |
| 22 | # 配置测试 |
| 23 | apachectl configtest |
| 24 | httpd -t |
| 25 | ``` |
| 26 | |
| 27 | ### 配置文件 |
| 28 | ```bash |
| 29 | # CentOS/RHEL |
| 30 | /etc/httpd/conf/httpd.conf |
| 31 | /etc/httpd/conf.d/*.conf |
| 32 | |
| 33 | # Ubuntu/Debian |
| 34 | /etc/apache2/apache2.conf |
| 35 | /etc/apache2/sites-available/ |
| 36 | /etc/apache2/sites-enabled/ |
| 37 | |
| 38 | # 日志 |
| 39 | /var/log/httpd/ # CentOS |
| 40 | /var/log/apache2/ # Ubuntu |
| 41 | ``` |
| 42 | |
| 43 | ### 模块管理 |
| 44 | ```bash |
| 45 | # Ubuntu/Debian |
| 46 | a2enmod rewrite # 启用模块 |
| 47 | a2dismod rewrite # 禁用模块 |
| 48 | a2ensite example.conf # 启用站点 |
| 49 | a2dissite example.conf # 禁用站点 |
| 50 | |
| 51 | # CentOS/RHEL |
| 52 | # 编辑 /etc/httpd/conf.modules.d/ |
| 53 | httpd -M # 列出已加载模块 |
| 54 | ``` |
| 55 | |
| 56 | ## 虚拟主机 |
| 57 | |
| 58 | ### 基于域名 |
| 59 | ```apache |
| 60 | <VirtualHost *:80> |
| 61 | ServerName example.com |
| 62 | ServerAlias www.example.com |
| 63 | DocumentRoot /var/www/example |
| 64 | |
| 65 | <Directory /var/www/example> |
| 66 | Options -Indexes +FollowSymLinks |
| 67 | AllowOverride All |
| 68 | Require all granted |
| 69 | </Directory> |
| 70 | |
| 71 | ErrorLog ${APACHE_LOG_DIR}/example-error.log |
| 72 | CustomLog ${APACHE_LOG_DIR}/example-access.log combined |
| 73 | </VirtualHost> |
| 74 | ``` |
| 75 | |
| 76 | ### HTTPS 配置 |
| 77 | ```apache |
| 78 | <VirtualHost *:443> |
| 79 | ServerName example.com |
| 80 | DocumentRoot /var/www/example |
| 81 | |
| 82 | SSLEngine on |
| 83 | SSLCertificateFile /etc/ssl/certs/example.crt |
| 84 | SSLCertificateKeyFile /etc/ssl/private/example.key |
| 85 | SSLCertificateChainFile /etc/ssl/certs/chain.crt |
| 86 | |
| 87 | # SSL 优化 |
| 88 | SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 |
| 89 | SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256 |
| 90 | SSLHonorCipherOrder off |
| 91 | |
| 92 | Header always set Strict-Transport-Security "max-age=31536000" |
| 93 | </VirtualHost> |
| 94 | |
| 95 | # HTTP 重定向 |
| 96 | <VirtualHost *:80> |
| 97 | ServerName example.com |
| 98 | Redirect permanent / https://example.com/ |
| 99 | </VirtualHost> |
| 100 | ``` |
| 101 | |
| 102 | ## 反向代理 |
| 103 | |
| 104 | ### 基础代理 |
| 105 | ```apache |
| 106 | # 启用模块 |
| 107 | # a2enmod proxy proxy_http |
| 108 | |
| 109 | <VirtualHost *:80> |
| 110 | ServerName api.example.com |
| 111 | |
| 112 | ProxyPreserveHost On |
| 113 | ProxyPass / http://127.0.0.1:3000/ |
| 114 | ProxyPassReverse / http://127.0.0.1:3000/ |
| 115 | |
| 116 | # 超时设置 |
| 117 | ProxyTimeout 300 |
| 118 | </VirtualHost> |
| 119 | ``` |
| 120 | |
| 121 | ### 负载均衡 |
| 122 | ```apache |
| 123 | # 启用模块 |
| 124 | # a2enmod proxy_balancer lbmethod_byrequests |
| 125 | |
| 126 | <Proxy "balancer://mycluster"> |
| 127 | BalancerMember http://192.168.1.10:8080 |
| 128 | BalancerMember http://192.168.1.11:8080 |
| 129 | ProxySet lbmethod=byrequests |
| 130 | </Proxy> |
| 131 | |
| 132 | <VirtualHost *:80> |
| 133 | ServerName app.example.com |
| 134 | ProxyPass / balancer://mycluster/ |
| 135 | ProxyPassReverse / balancer://mycluster/ |
| 136 | </VirtualHost> |
| 137 | ``` |
| 138 | |
| 139 | ## URL 重写 |
| 140 | |
| 141 | ### 基础重写 |
| 142 | ```apache |
| 143 | # 启用模块 |
| 144 | # a2enmod rewrite |
| 145 | |
| 146 | <Directory /var/www/html> |
| 147 | RewriteEngine On |
| 148 | |
| 149 | # 强制 HTTPS |
| 150 | RewriteCond %{HTTPS} off |
| 151 | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
| 152 | |
| 153 | # 去除 www |
| 154 | RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] |
| 155 | RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301] |
| 156 | |
| 157 | # 前端路由(SPA) |
| 158 | RewriteCond %{REQUEST_FILENAME} !-f |
| 159 | RewriteCond %{REQUEST_FILENAME} !-d |
| 160 | RewriteRule ^ index.html [L] |
| 161 | </Directory> |
| 162 | ``` |
| 163 | |
| 164 | ### .htaccess |
| 165 | ```apache |
| 166 | # /var/www/html/.htaccess |
| 167 | RewriteEngine On |
| 168 | |
| 169 | # 隐藏 .php 扩展名 |
| 170 | RewriteCond %{REQUEST_FILENAME} !-d |
| 171 | RewriteCond %{REQUEST_FILENAME}\.php -f |
| 172 | RewriteRule ^(.*)$ $1.php [L] |
| 173 | |
| 174 | # 防盗链 |
| 175 | RewriteCond %{HTTP_REFERER} !^$ |
| 176 | RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC] |
| 177 | RewriteRule \.(jpg|jpeg|png|gif)$ - [F] |
| 178 | ``` |
| 179 | |
| 180 | ## 安全配置 |
| 181 | |
| 182 | ### 基础安全 |
| 183 | ```apache |
| 184 | # 隐藏版本信息 |
| 185 | ServerTokens Prod |
| 186 | ServerSignature Off |
| 187 | |
| 188 | # 禁用目录列表 |
| 189 | <Directory /var/www> |
| 190 | Options -Indexes |
| 191 | </Directory> |
| 192 | |
| 193 | # 安全头 |
| 194 | Header always set X-Content-Type-Options "nosniff" |
| 195 | Header always set X-Frame-Options "SAMEORIGIN" |
| 196 | Header always set X-XSS-Protection "1; mode=block" |
| 197 | ``` |
| 198 | |
| 199 | ### 访问控制 |
| 200 | ```apache |
| 201 | # IP 限制 |
| 202 | <Directory /var/www/admin> |
| 203 | Require ip 192.168.1.0/24 |
| 204 | </Directory> |
| 205 | |
| 206 | # 基础认证 |
| 207 | <Directory /var/www/private> |
| 208 | AuthType Basic |
| 209 | AuthName "Restricted Area" |
| 210 | AuthUserFile /etc/apache2/.htpasswd |
| 211 | Require valid-user |
| 212 | </Directory> |
| 213 | |
| 214 | # 创建密码文件 |
| 215 | # htpasswd -c /etc/apache2/.htpasswd username |
| 216 | ``` |
| 217 | |
| 218 | ## 常见场景 |
| 219 | |
| 220 | ### 场景 1:PHP 配置 |
| 221 | ```apache |
| 222 | <VirtualHost *:80> |
| 223 | ServerName example.com |
| 224 | DocumentRoot /var/www/html |
| 225 | |
| 226 | <FilesMatch \.php$> |
| 227 | SetHandler "proxy:unix:/var/run/php/php-fpm.sock|fcgi://localhost" |
| 228 | </FilesMatch> |
| 229 | |
| 230 | <Directory /var/www/html> |
| 231 | AllowOverride All |
| 232 | Require all granted |
| 233 | </Directory> |
| 234 | </VirtualHost> |
| 235 | ``` |
| 236 | |
| 237 | ### 场景 2:限流 |
| 238 | ```apache |
| 239 | # 启用模块 |
| 240 | # a2enmod ratelimit |
| 241 | |
| 242 | <Location /api> |
| 243 | SetOutputFilter RATE_LIMIT |
| 244 | SetEnv rate-limit 400 |
| 245 | </Location> |
| 246 | ``` |
| 247 | |
| 248 | ### 场景 3:日志格式 |
| 249 | ```apache |
| 250 | LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combined_time |
| 251 | CustomLog ${APACHE_LOG_DIR}/access.log combined_time |
| 252 | ``` |
| 253 | |
| 254 | ## 故障排查 |
| 255 | |
| 256 | | 问题 | 排查方法 | |
| 257 | |------|----------| |
| 258 | | 配置错误 | `apach |