ssl對于用戶和web服務(wù)器之間的安全通信很有用。證書在公共線路上傳輸時對數(shù)據(jù)進行加密,這樣它就不會受到黑客的攻擊。自簽名證書是免費使用的,但不在生產(chǎn)環(huán)境中使用,例如使用信用卡、paypal信息的機密數(shù)據(jù)。本篇文章將介紹關(guān)于在linux系統(tǒng)上的apache服務(wù)器中創(chuàng)建和安裝自簽名證書。
步驟1:安裝mod_ssl包
要設(shè)置SSL證書,請確保在系統(tǒng)上安裝了mod_ssl。如果尚未安裝,需要使用以下命令進行安裝。另外,安裝openssl包來創(chuàng)建證書。
$?sudo?apt-get?install?openssl??????????#?Debian?based?systems? $?sudo?yum?install?mod_ssl?openssl??????#?Redhat?/?CentOS?systems? $?sudo?dnf?install?mod_ssl?openssl??????#?Fedora?22+?systems
步驟2:創(chuàng)建自簽名證書
安裝mod_ssl和openssl后,使用以下命令為你的域創(chuàng)建一個自簽名證書。
$?sudo?mkdir?-p?/etc/pki/tls/certs $?sudo?cd?/etc/pki/tls/certs
現(xiàn)在創(chuàng)建SSL證書
$?sudo?openssl?req?-x509?-nodes?-newkey?rsa:2048?-keyout?example.com.key?-out?example.com.crt
輸出
Generating?a?2048?bit?RSA?private?key ....................................+++ ...................................+++ writing?new?private?key?to?'example.com.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)?[XX]:?IN State?or?Province?Name?(full?name)?[]:?Delhi Locality?Name?(eg,?city)?[Default?City]:?Delhi Organization?Name?(eg,?company)?[Default?Company?Ltd]:?TecAdmin Organizational?Unit?Name?(eg,?section)?[]:?blog Common?Name?(eg,?your?name?or?your?server's?hostname)?[]:?www.example.com Email?Address?[]:?admin@example.com
上面的命令將在當前目錄中創(chuàng)建一個ssl密鑰文件example.com.key和一個證書文件example.com.crt。
步驟3:在apache中安裝自簽名證書
現(xiàn)在擁有了自簽名SSL證書和密鑰文件。接下來編輯Apache SSL配置文件并按照以下指令進行編輯/更新。
Apache虛擬主機配置:
<virtualhost> ????ServerAdmin?admin@example.com ????ServerName?www.example.com ????ServerAlias?example.com ????DocumentRoot?/var/www/html ????SSLEngine?on ????SSLCertificateFile?/etc/pki/tls/certs/example.com.crt ????SSLCertificateKeyFile?/etc/pki/tls/certs/example.com.key </virtualhost>
步驟4:重啟Apache
如果上面的命令沒有顯示任何錯誤,請重新啟動Apache服務(wù)。
$?sudo?systemctl?restart?apache2???????#?Debian?based?systems??? $?sudo?systemctl?restart?httpd?????????#?Redhat?based?systems
步驟5:使用https測試網(wǎng)站
最后,使用https在你的Web瀏覽器中打開你的站點。它需要打開端口443才能使用HTTPS訪問站點。
?https://www.example.com
當我們使用自簽名證書時,你將在瀏覽器中收到一條警告消息,忽略此消息就可以了。