這些天接觸瞭解SSL證書後,寫了一篇《申請免費的SSL證書,開通https網站》博文,其中簡單記錄了Apache的設置,後來又涉及到多個域名、泛域名解析、通配符SSL證書、單服務器/多服務器、IP、端口等方方面面,去查了一些資料纔在Apache上配置成功,乾脆從新寫一篇博文來記錄。php
先寫各類可能的狀況:html
設置的過程:node
下面是一個修改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 請求狀態 失敗」也一併解決。
轉載自:這裏