温馨提示×

ubuntu apache支持哪些配置

小樊
38
2026-10-19 18:31:51
栏目: 智能运维

在 Ubuntu 上,Apache(通常是 Apache2)支持非常丰富和灵活的配置。下面按常见分类给你一个较系统的说明,适合实际运维和使用。


一、Apache 配置文件结构(Ubuntu 特有)

Ubuntu 中 Apache 配置主要位于:

/etc/apache2/
├── apache2.conf          # 主配置文件
├── ports.conf           # 监听端口
├── mods-available/      # 可用模块
├── mods-enabled/        # 已启用模块(软链接)
├── sites-available/     # 虚拟主机配置
├── sites-enabled/       # 启用站点(软链接)
├── conf-available/      # 额外配置
├── conf-enabled/        # 启用配置
└── envvars             # 环境变量

常用命令:

sudo a2ensite xxx      # 启用站点
sudo a2dissite xxx     # 禁用站点
sudo a2enmod xxx       # 启用模块
sudo a2dismod xxx      # 禁用模块
sudo systemctl reload apache2

二、核心服务器配置(apache2.conf / 全局)

支持的主要配置项包括:

1. 基本服务器参数

ServerName www.example.com
ServerAdmin admin@example.com
ServerTokens Prod
ServerSignature Off

2. 目录与权限

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3. 超时与性能

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

三、虚拟主机(VirtualHost)

最常用功能之一。

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

支持:

  • 多域名
  • 多端口
  • HTTPS(*:443)
  • 反向代理
  • 子目录站点

四、HTTPS / SSL 配置

启用模块:

sudo a2enmod ssl

示例:

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.crt
    SSLCertificateKeyFile /etc/ssl/private/example.key
</VirtualHost>

常配合:

  • Let’s Encrypt(certbot)
  • HTTP 强制跳转 HTTPS

五、URL 重写(Rewrite)

sudo a2enmod rewrite
RewriteEngine On
RewriteRule ^old.html$ new.html [R=301,L]

支持:

  • 伪静态
  • 跳转
  • 防盗链

六、反向代理(非常常用)

sudo a2enmod proxy proxy_http
ProxyPass /api/ http://127.0.0.1:3000/
ProxyPassReverse /api/ http://127.0.0.1:3000/

支持:

  • Node.js
  • Java
  • Python 后端
  • Docker 服务

七、访问控制与安全

Require ip 192.168.1.0/24
Require all denied
<Files *.php>
    Require all granted
</Files>

可结合:

  • .htaccess
  • ModSecurity

八、日志配置

LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog ${APACHE_LOG_DIR}/access.log common
ErrorLog ${APACHE_LOG_DIR}/error.log

支持:

  • 访问日志
  • 错误日志
  • 条件日志

九、常见模块支持

Ubuntu Apache 支持大量模块,例如:

  • rewrite:URL重写
  • ssl:HTTPS
  • proxy / proxy_http:反向代理
  • headers:HTTP头控制
  • deflate / gzip:压缩
  • cache / cache_disk:缓存
  • php / php8.1:PHP支持
  • auth_basic:基础认证

十、.htaccess 支持的配置

(需 AllowOverride All)

Options -Indexes
ErrorDocument 404 /404.html
Redirect 301 /old /new

总结一句话

Ubuntu 上的 Apache 支持 虚拟主机、SSL、重写、代理、权限控制、日志、模块扩展 等几乎全部 Web 服务器功能,配置灵活且适合生产环境。

如果你有具体需求(比如:
✅ 配置 HTTPS
✅ 多站点
✅ 反向代理
✅ 限制 IP
✅ 性能优化)
可以直接告诉我,我可以给你完整可直接用的配置示例。

0 踩