Peter 工程日誌

如何在Apache2上新增SSL憑證

通常我們在使用Apache時都是走http的協定,可是有的時候需要傳輸敏感的資料時候,使用http都是明文的傳輸協定,需要使用加密方式傳輸資料到後端,因此我們會使用https來作為此目的選擇。所以我們通常有兩種方式,第一種,自己簽一個,不過這種方式瀏覽器不認識,因此我們通常會走第二種方式:買憑證,購買憑證的意思是,把在Server上產生的csr送到可以產生具有公信的憑證中心,產生對應的root.crt和網域名稱的crt,兩個憑證檔案。網路上除了有需要購買憑證方式之外,例如像是COMODO,RapidSSL等,也有免費的憑證,一年一次申請,像是startSSL,沃通SSL等都是。接下來的步驟則是教導該如何產生一個有公信力的憑證,我們以沃通SSL為例

第一步:註冊一個沃通帳號,下面是註冊網站

沃通

第二步:申請一個一年免費憑證,可以一直申請,不過只能維持一年。

免費申請

第三步:填寫請求SSL憑證表單,如下圖,依照要求填寫網域名稱,憑證的語言,還有使用的演算法,有SHA1和SHA2可以選擇。如果沒登入下面會出現要求登入的欄位就像下面這張圖一樣。

可以選擇證書要使用英文還是簡體中文,以及憑證可以使用年限,可以選擇一年,二年以及三年。不過一年以上就需要付錢從1.99美金,三年要3.98美金,所以不想花錢就選一年吧XD

這邊以英文網站為主。

 

 

 

 

 

申請完成之後,就會出現一個需要貼上csr的地方,選擇自行貼上,這部份要從自己server上的Apache2產生出來。所以我們到server產生這個csr檔。這邊以Linux Ubuntu為例

連線到server,輸入下面指令產生private key。其中www.yourdomain-example.com這是自己的網域名稱,副檔名是key

[注意]產生出來的key會需要填passphrase,這個需要記住,日後產生csr還是要設定Apache都會需要用到。


openssl genrsa -des3 -out www.yourdomain-example.com.key 2048

第四步:要使用剛剛產生的private key產生一個csr檔,拿來送給憑證供應商,廠商會根據csr產生對應具有公信的憑證。其中www.yourdomain-example.com這是自己的網域名稱,副檔名是csr

產生csr需要注意,email address, challenge password or an optional company name上述這些都是留空白,不填。

輸入下面指令產生csr檔。


openssl req -new -key www.yourdomain-example.com.key -out www.yourdomain-example.com.csr

第五步:有需要的話,記得備份自己的private key,避免遺失,因為往後憑證都需要靠此來驗證,尤其是自己使用VPS或是實體server架設尤為重要,若是使用shared hosting虛擬主機,可以不必擔心,因為會儲存在cPanel上,當然也可以從上面下載回來。

第六步:接續第三步,把產生的csr檔貼到沃通,要選自己提交csr,點選下圖中的Submit CSR

 

接著會彈跳出一個視窗,有兩個選項,一個是快速產生,一個是自己手動貼上csr,我們自己有產生csr,因此選擇第二項,把剛剛產生的csr貼上去。按下送出

如果想要確定csr所使用的演算法,可以使用Check CSR按鈕,右邊會顯示相關的資訊。

 

 

第七步:送出之後,就會開始處理,並產生憑證檔案。會提示說明,下載之後此壓縮檔就會刪除,會彈跳一個視窗作提示,在這同時,也會發送一份信件到填寫的信箱中,裡面有相關發送憑證的資訊。

 

注意視窗,下載後,壓縮檔自動刪除。

 

信箱通知憑證

第八步:打開下載的壓縮檔,下面還有多個壓縮檔,裡面包含,不一樣的server壓縮檔,我們選擇我們需要的,我們需要for Apache.zip這個壓縮檔。

壓縮檔內有兩個檔案,第一個檔案root_bundle.crt是跟憑證,也就是CA憑證中心的檔案,第二個檔案是網域上的憑證。

依照這兩種區分方式,我們接著可以來設定000-default.conf設定檔。

在server設定檔上,如下圖所示,default-ssl.conf定義了https相關的設定。要把裡面內容編輯好之後貼到000-default.conf裡面

如果還沒有啟動SSL模組,則需要輸入下面指令來啟動。


sudo a2enmod ssl

 

000-default.conf大致上如下面編輯。(從<IfModule mod_ssl.c>這一行開始才是設定SSL地方)


<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<Directory "/var/www/html">
AllowOverride All
</Directory>
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on

# A self-signed (snakeoil) certificate can be created by installing
# the ssl-cert package. See
# /usr/share/doc/apache2/README.Debian.gz for more info.
# If both key and certificate are stored in the same file, only the
# SSLCertificateFile directive is needed.
SSLCertificateFile /etc/apache2/ssl-certs/mywebservice_info_ee.crt
SSLCertificateKeyFile /etc/apache2/ssl-certs/mywebservice.info.key

SSLCACertificateFile /etc/apache2/ssl-certs/intermidiate_ca2015.crt

# Server Certificate Chain:
# Point SSLCertificateChainFile at a file containing the
# concatenation of PEM encoded CA certificates which form the
# certificate chain for the server certificate. Alternatively
# the referenced file can be the same as SSLCertificateFile
# when the CA certificates are directly appended to the server
# certificate for convinience.
#SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt

# Certificate Authority (CA):
# Set the CA certificate verification path where to find CA
# certificates for client authentication or alternatively one
# huge file containing all of them (file must be PEM encoded)
# Note: Inside SSLCACertificatePath you need hash symlinks
# to point to the certificate files. Use the provided
# Makefile to update the hash symlinks after changes.
#SSLCACertificatePath /etc/ssl/certs/
#SSLCACertificateFile /etc/apache2/ssl.crt/ca-bundle.crt

# Certificate Revocation Lists (CRL):
# Set the CA revocation path where to find CA CRLs for client
# authentication or alternatively one huge file containing all
# of them (file must be PEM encoded)
# Note: Inside SSLCARevocationPath you need hash symlinks
# to point to the certificate files. Use the provided
# Makefile to update the hash symlinks after changes.
#SSLCARevocationPath /etc/apache2/ssl.crl/
#SSLCARevocationFile /etc/apache2/ssl.crl/ca-bundle.crl

# Client Authentication (Type):
# Client certificate verification type and depth. Types are
# none, optional, require and optional_no_ca. Depth is a
# number which specifies how deeply to verify the certificate
# issuer chain before deciding the certificate is not valid.
#SSLVerifyClient require
#SSLVerifyDepth 10

# SSL Engine Options:
# Set various options for the SSL engine.
# o FakeBasicAuth:
# Translate the client X.509 into a Basic Authorisation. This means that
# the standard Auth/DBMAuth methods can be used for access control. The
# user name is the `one line' version of the client's X.509 certificate.
# Note that no password is obtained from the user. Every entry in the user
# file needs this password: `xxj31ZMTZzkVA'.
# o ExportCertData:
# This exports two additional environment variables: SSL_CLIENT_CERT and
# SSL_SERVER_CERT. These contain the PEM-encoded certificates of the
# server (always existing) and the client (only existing when client
# authentication is used). This can be used to import the certificates
# into CGI scripts.
# o StdEnvVars:
# This exports the standard SSL/TLS related `SSL_*' environment variables.
# Per default this exportation is switched off for performance reasons,
# because the extraction step is an expensive operation and is usually
# useless for serving static content. So one usually enables the
# exportation for CGI and SSI requests only.
# o OptRenegotiate:
# This enables optimized SSL connection renegotiation handling when SSL
# directives are used in per-directory context.
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>

# SSL Protocol Adjustments:
# The safe and default but still SSL/TLS standard compliant shutdown
# approach is that mod_ssl sends the close notify alert but doesn't wait for
# the close notify alert from client. When you need a different shutdown
# approach you can use one of the following variables:
# o ssl-unclean-shutdown:
# This forces an unclean shutdown when the connection is closed, i.e. no
# SSL close notify alert is send or allowed to received. This violates
# the SSL/TLS standard but is needed for some brain-dead browsers. Use
# this when you receive I/O errors because of the standard approach where
# mod_ssl sends the close notify alert.
# o ssl-accurate-shutdown:
# This forces an accurate shutdown when the connection is closed, i.e. a
# SSL close notify alert is send and mod_ssl waits for the close notify
# alert of the client. This is 100% SSL/TLS standard compliant, but in
# practice often causes hanging connections with brain-dead browsers. Use
# this only for browsers where you know that their SSL implementation
# works correctly.
# Notice: Most problems of broken clients are also related to the HTTP
# keep-alive facility, so you usually additionally want to disable
# keep-alive for those clients, too. Use variable "nokeepalive" for this.
# Similarly, one has to force some clients to use HTTP/1.0 to workaround
# their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
# "force-response-1.0" for this.
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

</VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

其中上面設定檔,找到這四行(65~68行)是重要的。

SSLCertificateFile指的是網域憑證,也就是for apache.zip中的第二個檔案,我重新命名成mywebservice_info_ee.crt

SSLCertificateKeyFile是在產生csr檔之前所產生的private key檔。

SSLCACertificateFile指的是CA憑證中心根的憑證檔,或稱做網域憑證與憑證中心溝通的中介憑證,也就是for apache.zip檔中第一個檔案,root_bundle.crt,我重新命名成intermidiate_ca2015.crt


SSLCertificateFile /etc/apache2/ssl-certs/mywebservice_info_ee.crt
SSLCertificateKeyFile /etc/apache2/ssl-certs/mywebservice.info.key

SSLCACertificateFile /etc/apache2/ssl-certs/intermidiate_ca2015.crt

完成之後,輸入下面指令重新啟動apache服務:


sudo service apache2 restart

接著會出現下圖:提是要輸入私鑰當初設定的passphrase,這裡一定要輸入正確,否則的話則無法啟動Apache服務。

 

接著去輸入網址,看看有沒有設定成功,如圖,設定成功了。

憑證相關資訊

 

點選憑證資訊

[後記]

無法啟動服務可能原因大致上有兩種:

1.  私鑰的passphrase輸入與私鑰定義的不同。

2.  設定憑證crt檔案對應錯誤,需檢查檔案是哪一種類型,是網域名稱憑證,還是中介憑證中心的crt檔等。

之後約有兩篇會討論如何使用cPanel管理SSL站點與安裝與設定付費憑證:COMODO基本防護憑證,這個設定會比Apache好設定,不過缺點是這侷限於有購買cPanel服務與使用共享主機(shared hosting)方案才有的服務了。

Exit mobile version