Apache源码编译开启SSL支持需先安装OpenSSL开发库,再通过./configure --enable-ssl --with-ssl=/usr --enable-so等参数启用mod_ssl模块,编译安装后用httpd -M验证ssl_module是否加载成功。

Apache 源码编译时开启 SSL 模块支持,核心是让 mod_ssl 在构建阶段被静态或动态集成进服务器,并确保 OpenSSL 开发库可用。整个过程不依赖已安装的 Apache,而是从源码开始控制模块启用。
确认 OpenSSL 开发环境就绪
编译前必须安装 OpenSSL 及其头文件和静态库(不只是运行时的 openssl 命令):
- Ubuntu/Debian:
sudo apt install openssl libssl-dev - CentOS/RHEL:
sudo yum install openssl-devel或sudo dnf install openssl-devel - 验证:执行
openssl version看版本,pkg-config --modversion openssl应返回版本号,ls /usr/include/openssl/ssl.h应存在
configure 阶段启用 SSL 支持
进入 Apache 源码根目录后,运行 ./configure 时加入以下关键参数:
-
--enable-ssl:启用mod_ssl模块(必需) -
--with-ssl=/path/to/openssl:显式指定 OpenSSL 安装路径(仅当 OpenSSL 不在系统默认路径时需要,例如自编译安装到/opt/openssl) -
--enable-so:启用 DSO(Dynamic Shared Object)支持——即使静态编译mod_ssl,也建议开启,便于后续加载其他模块 - 可选但推荐:
--with-crypto(部分旧版本需显式启用加密后端)
示例命令:
./configure \ --prefix=/usr/local/apache2 \ --enable-ssl \ --with-ssl=/usr \ --enable-so \ --enable-rewrite \ --enable-headers
编译与安装
make -j$(nproc) sudo make install
安装完成后,检查模块是否内置:
/usr/local/apache2/bin/httpd -M | grep ssl
若输出 ssl_module (static),说明已静态集成;若为 (shared),则 mod_ssl.so 已生成在 modules/ 目录下,且 httpd.conf 中会自动包含 LoadModule ssl_module modules/mod_ssl.so(取决于 --enable-module=so 和 apxs 环境)。
验证 HTTPS 基础能力
无需立即配证书,先确认模块加载成功:
- 编辑
conf/httpd.conf,确保有Listen 443和<IfModule ssl_module>区块 - 运行
sudo /usr/local/apache2/bin/apachectl configtest,无报错即表示 SSL 模块已就位 - 启动服务后,
netstat -tlnp | grep :443应可见监听状态(即使证书未配置,端口可监听)
不复杂但容易忽略


















