nginx 安装 ssl 证书,配置 https ,使用 Certbot 客户端免费配置 ssl 证书(centos7 下可以,其他版本自行测试)
* nginx 配置 ssl 证书前需要先安装 SSL 模块,本次操作是在已经安装 nginx 的基础上配置,先安装 SSL 模块,来到 nginx 源码目录,我的是这里,执行进入到自己的安装目录:
cd /usr/local/nginx-1.10.3
执行语句,重新安装 ssl 模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
如果报 error,一般是没有安装 openssl openssl-devel 导致的,先安装 openssl openssl-devel(没有报错直接跳过)
openssl openssl-devel
然后再:(不可 install )
make
等待执行完成后,我们需要把新编译的nginx模块替换原来的nginx,先备份(记得看好自己的安装路径)
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
关闭nginx(因为要把新的模块覆盖旧的nginx)
可以直接:ps aux | grep nginx 查看端口后:kill -9 xxxx
关闭nginx进程后就可以开始替换了(注意:我当前的位置是在我nginx的源码包中,目录不要搞错了)
cp ./objs/nginx /usr/local/nginx/sbin/
执行完后会出现如下提示:(直接输入 y 确定即可)
先切换到sbin目录,检测nginx的配置文件是否有错误
cd /usr/local/nginx/sbin/
./nginx -t
看到这个就是表示 ssl 模块安装成功。
========================
开始使用 Let’s Encrypt 安装证书:
安装 Certbot 客户端:
yum install -y epel-release
yum install -y certbot
因为使用 Certbot 获取证书时,Let's Encrypt 服务器会访问 http://sub.domain.com/.well-known 来验证你的域名服务器,因此需要修改 nginx 配置文件,配置 .well-known 指向本地一个目录:
server { ...... location /.well-known { alias /usr/local/nginx/html/.well-known; } ...... }
然后就可以使用 certbot 命令来获取证书了,获取证书时需要输入你的Email并接受用户条款。需要注意:-w 指定的 web 目录需要和前边 nginx 配置的 .well-known 的本地目录一致(/usr/local/nginx/html):
#记得把 sub.domain.com 替换成自己的域名;同时把 xxxxxx@xxx.com 替换成自己的邮箱
certbot certonly --webroot -w /usr/local/nginx/html/ -d sub.domain.com -m xxxxxx@xxx.com --agree-tos【一般用下面那句能直接成功】
-w 指定 webroot 目录
-d domain 想要获取的证书域名,支持多个域名
但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone,这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。
certbot certonly --standalone -d sub.domain.com -m xxxxxx@xxx.com --agree-tos //记得替换域名和邮箱【一般用这句能直接成功】
如果成功获取证书,你的密钥和证书存放在 /etc/letsencrypt/live/sub.domain.com/ 目录(sub.domain.com 目录是自己的域名),可以看到如下:
ll /etc/letsencrypt/live/sub.domain.com/ cert.pem -> ../../archive/sub.domain.com/cert1.pem chain.pem -> ../../archive/sub.domain.com/chain1.pem fullchain.pem -> ../../archive/sub.domain.com/fullchain1.pem privkey.pem -> ../../archive/sub.domain.com/privkey1.pem
有时需要删除已生成的证书,重新生成。可使用如下命令进行删除:
certbot delete --cert-name sub.domain.com //记得替换自己的域名
生成 dhparam(这一步花费时间比较多,而且有时候会失败,报找不到 sites-enabled 目录,建议提前在 /etc/nginx/ 目录下新建好 sites-enabled 目录)
openssl dhparam -out /etc/nginx/sites-enabled/dh4096.pem 4096
做完以上步骤后,在我们:/usr/local/nginx/conf 和 /etc/nginx 目录下,都会有 conf 目录下的相同文件,如下:
通常我们配置 /usr/local/nginx/conf 下的即可,/etc/nginx 下的也会同步更新
接下来开始配置我们的 nginx.conf 文件:
server { listen 80; server_name sub.domain.com; rewrite ^ https://$server_name$request_uri? permanent; } server { listen 443 ssl; server_name sub.domain.com; include /etc/nginx/sites-enabled/sub.domain.com.ssl; location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; proxy_pass http://127.0.0.1:8080; } }
sub.domain.com.ssl 文件配置内容:
ssl on; ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem; ssl_prefer_server_ciphers on; ssl_dhparam /etc/nginx/sites-enabled/dhparam.pem; //这里的 dhparam.pem 去目录下自己查看,替换一下 ssl_ciphers HIGH:!ADH:!MD5:!aNULL:!eNULL:!MEDIUM:!LOW:!EXP:!kEDH; ssl_protocols TLSv1.2 TLSv1.1 TLSv1; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security max-age=15768000;
之后重启 nginx ,到此 nginx 配置 https 完成。
启动:/usr/local/nginx/sbin/nginx 停止:/usr/local/nginx/sbin/nginx -s stop 重启:/usr/local/nginx/sbin/nginx -s reload
如果配置了服务:(可以看我上一篇文章怎么配置服务)
启动:systemctl start nginx.service
停止:systemctl stop nginx.service
重启:systemctl restart nginx.service
使用 Let's Encrypt 证书只有 90 天有效期,也就是3个月。我们需要在证书到期之前更新证书,certbot 提供了相应的命令 certbot renew。
/usr/bin/certbot renew --dry-run
可以将此更新命令添加到计划任务中,certbot renew 命令只会更新还有 30 天才会到期的证书,所以我们可以每隔 2 个月在凌晨3:30执行一次更新操作即可,创建一个文件 certbot-auto-renew-cron ,我创建在 /usr/local/nginx/ 目录下:
vi /usr/local/nginx/certbot-auto-renew-cron
写入内容:【注意:crontab linux 定时任务:每隔2个月在凌晨执行一次写法为:30 3 1 */2 * ,图片里面是错误的,以下面的代码为准】
30 3 1 */2 * /usr/bin/certbot renew --post-hook "systemctl restart nginx.service" --quiet >> /var/log/cerbot.log
//注意:中间重启 nginx 命令是我已经创建了 nginx 服务,才可以这么写,没有的可以使用 /usr/local/nginx/sbin/nginx
启动 crontab 定时任务:
crontab certbot-auto-renew-cron
crontab 默认不会开机自启,加入开机自启:
vi /etc/rc.d/rc.local
最下面加入:
/sbin/systemctl start crond
授权:
chmod +x /etc/rc.d/rc.local
重启:
/sbin/systemctl status crond //查看状态 /sbin/systemctl start crond //启动 /sbin/systemctl stop crond //停止 /sbin/systemctl restart crond //重启
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
2018-05-25 win10下安装ubuntu双系统方法