1、獲取證書
1.1 openssl生成SSL證書(自行百度)
1.2 騰訊雲,阿里雲,百度雲等等都有提供免費的SSL證書
2、證書安裝
編輯Apache根目錄下 conf/httpd.conf 文件,找到#LoadModule ssl_module/mod_ssl.so 和 #Include conf/extra/httpd-ssl.conf,去掉前面的#號注釋;
編輯Apache跟目錄下 conf/extra/httpd-ssl.conf 文件,修改如下內容:
<VirtualHost *:443> DocumentRoot "/var/www/html" ServerName www.domain.com:443 SSLEngine on SSLCertificateFile /usr/local/apache/conf/2_www.domain.com_cert.crt SSLCertificateKeyFile /usr/local/apache/conf/3_www.domain.com.key SSLCertificateChainFile /usr/local/apache/conf/1_root_bundle.crt </VirtualHost>
配置完成后,重新啟動 Apache 就可以使用https://www.domain.com
來訪問了。
注:
配置文件參數 | 說明 |
SSLEngine on | 啟用SSL功能 |
SSLCertificateFile | 證書文件 |
SSLCertificateKeyFile | 私鑰文件 |
SSLCertificateChainFile | 證書鏈文件 |
3、apache配置重定向
在.htaccess文件中加入如下代碼:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
ps:如果項目使用了.htaccess隱藏了index.php(例如tp3.2),可在.htaccess文件中加入如
在.htaccess文件中加入如下代碼:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
<?php if ($_SERVER["HTTPS"] <> "on") { $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; header("Location: ".$xredir); } ?>
然后偽靜態就正常去掉index.php功能:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
配置完成后,http就可以重定向到https了。
4、遇到的問題
4.1 外網443端口被防火牆關閉
4.2 在windows Server 2012服務器上配置
時VirtualHost *:443 * 號使用域名出錯,這里建議用*/iphttpd-ssl.conf