2016-08-09
參考
https://wiki.centos.org/zh-tw/HowTos/Https
CentOS 7-1511 (7.2) 的
Apache Web Server ( httpd ) 是 2.4.x 系列
OpenSSL 是 1.0.1x 系列
取得所需的軟件
產生一張自我簽署的憑證
設置虛擬主機
設置防火牆
這個指引會解釋如何設置一個支援 https 的網站。這個教程使用一個自我簽署的金鑰,因此它適用於個人網站或作測試用途。這個指引並未經修訂,因此請自行承擔風險,並進行備份!
1. 安裝套件
yum -y install httpd mod_ssl openssl
# 產生私鑰
openssl genrsa -out ca.key 2048
[root@localhost ~]# openssl genrsa -out ca.key 2048
Generating RSA private key, 2048 bit long modulus
..+++
..................................+++
e is 65537 (0x10001)
[root@localhost ~]#
openssl req -new -key ca.key -out ca.csr
[root@localhost ~]# openssl req -new -key ca.key -out ca.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]: State or Province Name (full name) []: Locality Name (eg, city) [Default City]: Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: [root@localhost ~]# |
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
[root@localhost ~]# openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd
Getting Private key
[root@localhost ~]#
cp ca.crt /etc/pki/tls/certs
cp ca.key /etc/pki/tls/private/ca.key
cp ca.csr /etc/pki/tls/private/ca.csr
警告:如果你採用 SELinux,請確保你複製這些檔案而不是遷移它們。否則 Apache 將會投訴關於違漏了的憑證檔,因為它無法讀取這些擁有錯誤 SELinux 脈絡的憑證檔。
假如你遷移了這些檔案而不是複製它們,你可以用以下的指命來矯正這些檔案的 SELinux 脈絡,因為 /etc/pki/* 的正確脈絡定義已包含在 SELinux 政策裡。
restorecon -RvF /etc/pki
[root@localhost ~]# restorecon -RvF /etc/pki
restorecon reset /etc/pki/tls/certs/localhost.crt context unconfined_u:object_r:cert_t:s0->system_u:object_r:cert_t:s0
restorecon reset /etc/pki/tls/certs/ca.crt context unconfined_u:object_r:cert_t:s0->system_u:object_r:cert_t:s0
restorecon reset /etc/pki/tls/private/localhost.key context unconfined_u:object_r:cert_t:s0->system_u:object_r:cert_t:s0
restorecon reset /etc/pki/tls/private/ca.key context unconfined_u:object_r:cert_t:s0->system_u:object_r:cert_t:s0
restorecon reset /etc/pki/tls/private/ca.csr context unconfined_u:object_r:cert_t:s0->system_u:object_r:cert_t:s0
[root@localhost ~]# vi +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf
[root@localhost ~]#
修改 /etc/httpd/conf.d/ssl.conf 檔案中憑證路徑
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
改為
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
改為
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
存檔,離開
重新啟動 httpd讓設定生效
service httpd restart
或
systemctl restart httpd
原來網頁上說用 /etc/init.d/httpd restart,這對 CentOS 7.2 無用,會產生下面錯誤
[root@localhost ~]# /etc/init.d/httpd restart
-bash: /etc/init.d/httpd: No such file or directory
3. 設置虛擬主機
一如你為 http 在連接埠 80 上設立 VirtualHost,你亦可為 https 在連接埠 443 上作樣似的設置。一個在連接埠 80 上的網站的典型 VirtualHost 有如下樣子
編輯 /etc/httpd/conf/httpd.conf
vi /etc/httpd/conf/httpd.conf
依照實際情況,增加下面格式的內容
<VirtualHost *:80>
<Directory /var/www/vhosts/yoursite.com/httpdocs>
AllowOverride All
</Directory>
DocumentRoot /var/www/vhosts/yoursite.com/httpdocs
ServerName yoursite.com
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
<Directory /var/www/vhosts/yoursite.com/httpsdocs>
AllowOverride All
</Directory>
DocumentRoot /var/www/vhosts/yoursite.com/httpsdocs
ServerName yoursite.com
</VirtualHost>
敝人在檔案最下方增加了
<VirtualHost *:80>
<Directory /var/www/html>
AllowOverride All
</Directory>
DocumentRoot /var/www/html
#ServerName yoursite.com
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
<Directory /var/www/html>
AllowOverride All
</Directory>
DocumentRoot /var/www/html
#ServerName yoursite.com
</VirtualHost>
重新啟動 Apache,讓設定生效
service httpd restart
設定 reboot 後 httpd 自動啟動
systemctl enable httpd
4. 設置防火牆
原始網頁上說用下面方法
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
/sbin/service iptables save
iptables -L -v
但根據以往經驗,CentOS 7 上用 iptables 可能有問題
[研究] CentOS 7.0 x64 的 iptables 與 firewall-cmd 防火牆
http://shaurong.blogspot.tw/2014/07/centos-70-x64.html
建議改用下面方法
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=443/tcp
systemctl restart firewalld
firewall-cmd --list-allPS:實際測試
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=443/tcp
無法合併成為
firewall-cmd --permanent --add-service=http --add-port=443/tcp
會執行失敗
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
interfaces: eno16777736
sources:
services: dhcpv6-client http ssh
ports: 443/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
[root@localhost ~]#
自簽的憑證,在使用 HTTPS 時候會有警告;如果是個人 or 公司內部使用,用免費自簽的就可以了。如果是要公開給大眾瀏覽,可以考慮花錢買憑證,避免出現一些警告訊息。
Internet Exploer 11 上測試
HTTP 連線正常
HTTPS 連線會有警告 ( 因為憑證是自簽的,不是花錢買的)
HTTPS 也可連上
Google Chrome 上測試
Mozilla FireFox 上測試
(下圖) 如果要刪除例外
(完)
相關
[研究] Apache HTTPd Web Server 2.4.6 + HTTPS (SSL) yum 安裝 (CentOS 7.2 x64)
http://shaurong.blogspot.com/2016/08/apache-httpd-web-server-246-https-ssl.html
網際網路資訊服務(英语:Internet Information Services,簡稱IIS)
https://zh.wikipedia.org/wiki/%E7%B6%B2%E9%9A%9B%E7%B6%B2%E8%B7%AF%E8%B3%87%E8%A8%8A%E6%9C%8D%E5%8B%99
有 Windows 版本和內建 IIS 版本關係
[研究] Windows 2012 R2 安裝 IIS 8.5 和 HTTP (SSL) 連線 (方法二)
http://shaurong.blogspot.com/2015/04/windows-2012-r2-iis-http-ssl.html
[研究] Windows 2012 R2 安裝 IIS 8.5 和 HTTP (SSL) 連線 (方法一)
http://shaurong.blogspot.com/2015/04/windows-2008-r2-iis-http-ssl.html
[研究] Windows 2012 安裝 IIS 8.0 和 HTTP (SSL) 連線
http://shaurong.blogspot.com/2015/04/windows-2012-iis-http-ssl.html
[研究] Windows 2008 R2 安裝 IIS 7.5 和 HTTP (SSL) 連線
http://shaurong.blogspot.com/2015/04/windows-2008-r2-iis-http-ssl.html
[研究] Windows 2003 R2 安裝 IIS 6.0 和 HTTPS (SSL) 連線
http://shaurong.blogspot.com/2015/04/windows-2003-r2-iis-https-ssl.html
[研究] Windows 10 Enterprise 1511 (x64)安裝架設IIS 10.0、建立SSL憑證、提供 HTTPS (SSL) 連線
http://shaurong.blogspot.com/2016/02/windows-10-enterprise-1511-x64iisssl.html
[研究] Windows 7 Ultimate x64安裝架設IIS 7.5、建立SSL憑證、提供 HTTPS (SSL) 連線
http://shaurong.blogspot.com/2016/02/windows-7-ultimate-x64iisssl-https-ssl.html
[研究] Windows XP Professional x86 安裝架設IIS 5.1、建立SSL憑證、提供 HTTPS (SSL) 連線
http://shaurong.blogspot.com/2016/02/windows-xp-professional-x86-iisssl.html
[研究] 在Windows XP Professional上IIS 5.1啟動SSL
http://shaurong.blogspot.com/2011/06/windows-xp-professionaliisssl.html
Internet Information Services (IIS) 10.0 Express 下載
https://www.microsoft.com/zh-TW/download/details.aspx?id=48264
支援 Windows 7/2008R2,8/2012,8.1/2012 R2,10/2016
Internet Information Services (IIS) 8.0 Express
https://www.microsoft.com/en-us/download/details.aspx?id=34679
Internet Information Services (IIS) 7 Manager
https://www.microsoft.com/en-us/download/details.aspx?id=2299
Internet Information Services (IIS) 6.0 Resource Kit
https://www.microsoft.com/en-us/download/details.aspx?id=5135
支援 Windows XP/2003
Internet Information Services (IIS) 6.0 Resource Kit Tools
https://www.microsoft.com/en-us/download/details.aspx?id=17275
Internet Information Services (IIS) 6.0 Manager for Windows XP
https://www.microsoft.com/en-us/download/details.aspx?id=15662
沒有留言:
張貼留言