安装nginx
本人采用手动编译安装方式,nginx版本为1.24.0
下载nginx源码
通过ftp工具上传到服务器并解压
进入nginx-1.24.0目录,并运行如下命令
./configure --with-http_ssl_module
如下图所示:
其中 --with-http_ssl_module 目的是为了加入https功能必须引入的模块,默认如果直接./configure是不会加入该模块,运行成功如下图
分别运行make和make install命令完成安装
完成安装
默认安装路径为 /usr/local/nginx
阿里云配置
安全组开通80和443端口
Centos防火墙配置
默认阿里云的Centos机器安装好后,是不会fiewalld默认是没有开启的
如果您机器上启用了firewalld,通过以下方式开启
sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https
sudo firewall-cmd --runtime-to-permanent
安装certbox
sudo yum install epel-release
sudo yum install certbot-nginx
配置nginx
在前面安装好的nginx中找到配置文件nginx.conf,如下图
打开nginx.conf编辑server模块中的server_name为你自己的注册的域名(xxx.com)编辑好后保存退出
获得免费证书
输入命令
sudo certbot --nginx -d xxx.com -d www.xxx.com
-d 表示你自己注册好的域名,这里由于是根域名所示指定了两个(xxx.com是举例)
激活中会让你输入email,也可以不输入
OutputPlease choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
OutputIMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
expire on 2024-4-1. To obtain a new or tweaked version of this
certificate in the future, simply run certbot again with the
"certonly" option. To non-interactively renew *all* of your
certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
同时会收到一份邮件(如果你填了邮箱)
自动续订证书
由于Let’s Encrypt证书有效期为90天,所以为了避免90天后手动更新,一般会通过定时任务crontab来进行更新证书,步骤如下
输入
sudo crontab -e
填写更新规则
0 1 * * * /usr/bin/certbot renew --quiet
其中–quiet表示无须certbox任何交互和输出
遇到问题
以上步骤都完成后,Let’s Encrypt证书已经安装成功,但是如果您试着去使用https://www.xxx.com去访问您的网站发现提示网络不通,您也可以试着通过telnet xxx.com 443端口,发现是不通的,问题在于nginx不会自动加载nginx的改变配置,需要重启nginx