準備
- 已購買已備案的域名
- acme.sh 是一個自動申請 https 證書的腳本,使用方便,功能也非常強大。
安裝:curl https://get.acme.sh | sh
或
wget -O - https://get.acme.sh | sh
這樣你已經把 acme.sh 這個小工具安裝到你本地的
~/.acme.sh/
中了,而不會在你係統的其它地方裝些亂七八糟的東西。
生成證書
這篇文章後面用到的一鍵腳本申請Let's Encrypt泛域名SSL證書,是利用第三方域名DNS API接口快速申請的,這樣。無論我們Linux VPS、服務器使用的何種WEB環境都可以快速申請。
根據腳本的介紹,提供且支持我們常見的第三方DNS,包括DNSPOD、CLOUDXNS、阿里雲DNS、GoDaddy、CLOUDFLARE、PowerDNS、Linode 等40多家DNS服務商。如果我們域名有用哪家的DNS,然後到對應的商家申請API。查看支持的商家:https://github.com/acmesh-official/acme.sh/wiki/dnsapi
阿里雲購買域名的朋友可以走這個極速通道,因爲阿里雲有接口可以直接操作域名控制檯,這個接口已經被整合到了 acme.sh
這個工具裏面。只要設置一下 Ali_Key 和 Ali_Secret,獲取入口:https://ak-console.aliyun.com/#/accesskey
- 在你的命令行中執行如下命令:
export Ali_Key="換成你的 AccessKey ID" export Ali_Secret="換成你的 Access Key Secret"
- 開始申請證書
acme.sh --issue --dns dns_ali -d im20.life -d '*.im20.life'
參數解釋:
acme.sh :表示使用你剛安裝好的acme.sh
--issue :申請證書
--dns dns_ali:使用阿里雲的 dns 服務,在阿里雲買的域名,在沒有修改默認 dns 的前提下,都可以使用這個參數來申請 https 證書。
-d im20.life:-d
表示 domain,後面跟你要申請域名。
-d '*.im20.life':這裏的-d 與上方一樣,-d 參數可以帶多個,這裏的'*.im20.life'
中的 * 表示泛域名,只要申請了這個證書像(www.im20.life, m.im20.life ...)這類的二級域名都可以使用此證書來實現 https。注意 baidu.com
這個域名不在這條規則裏,所以上面又加了一條-d im20.life
,這樣你的主域名、二級子域名均可以使用此證書。
證書生成成功後,默認保存在 .acme.sh/你的頂級域名
中。如下圖所示即爲成功。
配置Nginx
1、移動證書到/etc/nginx/cert文件夾,若無該文件夾,自行創建。
cp ~/.acme.sh/im20.life/fullchain.cer /etc/nginx/cert/fullchain.cer
cp ~/.acme.sh/im20.life/im20.life.key /etc/nginx/cert/im20.key
2、新建 ssl-params.conf
並把它放到 Nginx 的 snippets
目錄中。
# /etc/nginx/snippets/ssl-params.conf
server_tokens off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 60m;
ssl_session_tickets on;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
ssl_prefer_server_ciphers on;
# 證書路徑 絕對地址
ssl_certificate /etc/nginx/cert/fullchain.cer;
ssl_certificate_key /etc/nginx/cert/im20.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
add_header Strict-Transport-Security "max-age=31536000;includeSubDomains;preload";
add_header X-Frame-Options deny;
add_header X-Content-Type-Options nosniff;
add_header x-xss-protection "1; mode=block";
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https:; connect-src 'self' https:; img-src 'self' data: https: blob:; style-src 'unsafe-inline' https:; font-src https:";
3、接下來在 Nginx 主配置文件中開啓 SSL
支持
# /etc/nginx/nginx.conf
http {
....
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
4、配置虛擬主機
# /etc/nginx/conf.d/im20.life.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name im20.life www.im20.life;
return 301 https://$server_name$request_uri;
}
server {
# 注意我們設置該站點爲默認站點,並移除了 nginx 默認的 default 配置
listen 443 ssl http2 fastopen=3 reuseport default_server;
listen [::]:443 ssl http2 fastopen=3 reuseport default_server;
server_name www.im20.life im20.life;
# 引入 SSL 及 PHP 配置
include snippets/fastcgi-php.conf;
include snippets/ssl-params.conf;
root /home/www/websites/im20.life/public;
access_log /home/www/websites/im20.life/storage/logs/nginx-access.log;
error_log /home/www/websites/im20.life/storage/logs/nginx-error.log error; index index.php;
# 當訪問域名是不 im20.life 強制跳轉到 https://im20.life
if ($host != 'im20.life' ) {
rewrite ^/(.*)$ https://im20.life/$1 permanent;
}
}
二級域名blog.im20.life的配置
# /etc/nginx/conf.d/blog.im20.life.conf
server {
listen 80;
listen [::]:80;
server_name blog.im20.life;
return 301 https://$server_name$request_uri;
}
server {
# 如果多個域名配置在同一主機,這裏只需要監聽到 433 就可以了,
# 不需要再添加 ssl http2 fastopen=3 reuseport default_server 之類的了
listen 443;
listen [::]:443;
root /home/www/websites/blog.im20.life/public-admin;
access_log /home/www/websites/blog.im20.life/storage/logs/nginx-access.log;
error_log /home/www/websites/blog.im20.life/storage/logs/nginx-error.log error;
server_name blog.im20.life;
index index.php;
client_max_body_size 20M;
include snippets/fastcgi-php.conf;
include snippets/ssl-params.conf;
if ($host != 'blog.im20.life' ) {
rewrite ^/(.*)$ https://blog.im20.life/$1 permanent;
}
}
5、虛擬主機配置完成後重啓nginx即可
service nginx restart