certbot 快速配置 let’s encrypt https
请注意,本教程写于 2017 年,教程中的许多内容都均已过时
安装cerbot
以下内容部分来自cerbot官网。
首先我们下载cerbot
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
运行
./certbot-auto
运行之后你可能会看到一个报错
Failed to find apache2ctl in PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Certbot doesn’t know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run “certbot-auto certonly” to do so. You’ll need to manually configure your web server to use the resulting certificate.
不用担心,我们可以继续运行它。
certbot-auto is looking for apache despite following instructions for nginx
创建证书
现在需要为你的服务器创建一个let’s encrypt签发的证书,证书可以用于多个域名,-w
后输入你的网站目录,-d
输入网站目录所绑定的域名,同一目录绑定了多个域名的话,使用连续的 -d
加空格隔开,例如,我有2个网站目录,分别绑定了 www.网站.com
, 网站.com
和www.网站2.com
, 网站2.com
则运行:
./certbot-auto certonly –webroot -w /var/www/网站目录1 -d 网址.com -d www.网址.com -w /var/www/网站目录2 -d www.网址2.com -d 网址2.com
然后按照提示进行,便会生成相应证书在相应的目录下。
设置自动更新证书
let’s encrypt签发的免费证书有效期为3个月,而使用以下命令便能自动更新证书:
./certbot-auto renew –dry-run
有关续订的更多详细信息和选项,请参阅完整文档。
More detailed information and options about renewal can be found in the full documentation.
配置nginx
现在在你的 /etc/letsencrypt/live
目录下应该可以看到以你的网站目录命名的文件夹,里面含有cerbot生产的key。
查阅README,就可以看到相关说明如下:
This directory contains your keys and certificates.
privkey.pem : the private key for your certificate.
fullchain.pem: the certificate file used in most server software.
chain.pem : used for OCSP stapling in Nginx >=1.3.7.
cert.pem : will break many server configurations, and should not be used without reading further documentation (see link below).
We recommend not moving these files. For more information, see the Certbot
User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates.
接下来需要将私钥和证书写入到nginx的配置文件中,到/etc/nginx 你nginx的想要网站配置文件中加入以下配置:
server {
listen 443 ssl;
root /var/www/你的网站目录;
index index.php index.html index.htm;
server_name www.你的域名.com 你的域名.com
;
ssl_certificate /etc/letsencrypt/live/相应目录/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/相应目录/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/你的网站目录;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存,
重启nginx:
service nginx restart
使用https访问你的网站
使用 https://www.你的域名.com 访问你的网站。
大功告成!