阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

HTTPS多网站1个IP多个SSL证书的Apache设置办法

248次阅读
没有评论

共计 5834 个字符,预计需要花费 15 分钟才能阅读完成。

简单记录了 Apache 的设置,后来又涉及到多个域名、泛域名解析、通配符 SSL 证书、单服务器 / 多服务器、IP、端口等方方面面,去查了一些资料才在 Apache 上配置成功,干脆重新写一篇博文来记录。

多种情况

先写各种可能的情况:

  • 老式的 SSL 证书是一个证书一个站点一个 IP 的一一对应,但后来有了改进;
  • 可以配置为一台服务器多个 IP,分别对应不同的站点、不同的证书;
  • 还可以配置为一台服务器一个 IP,多个端口号对应不同的站点、不同的证书;
  • 后来出现 SNI(Server Name Indication 服务器名称指示)技术,让 https 与 http 一样实现一台服务器多个虚拟站点,每个站点都可以对应不同的证书,无需多个 IP、无需多个端口(全部都用 https 标准的端口号 443),多个域名、泛域名都支持。

设置过程

设置的过程:

  • 首先 SSL 证书提供商,根据自己的需要及预算选择,如果自己的站点多,最好是选择支持多域名、通配符的证书,例如 StartCom(link is external) 的 EV、OV、IV 认证支持的证书(DV 认证不支持通配符);
  • 购买需要的证书,这个过程中需要上传或者粘贴 CSR(PEM 格式),这个 CSR 可以用 startcomtool.exe 来生成(同时保存 Private Key 文件 startssl.key 和 CSR 文件 startssl.csr 到本机),也可以在 Linux 下运行 openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr 来生成 key 和 csr;
  • 下载生成的证书压缩包文件 example.com.zip,解压后有 ApacheServer.zip、IISServer.zip、NginxServer.zip、OtherServer.zip 四个压缩文件,再解压其中的 ApacheServer.zip 得到两个证书文件 1_root_bundle.crt、2_example.com.crt;
  • 上传 startssl.key 和 startssl.csr 两个文件以及两个证书文件 1_root_bundle.crt、2_example.com.crt 到 apache/conf 目录下,将 startssl.csr 加到 2_example.com.crt 文件后面形成一个新的文件 startssl.crt;
  • 修改 httpd.conf 或者包含的 extra/httpd-ssl.conf 文件,在 ssl 站点中设置 key 为上传的 startssl.key,crt 为合并的 startssl.crt,证书链 crt 为 1_root_bundle.crt(每个证书的这三个文件内容都不一样,即使文件名一样);
  • 如果需要设置多个 SSL 站点,在 Apache 2.2 以上版本中是开启 SSL 模块后是直接支持 SNI 的,添加 NameVirtualHost *:443 和 SSLStrictSNIVHostCheck off 两句后,就可以像 http 虚拟站点一样设置多个 https 虚拟站点;
  • 多个 https 虚拟站点可以分别指向多个不同的证书文件,其中第一个默认 https 站点是在后续 https 站点配置找不到的时候自动使用的默认配置;
  • https 虚拟站点与 http 虚拟站点配置一样,可以使用 ServerAlias 来将多个子域名指向同一个目录、采用相同的 SSL 证书;
  • 重启 apache,上面修改的配置就可以生效,再用浏览器检查是否有问题,特别是查看一下安全证书是否与申请的一致,如果提示网页中有部分非安全内容,则要检查嵌入的非安全部分内容,改为安全的内容;
  • 没有问题了可以修改站点中的链接,让 http 都改为 https,如果是内部链接则与 http 或者 https 无关,可以不修改,如果是写死的 http,可以改为 https 或者内部链接,还可以改为 //example.com 这样的 URL 形式;
  • 最后修改 apache 配置文件或者.htaccess,让以前的 http 访问全部 301 跳转到 https 对应的网页。

代码示范

下面是一个修改 httpd-ssl.conf 文件的例子:

Listen 443
#Listen 8081
NameVirtualHost *:443
SSLStrictSNIVHostCheck off
 
<VirtualHost _default_:443>
DocumentRoot “/usr/local/apache/htdocs/example.com”
ServerName example.com
ServerAlias subdomain.example.com
ServerAdmin you@example.com
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile “/usr/local/apache/conf/server.crt”
SSLCertificateKeyFile “/usr/local/apache/conf/server.key”
SSLCertificateChainFile “/usr/local/apache/conf/1_root_bundle.crt”
<FilesMatch “\.(cgi|shtml|phtml|php)$”>
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory “/usr/local/apache/htdocs/example.com”>
    AllowOverride All
    SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
 
<VirtualHost *:443>
DocumentRoot “/usr/local/apache/htdocs/example2.com”
ServerName example2.com
ServerAlias subdomain.example2.com
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile “/usr/local/apache/conf/server2.crt”
SSLCertificateKeyFile “/usr/local/apache/conf/server2.key”
SSLCertificateChainFile “/usr/local/apache/conf/1_root_bundle2.crt”
<FilesMatch “\.(cgi|shtml|phtml|php)$”>
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory “/usr/local/apache/htdocs/example2.com”>
    AllowOverride All
    SSLOptions +StdEnvVars
</Directory>
</VirtualHost>

修改.htaccess 文件实现 301 永久重定向的例子:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

补充:日志问题,为了简化可以把 httpd-ssl.conf 中的日志都关闭:

#ErrorLog “/usr/local/apache/logs/error_log”
#TransferLog “/usr/local/apache/logs/access_log”
#CustomLog “/usr/local/apache/logs/ssl_request_log” \
#          “%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \”%r\” %b”

然后修改 httpd.conf 中的设置,添加 port:%p,从端口号是 80 还是 443 来分辨 http 和 https:

LogFormat “%h %l %u %t port:%p \”%{Host}i\” \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
CustomLog “|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_%Y-%m-%d.log 86400 480” combined

重启 httpd 服务后生效,日志文件依然是以前的。

再补充:在部分阿里云国内服务器上使用 get_headers(‘https://www.baidu.com/’,1(link is external)); 这样的语句报错:

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 在 eval() (行 6 在 /mnt/gb/www/drupal.chahaoba.com/modules/php/php.module(80) : eval()’d code).
Warning: get_headers(): Failed to enable crypto 在 eval() (行 6 在 /mnt/gb/www/drupal.chahaoba.com/modules/php/php.module(80) : eval()’d code).
Warning: get_headers(https://www.baidu.com/node/4): failed to open stream: operation failed 在 eval() ( 行 6 在 /mnt/gb/www/drupal.chahaoba.com/modules/php/php.module(80) : eval()’d code).

用 print_r(openssl_get_cert_locations()); 打印出来是这样的:

(
    [default_cert_file] => /usr/local/ssl/cert.pem
    [default_cert_file_env] => SSL_CERT_FILE
    [default_cert_dir] => /usr/local/ssl/certs
    [default_cert_dir_env] => SSL_CERT_DIR
    [default_private_dir] => /usr/local/ssl/private
    [default_default_cert_area] => /usr/local/ssl
    [ini_cafile] =>
    [ini_capath] =>
)

而不报错的国外服务器上打印出来是这样的:

 (
    [default_cert_file] => /etc/pki/tls/cert.pem
    [default_cert_file_env] => SSL_CERT_FILE
    [default_cert_dir] => /etc/pki/tls/certs
    [default_cert_dir_env] => SSL_CERT_DIR
    [default_private_dir] => /etc/pki/tls/private
    [default_default_cert_area] => /etc/pki/tls
    [ini_cafile] =>
    [ini_capath] =>
)

原因可能是安装的 CentOS 版本及 php 版本上有小的差别,修改 /alidata/server/php5/etc/php.ini 强制设置证书路径:

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =
 
[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the “cafile” SSL stream context
; option.
;openssl.cafile=
openssl.cafile=”/etc/pki/tls/cert.pem”
 
; If openssl.cafile is not specified or if the CA file is not found, the
; directory pointed to by openssl.capath is searched for a suitable
; certificate. This value must be a correctly hashed certificate directory.
; Most users should not specify a value for this directive as PHP will
; attempt to use the OS-managed cert stores in its absence. If specified,
; this value may still be overridden on a per-stream basis via the “capath”
; SSL stream context option.
;openssl.capath=
openssl.capath=”/etc/pki/tls/certs”

重启 apache 后解决。Drupal 网站状态报告中的“HTTP 请求状态 失败”也一并解决。

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2018-01/150589.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计5834字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中