apache
8
总安装量
4
周安装量
#33775
全站排名
安装命令
npx skills add https://github.com/chaterm/terminal-skills --skill apache
Agent 安装分布
claude-code
4
opencode
3
windsurf
2
codex
2
github-copilot
2
antigravity
2
Skill 文档
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 é ç½®ãè¿æ¥æ° |