標籤:
https是一個安全的訪問方式,資料在傳輸過程中是加密的,https基於ssl。
一、安裝apache和ssl模組
1、安裝apache
#yum install httpd
2、安裝ssl模組
#yum install mod_ssl
重啟apache:
#service httpd restart
安裝完mod_ssl會建立一個預設的SSL認證,路徑位於/etc/pki/tls,此時可以立即通過https訪問伺服器了:
https://X.X.X.X/
如果不使用預設的認證,也可以使用openssl手動建立認證。
二、使用openssl手動建立認證
1、安裝openssl
#yum install openssl
2、產生伺服器私密金鑰
#cd /etc/pki/tls
#openssl genrsa -out server.key 1024
注意:server.key是私密金鑰。
3、用私密金鑰server.key檔案產生認證請求檔案csr
#openssl req -new -key server.key -out server.csr
註:server.csr是認證請求檔案。
此步驟需要輸入一些認證資訊:
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:ccc
Organizational Unit Name (eg, section) []:bbb
Common Name (eg, your name or your server‘s hostname) []:www.test.com
Email Address []:[email protected]
輸入國家、省份、城市、公司、部門、姓名或伺服器名、電子郵箱,隨後會要求輸入一個challengepassword(密碼),無需輸入,後面一律直接斷行符號即可。
4、產生數位簽章crt檔案(認證檔案)
#openssl x509 -days 365 -req -in server.csr -signkey server.key -outserver.crt
用私密金鑰簽署憑證請求檔案,認證的申請機構和頒發機構都是自己。
5、編輯apache的ssl設定檔
vim/etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.d/ssl.conf檔案配置具體如下:
<VirtualHost _default_:443>
DocumentRoot "/var/www/https" //設定網頁存放目錄
ServerName *:443 //伺服器的連接埠
DirectoryIndex index.html index.html.var //首頁名稱
SSLEngine on
SSLCertificateFile /etc/pki/tls/server.crt //認證
SSLCertificateKeyFile /etc/pki/tls/server.key //私密金鑰
</VirtualHost>
6、重啟apache
#servicehttpd restart
訪問https://ip/,就能看到認證資訊了。
由於不是第三方根授權頒發的認證,而是自己頒發的認證,所以瀏覽器會提示安全性憑證不受信任。
!!!注意:首頁index.html的檔案許可權為755,否則將會出現如上提示:
Forbidden
Youdon‘t have permission to access /main.html on this server.
解決方案:修改首頁index.html讀寫權限。
#Chmod755 index.html
關於openssl指令的補充說明:
#openssl [操作] -out filename [bits]
參數說明:
[操作] 主要的操作有如下兩個:
genrsa,建立RSA加密的Public key
req,建立憑證要求檔案或者是憑證檔案
-out ,後面加上輸出的檔案名稱,就是那把key name
bits,用在genrsa加密的公開金鑰的長度
-x509,X.509,CertificateData Management. 一種驗證的管理方式
例:建立一支長度為1024bits的Public Key,注意檔案名稱。
#openssl genrsa -out Server.key 1024
產生認證請求命令:
#Openssl req -new -key file.key -out file.csr -config /path/to/openssl.cnf
-config:指定openssl的設定檔路徑,不指定時,預設會訪問Unix格式的預設路徑:/usr/local/ssl/openssl.cnf。
例:#openssl req -new -key server.key -outserver.csr
Centos伺服器下安裝配置SSL