在 Linux 上使用 mod_ssl、openssl 安装 Apache 2

举报
Tiamo_T 发表于 2022/04/07 18:17:30 2022/04/07
【摘要】 本文提供了有关如何使用 mod_ssl 安装 Apache 2 的分步说明。 我更喜欢从源代码安装 Apache,因为它让我可以更灵活地选择要启用或禁用的模块,而且我还可以在 Apache 发布补丁后立即升级或应用补丁。

本文提供了有关如何使用 mod_ssl 安装 Apache 2 的分步说明。

我更喜欢从源代码安装 Apache,因为它让我可以更灵活地选择要启用或禁用的模块,而且我还可以在 Apache 发布补丁后立即升级或应用补丁。

1.下载apache

从httpd.apache.org下载 Apache 。当前的稳定版本是 2.2.17。

获得下载最新稳定版 Apache 的直接 URL 后,使用 wget 如下所示将其直接下载到您的服务器。

cd ~
wget http://www.eng.lsu.edu/mirrors/apache//httpd/httpd-2.2.17.tar.gz
tar xvfz httpd-2.2.17.tar.gz

2. 使用 SSL/TLS 安装 Apache

查看所有可用的 Apache 安装和配置选项,如下所示。

cd httpd-2.2.17
./configure --help

要安装 Apache 模块,您通常会说 –enable-{module-name}。例如,要使用 Apache 安装 SSL,它是 –enable-ssl。要安装 ldap 模块,它是 –enable-ldap。

要卸载 Apache 附带的任何默认模块,您通常会说 –disable-{module-name}。例如,要在 Apache 中禁用基本身份验证,它是 –disable-auth-basic

在此示例中,我们将安装 Apache 和所有默认模块,并添加 –enable-ssl(安装 mod_ssl 以支持 SSL)和 –enable-so,这有助于在运行时通过 Dynamic Shared 在 Apache 中加载模块对象(DSO)机制,而不需要重新编译。

./configure --enable-ssl --enable-so
make
make install

注意:默认情况下,上述安装 Apache 在 /usr/local/apache2 下。如果您想更改此位置,请使用 ./configure 中的 –prefix 选项。

3. 在 httpd.conf 中启用 SSL

Apache 配置文件 httpd.conf 位于 /usr/local/apache2/conf 下。

取消注释 /usr/local/apache2/conf/httpd.conf 文件中的 httpd-ssl.conf Include 行。

# vi /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-ssl.conf

查看 httpd-ssl.conf 以查看所有默认 SSL 配置。大多数情况下,您不需要修改此文件中的任何内容。

vi /usr/local/apache2/conf/extra/httpd-ssl.conf

在我们启动 Apache 之前需要 SSL 证书和密钥。httpd-ssl.conf 中提到的 server.crt 和 server.key 文件需要在我们继续之前创建。

# egrep 'server.crt|server.key' httpd-ssl.conf
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"

4.创建server.crt和server.key文件

首先,使用 openssl 生成 server.key。

cd ~
openssl genrsa -des3 -out server.key 1024

上面的命令会要求输入密码。请务必记住此密码。您在稍后启动 Apache 时需要它。

如果您不提供密码,您将收到以下错误消息。

2415:error:28069065:lib(40):UI_set_result:result too small:ui_lib.c:849:You must type in 4 to 8191 characters

接下来,使用上面的 server.key 文件生成证书请求文件(server.csr)。

openssl req -new -key server.key -out server.csr

最后,使用上面的 server.key 和 server.csr 文件生成一个自签名的 ssl 证书(server.crt)。

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

5.复制server.key和server.crt

将 server.key 和 server.crt 文件复制到适当的 Apache 配置目录位置。

cd ~
cp server.key /usr/local/apache2/conf/
cp server.crt /usr/local/apache2/conf/

6.启动apache并验证SSL

如下所示启动 Apache。

/usr/local/apache2/bin/apachectl start

这将提示您输入私钥的密码。

Apache/2.2.17 mod_ssl/2.2.17 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server www.example.com:443 (RSA)
Enter pass phrase:

OK: Pass Phrase Dialog successful.

默认情况下,Apache SSL 在 443 端口上运行。打开网络浏览器并验证您是否可以使用 https://(your-ip-address) 访问您的 Apache

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200