配置流程

一般情况下,httpd的安装,默认没有mod_ssl模块,需要通过yum自行安装。

  1. #可以通过httpd -M指令查询是否存在ssl模块
  2. #如果httpd.conf文件还没有配置ServerName,则-M操作会报错
  3. httpd -M |grep ssl
  4. #yum安装mod_ssl
  5. yum install -y mod_ssl.x86_64
  6. systemctl restart httpd
  7. httpd -M|grep ssl
  8. #在httpd下创建一个存放ssl证书的cert目录
  9. mkdir cert
  10. #配置ssl
  11. vim conf.d/ssl.conf
  12. #
  13. # When we also provide SSL we have to listen to the
  14. # standard HTTPS port in addition.
  15. #
  16. Listen 443 https
  17. ##
  18. ## SSL Global Context
  19. ##
  20. ## All SSL configuration in this context applies both to
  21. ## the main server and all SSL-enabled virtual hosts.
  22. ##
  23. # Pass Phrase Dialog:
  24. # Configure the pass phrase gathering process.
  25. # The filtering dialog program (`builtin' is a internal
  26. # terminal dialog) has to provide the pass phrase on stdout.
  27. SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
  28. # Inter-Process Session Cache:
  29. # Configure the SSL Session Cache: First the mechanism
  30. # to use and second the expiring timeout (in seconds).
  31. SSLSessionCache shmcb:/run/httpd/sslcache(512000)
  32. SSLSessionCacheTimeout 300
  33. #
  34. # Use "SSLCryptoDevice" to enable any supported hardware
  35. # accelerators. Use "openssl engine -v" to list supported
  36. # engine names. NOTE: If you enable an accelerator and the
  37. # server does not start, consult the error logs and ensure
  38. # your accelerator is functioning properly.
  39. #
  40. SSLCryptoDevice builtin
  41. #SSLCryptoDevice ubsec
  42. ##
  43. ## SSL Virtual Host Context
  44. ##
  45. <VirtualHost _default_:443>
  46. DocumentRoot "/var/www/html"
  47. ServerName www.hunzi.online
  48. ErrorLog logs/ssl_error_log
  49. TransferLog logs/ssl_access_log
  50. LogLevel warn
  51. SSLEngine on
  52. SSLHonorCipherOrder on
  53. SSLCipherSuite PROFILE=SYSTEM
  54. SSLProxyCipherSuite PROFILE=SYSTEM
  55. <FilesMatch "\.(cgi|shtml|phtml|php)$">
  56. SSLOptions +StdEnvVars
  57. </FilesMatch>
  58. <Directory "/var/www/cgi-bin">
  59. SSLOptions +StdEnvVars
  60. </Directory>
  61. BrowserMatch "MSIE [2-5]" \
  62. nokeepalive ssl-unclean-shutdown \
  63. downgrade-1.0 force-response-1.0
  64. CustomLog logs/ssl_request_log \
  65. "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
  66. SSLProtocol all -SSLv2 -SSLv3
  67. SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
  68. SSLHonorCipherOrder on
  69. SSLCertificateFile cert/www.hunzi.online_public.crt
  70. SSLCertificateKeyFile cert/www.hunzi.online.key
  71. SSLCertificateChainFile cert/www.hunzi.online_chain.crt
  72. </VirtualHost>
  73. #检查配置文件正确性
  74. httpd -t
  75. systemctl restart httpd

利用rewrite实现强制用户通过https访问服务

  1. RewriteEngine on
  2. RewriteCond %{SERVER_PORT} !^443$
  3. RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]