1 頁 (共 1 頁)

10.04 Apache SSL

發表於 : 2013-05-12 18:52:53
yehlu
http://www.arthurtoday.com/2011/11/ubun ... Y9ysbXa5j4

代碼: 選擇全部

sudo apt-get install openssl
sudo a2enmod ssl
sudo mkdir /etc/apache2/ssl
openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.key
/etc/apache2/sites-enabled/000-defaul

代碼: 選擇全部

<VirtualHost _default_:443>
       SSLEngine On
       SSLCertificateFile /etc/apache2/ssl/apache.pem
       SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

<VirtualHost 8.8.8.8:80>
DocumentRoot /var/www/
ServerName www.551.com.tw
TransferLog /var/log/apache2/551.log
</VirtualHost>
/etc/apache2/sites-enabled/ports.conf

代碼: 選擇全部

#NameVirtualHost *:80
NameVirtualHost 8.8.8.8:80

ROOT CA

發表於 : 2013-05-22 20:12:59
yehlu
http://www.weithenn.org/cgi-bin/wiki.pl ... 1%E8%AD%89

SSL Certificate-自行產生 SSL 憑證

內容

前言
實作環境
安裝及設定
製作最高層認證中心 (Root CA)
步驟1.製作亂數檔
步驟2.製作最上層認證中心的 Private Key (rootca.key)
步驟3.填寫憑證申請書 (rootca.req)
步驟4.簽發憑證 (rootca.crt)
製作伺服器用的憑證
步驟1.製作伺服器用的 Private Key (weithenn.org.key)
步驟2.填寫憑證申請書 (weithenn.org.req)
步驟3.最高層認證中心簽發憑證
步驟4.修改 Apache 設定檔 (httpd.conf)
步驟5.測試 SSL 功能 (https)
參考
前言

[SSL] 採用的是 [X.509] 由上而下金字塔式的憑證制度 (Root CA >> CA >> Certificate)

在 [X.509] 中每一個合格的憑證上都會有一個簽名。最下層的憑證上,會有一個認證中心 (CA) 的簽名表示此憑證為認證中心 (CA) 檢查過確認所有者資料無誤。中間的認證中心 (CA) 上,也會有管轄它的最高層認證中心 (Root CA) 的簽名,表示最高層認證中心授權給它,可以簽發別人的憑證。

程式自己會認得幾家可靠的認證中心 (CA),碰到 [SSL] 網站時,雖然不認得伺服器的憑證 (Certificate),但只要那個憑證上有自己認得的認證中心 (CA) 簽名保證過,那個憑證就沒有問題。

一般商業型網站申請憑證大致可分為下列的步驟 (CA for Apache):

產生 .key (private key) 和 .CSR (Certificate Signing Request)
將 CSR 送至廠商並靜待傳回 .CRT (Certificate)
將 .CRT 及 .key 放到 httpd.conf 指定之路徑下後 stratssl
一般商業型網站申請憑證大致可分為下列的步驟 (CA for IIS):

產生 .key (private key) 和 .CSR (Certificate Signing Request)
將 CSR 送至廠商並靜待傳回 .CER (Certificate)
將 .CER 和 .key 合併為 .p12 並送入 IIS後 restart IIS Service
實作環境

FreeBSD 5.2-RELEASE
apache+mod_ssl-1.3.29+2.8.16 (The Apache 1.3 webserver with SSL/TLS functionality)
安裝及設定

製作最高層認證中心 (Root CA)

步驟1.製作亂數檔

請依如下步驟來產生亂數檔案

#cd /etc/ssl //切換路徑
#openssl rand -out /etc/ssl/private/.rand 1024 //製作亂數檔
#vi /etc/ssl/openssl.cnf //修改 openssl.cnf 內容如下
dir = ./demoCA //預設值
dir = /etc/ssl //修改後
步驟2.製作最上層認證中心的 Private Key (rootca.key)

請依如下步驟製作最上層認證中心的 Private Key (rootca.key)

#cd /etc/ssl //切換路徑
#openssl genrsa -des3 -out rootca.key 2048 //產生最上層的 Private Key(rootca.key)
Generating RSA private key, 2048 bit long modulus
......................................................................+++
..........+++
e is 65537 (0x10001)
Enter pass phrase for rootca.key: //輸入密碼 (Rootca 的 Private key 密碼)
Verifying - Enter pass phrase for rootca.key: //再輸入一次密碼(Private key 的密碼)
步驟3.填寫憑證申請書 (rootca.req)

請依如下步驟填寫相關資訊來產生憑證申請書 (rootca.req)

#openssl req -new -key rootca.key -out rootca.req //產生 rootca.req (憑證申請書)
Enter pass phrase for rootca.key: //輸入密碼 (Rootca 的 Private key 密碼)
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) [AU]:TW //國碼臺灣是 TW
State or Province Name (full name) [Some-State]:Taiwan R.O.C //國名臺灣填 Taiwan
Locality Name (eg, city) []:Taipei //地名
Organization Name (eg, company) [Internet Widgits Pty Ltd]:FreeBSD Personal Reserach //組織單位名稱
Organizational Unit Name (eg, section) []:FreeBSD Personal Reserach //部門名稱
Common Name (eg, YOUR name) []:Weithenn-Wang //憑證的名稱
Email Address []:weithenn@weithenn.org //申請單位的聯絡信箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:按 Enter 直接跳過 //申請書的密碼
An optional company name []:按 Enter 直接跳過 //憑證代辦公司的名稱
步驟4.簽發憑證 (rootca.crt)

因為此次實作自已是擔任 RootCA 的角色所以自己給自己簽名,而最高層認證中心 (RootCA) 最好永遠不要過期。因為 RootCA 一旦過期則所有原來它 (RootCA) 所簽發出去的憑證也都要重簽,且所有 SSL 程式也都要重新設定。因此我們將 RootCA 效期簽 7305 天 (20年)。若未設設定效期的話則預設為 30 天 (一個月)。

#openssl x509 -req -days 7305 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_ca -signkey rootca.key -in rootca.req -out rootca.crt
Signature ok
subject=/C=TW/ST=Taiwan R.O.C/L=Taipei/O=FreeBSD Personal Reserach/OU=FreeBSD Personal Reserach
/CN=Weithenn-Wang/emailAddress=weithenn@weithenn.org
Getting Private key
Enter pass phrase for rootca.key: //輸入密碼(Rootca 的 Private key 密碼)
製作伺服器用的憑證

建議伺服器的 Private Key 不要設定密碼,否則當 SSL 伺服器程式啟動的時候,Web Server 讀取憑證和 Private Key 時便會詢問一次密碼 (您必須親自在 Console 端輸入)。所以建議您若沒有 [KVM Over IP] 設定或您的機器在遠端的話請不要設定 Private Key 密碼。

步驟1.製作伺服器用的 Private Key (weithenn.org.key)

請鍵入如下指令產生伺服器用的 Private Key (weithenn.org.key)

#openssl genrsa -out weithenn.org.key 2048 //產生伺服器用的 Private key (weithenn.org.key)
Generating RSA private key, 2048 bit long modulus
......................................................+++
......+++
e is 65537 (0x10001)
步驟2.填寫憑證申請書 (weithenn.org.req)

請依如下步驟填寫相關資訊來產生憑證申請書 (weithenn.org.req)

#openssl req -new -key weithenn.org.key -out weithenn.org.req //產生伺服器憑證申請書 (weithenn.org.req)
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) [AU]:TW //國碼臺灣是 TW
State or Province Name (full name) [Some-State]:Taiwan R.O.C //國名臺灣填 Taiwan
Locality Name (eg, city) []:Taipei //地名
Organization Name (eg, company) [Internet Widgits Pty Ltd]:FreeBSD Personal Reserach //組織單位名稱
Organizational Unit Name (eg, section) []:FreeBSD Personal Reserach //部門名稱
Common Name (eg, YOUR name) []:www.weithenn.org //憑證的名稱 (通常為伺服器 FQDN)
Email Address []:weithenn@weithenn.org //申請單位的聯絡信箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 按 Enter 直接跳過 //申請書的密碼
An optional company name []: 按 Enter 直接跳過 //憑證代辦公司的名稱
步驟3.最高層認證中心簽發憑證

最高層認證中心 (RootCA) 簽發 Web 伺服器 (Apache伺服器) 3650 天 (十年) 的憑證 (RootCA >> Server)

#openssl x509 -req -days 3650 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_req -CA rootca.crt -CAkey rootca.key \
-CAserial rootca.srl -CAcreateserial -in weithenn.org.req -out weithenn.org.crt //接續上一行
Signature ok
subject=/C=TW/ST=Taiwan R.O.C/L=Taipei/O=FreeBSD Personal Research/OU=FreeBSD Personal Research
/CN=www.weithenn.org/emailAddress=weithenn@weithenn.org
Getting CA Private Key
Enter pass phrase for rootca.key: //輸入密碼 (RootCA 的 Private Key 密碼)
步驟4.修改 Apache 設定檔 (httpd.conf)

伺服器憑證產生完成之後將相關檔案路徑設定至 Apache 設定檔 (httpd.conf) 內

#vi /usr/local/etc/apache/httpd.conf //修改 Apache 設定檔內容如下
SSLCertificateFile /etc/ssl/weithenn.org.crt //指定使用的憑證
SSLCertificateKeyFile /etc/ssl/weithenn.org.key //指定使用的 Private Key
步驟5.測試 SSL 功能 (https)

完成後即可測試您的 Apache 是否具有 [HTTPs] 功能。

ex. http'''s'''://www.weithenn.org

由於此次實作為自行產生憑證也就是自已擔任最高層認證中心 (RootCA),因為它 (RootCA) 已經是最上層沒有再上層可以給它簽名了所以只好自己簽自己,憑證上的簽名是自己簽的。以下實作為自行產生 RootCA 之後簽憑證給自已的 Web Server (Apache),也因為 RootCA 是自行產生的所以一般的瀏灠器不可能認得,因此連結憑證畫面會出現一個驚嘆號而另外二個狀態則是 OK 的 (如左下圖),若您將 RootCA 匯入至瀏灠器後則憑證畫面應該三個狀態都要是打勾才正確。



參考

[ 旅舍依瑪 - 如何製作 SSL X.509 憑證?]

[SSL X.509 憑證教學]

IE 匯入 rootca.crt

發表於 : 2013-05-22 20:41:54
yehlu
選 第三方根憑證授權單位

Re: 10.04 Apache SSL

發表於 : 2013-05-23 09:06:22
yehlu

代碼: 選擇全部

openssl genrsa -out linode.551.com.tw.key 2048
openssl req -new -key linode.551.com.tw.key -out linode.551.com.tw.req
openssl x509 -req -days 3650 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_req -CA rootca.crt -CAkey rootca.key -CAserial rootca.srl -CAcreateserial -in linode.551.com.tw.req -out linode.551.com.tw.crt 

讓 http 網址自動轉址 https

發表於 : 2013-05-23 09:07:09
yehlu
http://www.bestlong.idv.tw/thread-787-1-1.html

.htaccess

代碼: 選擇全部

RewriteEngine on
RewriteBase /folder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Apahce Premature end of script headers

發表於 : 2013-05-24 09:35:26
yehlu

代碼: 選擇全部

ScriptAlias /xxx/ /var/www/xxxyyy/

Alias /xxx/ /var/www/xxxyyy/