Nginx-Let's Encrypt配置https

Let's Encrypt

如果要启用HTTPS,我们就需要从证书授权机构处获取一个证书,Let’s Encrypt 就是一个证书授权机构。我们可以从 Let’s Encrypt 获得网站域名的免费的证书。

Certbot

Certbot是Let’s Encrypt推出的获取证书的客户端,可以让我们免费快速地获取Let’s Encrypt证书。

 

注意:默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数。

一、安装Certbot

进入Certbot的官网,选择你所使用的软件和系统环境,然后就会跳转到对应版本的安装方法,以Ubuntu + Nginx为例。

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot 

 

二、申请证书

安装完成后执行:

certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com

这条命令的意思是为以/var/www/example为根目录的两个域名example.comwww.example.com申请证书。

如果你的网站没有根目录或者是你不知道你的网站根目录在哪里,可以通过下面的语句来实现

certbot certonly --standalone -d example.com -d www.example.com

使用这个语句时Certbot会自动启用网站的443端口来进行验证,如果你有某些服务占用了443端口,就必须先停止这些服务,然后再用这种方式申请证书。

证书申请完之后,Certbot会告诉你证书所在的目录,一般来说会在/etc/letsencrypt/live/这个目录下。

 

三、修改nginx的配置文件

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;


    #keepalive_timeout  0;
    keepalive_timeout  65;

    #设置HTTP强制跳转HTTPS
    server {
	listen 80;
	server_name example.com;  #这里修改为网站域名
	rewrite ^(.*)$ https://$host$1 permanent;
    }

    # HTTPS server
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

}

保存退出后,通过nginx -t来检查配置文件是否正确,有错误的话改之即可。配置文件检测正确之后,通过nginx -s reload来重载配置文件。

然后通过访问https://example.com来查看是否配置成功。

如果发现无法访问或者是加载不出来的话检查一下443端口有没有开启!
 

四、设置定时任务

出于安全策略, Let's Encrypt 签发的证书有效期只有 90 天。通过 ./certbot-auto renew 命令可以续签。
编辑 crontab 文件:

crontab -e

添加 certbot 命令:
在每个月1号凌晨3点更新,该命令将检查服务器上的证书是否将在未来30天内过期,如果是,则进行更新。--quiet 指令告诉 certbot 不要生成输出。

0 0 3 * * certbot renew --quiet --renew-hook "/usr/local/nginx/sbin/nginx -s reload"

或者

0 0 3 * * certbot renew --force-renew --renew-hook "/usr/local/nginx/sbin/nginx -s reload"

 

五、crontab操作

Ubuntu系统使用crontab定时执行python脚本

crontab -e配置完成后,如何把保存并退出?

1.Ctrl+O  写入

2.出现“FIile name to Write...”,输入Enter

3.Ctrl+x 保存输出

提示“crontab:installing new crontab”表示成功。

 

 

 

相关学习链接:

https://blog.csdn.net/xs18952904/article/details/79262646

https://www.cnblogs.com/saneri/p/5391821.html

https://www.cnblogs.com/xiaobingch/p/10188055.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值