Nginx官网 https://www.nginx-cn.net/
Let's Encrypt官网 https://letsencrypt.org/zh-cn/how-it-works/
一、安装nginx
sudo yum install nginx
上面是直接安装到默认位置,如果是通过wget下载,安装在其他目录下,就需要稍微配置下。
wget https://nginx.org/download/nginx-1.19.5.tar.gz
二、将nginx软连接到全局(如果是用yum直接安装可以跳过此步骤)
将nginx执行文件软连接到/usr/local/bin (或者/usr/local/sbin也可以)
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
将conf文件夹软连接到/etc/nginx下面(注意:要确保/etc/nginx文件夹不存在,否则会导致无限递归文件夹软连接)
sudo rm -rf /etc/nginx # 若已经存在文件夹,可以先删除
sudo ln -s /usr/local/nginx/conf /etc/nginx
三、安装EPEL软件仓库
sudo yum install epel-release
本人是用阿里云服务器,所以当安装EPEL仓库的时候,会发现EPEL软件仓库已经存在,而且是epel-aliyuncs-release(应该是阿里专门弄了一个版本)。
这个时候可以直接跳过。进入下一步。
四、安装certbot
# 安装 Certbot 和 Nginx 插件 sudo yum install certbot python3-certbot-nginx
五、生成指定域名的https证书
# 执行 Certbot 自动化配置 sudo certbot --nginx -d xxx.abc.com
先会让你输入邮箱(通知证书到期)
后面都Y就可以了。
证书会保留在/etc/letsencrypt/live/xxx.abc.com,你可以通过命令查看证书。
sudo ls /etc/letsencrypt/live/xxx.abc.com/ # 应显示:cert.pem chain.pem fullchain.pem privkey.pem
如果自动配置未生效,手动编辑 Nginx 配置文件(默认路径 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf)
server {listen 80;server_name xxx.abc.com;return 301 https://$server_name$request_uri; # 强制跳转 HTTPS }server {listen 443 ssl;server_name xxx.abc.com;ssl_certificate /etc/letsencrypt/live/xxx.abc.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/xxx.abc.com/privkey.pem;# 增强 SSL 安全性ssl_protocols TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...';# 其他配置(如网站根目录)root /usr/share/nginx/html;index index.html;location / {try_files $uri $uri/ =404;} }
重新加载nginx配置
sudo nginx -t # 检查语法 sudo systemctl reload nginx # 重载配置
六、检查certbot是否正常运行
# 手动测试续期 sudo certbot renew --dry-run# 查看定时任务 sudo systemctl list-timers | grep certbot
# 查看文件
sudo systemctl list-unit-files | grep certbot
若查看定时任务没有出现以下记录,则可能certbot定时器每开启。
若查看文件,则会看到,certbot-renew.timer存在。
那么则启动timer。
sudo systemctl status certbot-renew.timer
再使用使用查看定时任务,并可以使用systemctl查看timer状态。
sudo systemctl status certbot-renew.timer
七、配置防火墙
# 开放 HTTP/HTTPS 端口 sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
八、测试
最后打开网站,查看证书是否正常存在,访问http是否自动跳转到https。