Certbot
Https生成證書后需要CA認證帆啃。未認證的搭建好后,瀏覽器會首先出現(xiàn)此網(wǎng)站不安全提示,選擇繼續(xù)訪問后才可訪問解寝。https://github.com/certbot/certbot 提供了免費認證功能,有效期90天觅闽。但之后重新生成證書就可以繼續(xù)使用帝雇。
未認證的證書
一、提前確保openssl包存在
二蛉拙、生成證書
2.1尸闸、在/etc/nginx下新建了一個ssl文件夾用于保存證書等文件
#????cd /etc/nginx
#????mkdir ssl
#????cd ssl???
2.2、創(chuàng)建服務(wù)器證書密鑰文件 server.key
#????openssl genrsa -des3 -out server.key 1024
### 輸入密碼孕锄,確認密碼吮廉,自己隨便定義,但是要記住畸肆,后面會用到宦芦。??
2.3、創(chuàng)建服務(wù)器證書的申請文件 server.csr
#????openssl req -new -key server.key -out server.csr
輸出內(nèi)容為:
????Enter pass phrase for root.key: ← 輸入前面創(chuàng)建的密碼?
????Country Name (2 letter code) [AU]:CN ← 國家代號轴脐,中國輸入CN?
????State or Province Name (full name) [Some-State]:BeiJing ← 省的全名调卑,拼音?
????Locality Name (eg, city) []:BeiJing ← 市的全名,拼音?
????Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名?
????Organizational Unit Name (eg, section) []: ← 可以不輸入?
????Common Name (eg, YOUR name) []: ← 此時不輸入?
????Email Address []:admin@mycompany.com ← 電子郵箱大咱,可隨意填
????Please enter the following ‘extra’ attributes?
????to be sent with your certificate request?
????A challenge password []: ← 可以不輸入?
????An optional company name []: ← 可以不輸入
2.4恬涧、備份一份服務(wù)器密鑰文件
#????cp server.key?server.key.org
2.5、去除文件口令
#????openssl rsa -in?server.key.org?-out server.key
2.6碴巾、生成證書文件server.crt
#????openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
三溯捆、配置nginx 并重啟
使用免費認證證書
一、首先準備自己的域名假設(shè)是 www.test.com
二厦瓢、安裝certbot
#????cd /opt
#????git clone https://github.com/certbot/certbot.git
三提揍、解決./well-known/acme-challenge
3.1、使用webroot方式生成證書的時候旷痕,會自動在服務(wù)的根目錄下生成一個臨時文件然后訪問驗證碳锈,所以在nginx先加入配置 并重啟
#????cd /etc/nginx
#????vim /etc/nginx/sites-enabled/default
?### 在默認配置的location前面加入這兩個過濾??
四、生成證書文件
#????cd /opt/certbot
#????./certbot-auto certonly --email youremail@xxx.com --agree-tos --no-eff-email --webroot -w /var/www/html -d www.test.com?
### ?上面命令中欺抗,自行用你的郵箱替換youremail@xxx.com售碳,你的域名替換www.test.com。命令后面可以繼續(xù)-d 域名绞呈,以配置多個域名贸人。??
?### 期間會有問yes/no等的,y就行佃声。生成成功后會告訴你生成的證書在 /etc/letsencrypt/live/www.test.com/下艺智。??
五、配置nginx 并重啟
涂白的地方就是 www.test.com圾亏,也就是你的域名十拣。上面我多加了一個80端口的server封拧,就是用于過濾http直接跳轉(zhuǎn)到https用的。
我的虛擬機簡約配置【未認證的證書】(80/433端口均可訪問)
server {
? ? ? ? ? ? listen 80;
? ? ? ? ? ? ?# 比起默認的80 使用了443 默認 是ssl方式? 多出default之后的ssl
? ? ? ? ? ? # default 可省略
? ? ? ? ? ? ?listen? 443 default ssl; #監(jiān)聽端口? ? ? ? ? ? ?
? ? ? ? ? ? ?# ssl on; # 開啟? 如果把ssl on夭问;這行去掉泽西,ssl寫在443端口后面。這樣http和https的鏈接都可以用
? ? ? ? ? ? ssl_certificate? ? ? ? ?/etc/nginx/ssl_cert/vm.self.com.crt; # 證書(公鑰.發(fā)送到客戶端的)
? ? ? ? ? ? ssl_certificate_key? ? ?/etc/nginx/ssl_cert/vm.self.com.key; # 私鑰
? ? ? ? ? ? ssl_session_timeout? ? ?5m; # 會話超時時間
? ? ? ? ? ? ssl_ciphers? ? ?ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # 加密算法
? ? ? ? ? ? ssl_protocols? ?TLSv1 TLSv1.1 TLSv1.2; # SSL協(xié)議
? ? ? ? ? ? ssl_prefer_server_ciphers on;
? ? ? ? ? ? ?
? ? ? ? ? ? ?server_name? vm.self.com; # 綁定域名
? ? ? ? ? ? ?
? ? ? ? ? ? ?error_log? /etc/nginx/logs/443-error.log notice;
? ? ? ? ? ? access_log? /etc/nginx/logs/443-access.log main;
? ? ? ? ? ? ?
? ? ? ? ? ? ?location / {
? ? ? ? ? ? ? ? ? ? ? ? ? proxy_redirect off; # 禁止跳轉(zhuǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? proxy_pass http://vm.self.com:8001; # 代理
?? ? ? ? ? ? }
}
錯誤日志【no "ssl_certificate" is defined】
### 如果錯誤日志出現(xiàn)服務(wù)器中沒有定義ssl證書參數(shù)【no "ssl_certificate" is defined】缰趋,建議查看下自己的NGINX是否真的重啟了捧杉,我遇到的問題是并沒有真正重啟,nginx -s reload 命令并沒有生效導(dǎo)致參數(shù)沒有定義錯誤
??#????netstat -tpln |grep 80 ???????????????? ### 查看80端口是否存在
#????netstat -tpln |grep 443? ? ? ? ? ? ???? ### 查看80端口是否存在
#????nginx -s stop? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ### 若沒有443端口存在秘血,則關(guān)閉NGINX
#? ? nginx?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ### 重新啟動NGINX重試
Linux防火墻如果已開啟味抖,需確保打開【443/80】等端口,未開啟防火墻無需配置端口開放
centos7防火墻常用操作 :http://www.reibang.com/p/e29add2c7a88
參考
搭建無認證 httpshttps://www.cnblogs.com/jingxiaoniu/p/6745254.html
解決生成證書時訪問臨時文件而不得的問題https://segmentfault.com/a/1190000005797776
還是letsencrypt時的用法https://blog.csdn.net/rickiyeat/article/details/53783605
http跳轉(zhuǎn)到https的配置https://www.cnblogs.com/kevingrace/p/6187072.html