certbot配置证书

之前配置证书一直用https://github.com/acmesh-official/acme.sh,网络原因生成时好时坏;换成Certbot后简单多了

contos7下安装Certbot

certbot官网:https://certbot.eff.org/instructions

先安装snapd

官网https://snapcraft.io/docs/installing-snap-on-centos

1
2
3
4
sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

更新snapd

1
2
sudo snap install core;
sudo snap refresh core

安装Certbot

1
2
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

nginx证书

会自动找到conf所有域名并设置好证书

1
sudo certbot --nginx

配置免费ssl证书

安装acme.sh

官方安装

1
curl  https://get.acme.sh | sh -s email=my@example.com

安装之后会在目录下生成个.acme.sh目录

资源在github,经常下载失败,手动克隆仓库安装

https://github.com/acmesh-official/acme.sh同步到gitee上,再克隆下来,执行下面命令

1
2
3
4
git https://gitee.com/xxx/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com
alias acme.sh=~/.acme.sh/acme.sh

生成证书

1
acme.sh  --issue -d www.example.com  --webroot  /home/wwwroot/example.com/

如果80端口只配置了一个站点,可以这样

1
2
3
4
# apache
acme.sh --issue -d www.example.com --apache
# nginx
acme.sh --issue -d www.example.com --nginx

安装证书

官方例子nginx

1
2
3
4
5
acme.sh --install-cert -d www.example.com \
--cert-file /path/to/certfile/in/apache/cert.pem \
--key-file /path/to/keyfile/in/apache/key.pem \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd "service apache2 force-reload"

官方例子apache

1
2
3
4
acme.sh --install-cert -d www.example.com \
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "service nginx force-reload"

手动安装

拷贝到指定的目录

1
2
cp  ~/.acme.sh/www.example.com/fullchain.cer /etc/nginx/ssl/www.example.com.cer
cp ~/.acme.sh/www.example.com/www.example.com.key /etc/nginx/ssl/www.example.com.key

修改nginx配置/etc/nginx/conf.d/www.example.com.conf

  • 启用https
1
2
listen 443 ssl;
listen [::]:443 ssl;
  • 证书
1
2
ssl_certificate /etc/nginx/ssl/www.example.com.cer;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
  • http跳转https
1
2
3
4
5
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}