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

这些天接触了解SSL证书后,写了一篇《申请免费的SSL证书,开通https网站》博文,其中简单记录了Apache的设置,后来又涉及到多个域名、泛域名解析、通配符SSL证书、单服务器/多服务器、IP、端口等方方面面,去查了一些资料才在Apache上配置成功,干脆从新写一篇博文来记录。php

多种状况

  先写各类可能的状况:html

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

设置过程

  设置的过程:node

  • 首先SSL证书提供商,根据本身的须要及预算选择,若是本身的站点多,最好是选择支持多域名、通配符的证书,例如StartCom的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文件的例子:apache

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服务后生效,日志文件依然是之前的。dom


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

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());打印出来是这样的:ide

(
    [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 请求状态 失败”也一并解决。

转载自:这里

相关文章
相关标签/搜索