#更新软件包
sudo apt-get update

#提前默认设置,可以跳过安装过程中的输入选择
export TZ=Asia/Shanghai

#安装Certbot
sudo apt-get install certbot -y

#检验是否安装成功
certbot --version

#生成SSL证书
###--register-unsafely-without-email,不提供email地址
###--agree-tos,跳过提示并默认服务条款
certbot certonly --webroot -w <web根目录> -d <网址.com> -d <www.网址.com> --register-unsafely-without-email --agree-tos

#示例
certbot certonly --webroot -w /var/www/html -d runtuchigua.cn -d www.runtuchigua.cn --register-unsafely-without-email --agree-tos

#若创建证书成功,可以在/etc/letsencrypt/live/目录下找到证书

#设置定时任务,定时更新ssl证书,-q以静默模式执行证书更新,--no-random-sleep-on-renew 参数用于在证书更新时禁用随机休眠,这可以确保在证书更新时不会出现不必要的延迟。
certbot -q renew --no-random-sleep-on-renew

#其实生成SS证书指令会自行设置定时任务,可以在/etc/cron.d/目录下看到定时任务文件,但后续测试发现,定时任务是创建了,却没有执行。
#为了确保证书定时更新,最好自建定时任务,步骤放在文章最后。


#查看证书到期时间
sudo certbot certificates

#安装证书
certbot install

#更新证书
certbot renew
#测试自动续订是否正常
certbot renew --dry-run

#删除证书
certbot delete
#避免交互提问,直接删除指定证书
certbot delete --non-interactive --cert-name

#安装指定的插件
certbot --install-only

#为特定的 Web 服务器配置获取证书
certbot --apache
certbot --nginx

#验证配置文件
certbot --apache --dry-run
certbot --nginx --dry-run
#以Apache为例

###创建证书链和私钥文件的软链接
ln -s /etc/letsencrypt/live/runtuchigua.cn/cert.pem /etc/apache2/ssl/cert.crt
ln -s /etc/letsencrypt/live/runtuchigua.cn/privkey.pem /etc/apache2/ssl/private.key
ln -s /etc/letsencrypt/live/runtuchigua.cn/chain.pem /etc/apache2/ssl/chain.pem

###执行以下命令,启用SSL模块
sudo a2enmod ssl

###编辑default-ssl.conf文件
vim /etc/apache2/sites-available/default-ssl.conf

###内容如下
ServerName example.com  #请将example.com替换为您证书绑定的域名。部分服务器,没有该配置参数,您需要手动添加。
SSLCertificateFile /etc/apache2/ssl/domain_name_public.crt  #证书文件路径。请替换为实际证书文件路径。
SSLCertificateKeyFile /etc/apache2/ssl/domain_name.key   #证书私钥文件路径。请替换为实际证书私钥文件路径。
SSLCertificateChainFile /etc/apache2/ssl/domain_name_chain.crt  #证书链文件路径。请替换为实际证书链文件路径。

###执行以下命令,将default-ssl.conf映射至/etc/apache2/sites-enabled目录,实现两者之间的自动关联
sudo ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/001-ssl.conf

###执行以下命令,重新加载Apache2配置文件
sudo /etc/init.d/apache2 force-reload

###执行以下命令,重启Apache2服务
sudo /etc/init.d/apache2 restart
#自建定时任务

##首先确认是否安装了cron

##其次确认cron是否开启
sudo service cron status  #查看
sudo service cron start   #开启

##创建定时任务
crontab -e

##添加如下内容
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

##也可以简单些
0 */12 * * * test -x /usr/bin/certbot -a \! -d /run/systemd/system && certbot -q renew

##保存退出

##查看定时任务
crontab -l

,