目標(biāo) :為域名配置SSL證書幽邓,使用https開頭訪問域名 并且兼容http開頭訪問。
基礎(chǔ)環(huán)境
centos7 x64 服務(wù)器
nginx 1.14.0
tomcat8 web容器
JDK8 Java環(huán)境
可愛的免費(fèi)SSL申請(qǐng)網(wǎng)站Let's encrypt https://letsencrypt.org/
配置順序
1撤蟆、生成證書
2奕塑、配置其他支持環(huán)境
3、證書配置進(jìn)tomcat
開始
一家肯、獲取 Let’s Encrypt 免費(fèi) SSL 證書
1龄砰、下載代碼,上傳至服務(wù)器指定目錄 我放在了/var/ssl/下
生成證書需要下載letsencrypt軟件支持讨衣,我直接在本地拉取github的代碼换棚,傳到服務(wù)器,解壓后位置為/var/ssl/letsencrypt/*
# letsencrypt拉取地址反镇,下面兩個(gè)都是可以的
https://github.com/certbot/certbot 或 https://github.com/letsencrypt/letsencrypt
2固蚤、用命令獲取ssl證書
# 獲取證書, 選用 standalone 參數(shù)愿险,在此需要把 tomcat 服務(wù)器關(guān)閉(否則可能會(huì)報(bào)錯(cuò))
# -d 你的域名 例如: smartisan.com -d www.smartisan.com
$ ./letsencrypt-auto certonly --standalone --email 你的郵箱 -d smartisan.com -d www.smartisan.com
注意了F睦!辆亏!
執(zhí)行出問題的先看這里了风秤,我在執(zhí)行上面獲取證書的代碼時(shí)遇到了一些問題主要是兩個(gè)
①首先要關(guān)閉正在運(yùn)行且使用80端口的tomcat 為了保險(xiǎn)期間 我建議你關(guān)閉所有tomcat
②仔細(xì)看上面的命令 [./letsencrypt-auto] 是不是感覺很熟悉啊,沒錯(cuò)這個(gè)就是shell腳本扮叨,我這邊呢下載下來的代碼是位于/var/ssl/letsencrypt/letsencrypt-auto 這個(gè)letsencrypt-auto其實(shí)就是sh腳本 不用管他為什么不寫后綴名缤弦,這里我下的版本有個(gè)坑,就是這個(gè)shell腳本的文件編碼是win的 如果你執(zhí)行上面的命令彻磁,出現(xiàn)
-bash: ./letsencrypt-auto: /bin/sh^M: bad interpreter: No such file or directory
那么請(qǐng)你先檢查一下 這個(gè)文件的編碼
# 查看該錯(cuò)誤文件的格式(一般報(bào)錯(cuò)的文件格式是DOS)
vi letsencrypt-auto
:set ff
如果ff=dos 說明就是這里了
:set ff=unix
:wq
再運(yùn)行就好了
如果遇到下面的情況說明你獲取成功了碍沐,中間黑的地方是以自己域名命名的文件夾
這段說明里面有一個(gè)點(diǎn)要注意下,就是證書到期的日期衷蜓,如圖我的是2019年3月22日到期
可以在你的 /etc/letsencrypt/live/域名/ 目錄下看到一下幾個(gè)文件
到這一步累提,說明你已經(jīng)準(zhǔn)備好相關(guān)的證書了。
接下來就是配置tomcat和nginx了
tomcat server.xml配置成這樣
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="443" />
nginx nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_pass http://127.0.0.1:8082;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
#這里一定要配置fullchain.pem 避免配置成cert.pem 不然小程序端會(huì)報(bào)錯(cuò) 即ios端可以正常訪問 Android 使用wx.request報(bào)錯(cuò)誤ssl hand shake error
#生成的ssl下有四個(gè)pem 其中一個(gè)是key 其他三個(gè)cert是一個(gè)根證書 chain是中間證書 fullchain是這兩個(gè)的并集
ssl_certificate "/etc/letsencrypt/live/smartisan.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/smartisan.com/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
location / {
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_pass http://127.0.0.1:8082;
}
}
}