Apache HTTP Server (httpd) 配置教程
1. 安装 Apache HTTP Server
在 Linux 上安装
Ubuntu/Debian:
sudo apt update
sudo apt install apache2
CentOS/RHEL:
sudo yum install httpd
在 Windows 上安装
- 下载 Apache HTTP Server 二进制文件
- 运行安装程序并按照提示操作
2. 基本配置
主配置文件通常位于:
- Linux:
/etc/apache2/apache2.conf或/etc/httpd/conf/httpd.conf - Windows:
Apache安装目录/conf/httpd.conf
常用配置指令
# 服务器根目录
ServerRoot "/etc/apache2"
# 监听端口
Listen 80
# 服务器管理员邮箱
ServerAdmin webmaster@example.com
# 服务器名称
ServerName www.example.com:80
# 文档根目录
DocumentRoot "/var/www/html"
# 目录权限设置
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
3. 虚拟主机配置
基于名称的虚拟主机
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/var/www/example.com/public_html"
ErrorLog "/var/log/apache2/example.com-error.log"
CustomLog "/var/log/apache2/example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName www.another.com
DocumentRoot "/var/www/another.com/public_html"
</VirtualHost>
基于 IP 的虚拟主机
<VirtualHost 192.168.1.100:80>
ServerName www.example.com
DocumentRoot "/var/www/example.com/public_html"
</VirtualHost>
4. 常用模块配置
启用/禁用模块
Ubuntu/Debian:
sudo a2enmod rewrite # 启用模块
sudo a2dismod rewrite # 禁用模块
CentOS/RHEL:
编辑 /etc/httpd/conf.modules.d/00-base.conf 文件
常用模块
- mod_rewrite (URL重写)
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All # 允许使用.htaccess
Require all granted
</Directory>
- mod_ssl (HTTPS支持)
sudo a2enmod ssl
sudo systemctl restart apache2
5. SSL/TLS 配置
- 生成证书 (自签名):
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/apache-selfsigned.key \
-out /etc/ssl/certs/apache-selfsigned.crt
- 配置 SSL 虚拟主机:
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot "/var/www/html"
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
</VirtualHost>
6. 访问控制和认证
基本认证
- 创建密码文件:
sudo htpasswd -c /etc/apache2/.htpasswd username
- 配置受保护目录:
<Directory "/var/www/html/protected">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
IP 限制
<Directory "/var/www/html/restricted">
Require ip 192.168.1.0/24
# 或拒绝特定IP
# Require not ip 192.168.1.100
</Directory>
7. 日志配置
访问日志
CustomLog "/var/log/apache2/access.log" combined
错误日志
ErrorLog "/var/log/apache2/error.log"
LogLevel warn
自定义日志格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
8. 性能调优
# 保持连接
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
# 多处理模块 (MPM) 配置
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
9. 常用命令
启动/停止/重启 Apache
# Ubuntu/Debian
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
# CentOS/RHEL
sudo systemctl start httpd
sudo systemctl stop httpd
sudo systemctl restart httpd
检查配置语法
sudo apachectl configtest
10. 故障排除
- 检查错误日志:
sudo tail -f /var/log/apache2/error.log
- 检查监听端口:
sudo netstat -tulnp | grep apache
- 检查模块是否加载:
sudo apachectl -M
- 检查完整配置:
sudo apachectl -S
以上是 Apache HTTP Server 的基本配置教程。根据您的具体需求,可能需要进一步调整配置。建议在进行重大更改前备份配置文件,并在修改后使用 apachectl configtest 检查语法是否正确。
(本文来源:nzw6.com)


