免费 Https 证书(Let's Encrypt)申请与配置

之前要申请免费的 https 证书操作步骤相当麻烦,今天看到有人在讨论,就搜索了一下。发现现在申请步骤简单多了

1. 下载 certbot

git clone https://github.com/certbot/certbot
cd certbot
./certbot-auto --help

解压打开执行就会有相关提示。

2. 生成免费证书

./certbot-auto certonly --webroot --agree-tos -v -t --email 邮箱地址 -w 网站根目录 -d 网站域名
./certbot-auto certonly --webroot --agree-tos -v -t --email [email protected] -w /path/to/your/web/root -d xxxx.com

注意 这里 默认会自动生成到 /网站根目录/.well-known/acme-challenge 文件夹,然后 shell 脚本会对应的访问 网站域名/.well-known/acme-challenge 是否存在来确定你对网站的所属权

比如:我的域名是 note.crazy4code.com 那我就得保证域名下面的 .well-known/acme-challenge/ 目录是可访问的

当然这里最重要的是.well-known/acme-challenge必须是在你的网站根目录(thinkphp5和laravel5.5的根目录一般是在public下啦!我们要在这下面创建,顺便给目录 chmod -R 777 .well-known)

在nginx下的配置,需要在域名配置文件。如xxx.conf中修改

server {
  listen   443 ssl;
  listen   80;
  server_name  cms2.wunsun.com;
  root /repo/web/laravel_cms2/public/;
  index  index.php index.html;
  autoindex       on;  #这里是给权限能打开目录
  ssl on;   #绿色部分是后面的配置,是生成证书后填入
  ssl_certificate /etc/letsencrypt/live/cms2.wunsun.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/cms2.wunsun.com/privkey.pem;
  ssl_dhparam /etc/ssl/certs/dhparams.pem;
  ssl_protocols SSLv3 TLSv1 TLSv1.1 TL
  location / {
    if (!-e $request_filename){
      rewrite ^/(.*)$ /index.php/$1 last;
    }
  }

  error_log  /repo/log/nginx/cms2-error.log error;
  access_log  /repo/log/nginx/cms2-access.log;

  client_max_body_size 100m;

  location ~ \.php($|/){
    set $script     $uri;
    set $path_info  "";
    if ($uri ~ "^(.+?\.php)(/.+)$") {
      set $script     $1;
      set $path_info  $2;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index  index.php;
    include        fastcgi_params; 
   fastcgi_param  SCRIPT_FILENAME    $document_root$script;
    fastcgi_param  SCRIPT_NAME        $script;
    fastcgi_param  PATH_INFO          $path_info;
    fastcgi_read_timeout 7200;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
      expires 7d;  #设置7天过期
  }
  location ~ .*\.(js|css)?$ {
      expires 12h; #设置12小时
  }
  location ~ .*\.(json)?$ {
      expires 12h; #缓存12h
  }
  gzip on;
  gzip_disable "msie6";
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
  gzip_vary on;
}

如果返回正常就确认了你对这个网站的所有权,就能顺利生成,完成后这个目录会被清空

3. 获取证书

如果上面的步骤正常 shell 脚本会展示如下信息:

- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/网站域名/fullchain.pem
...

4. 生成 dhparams

使用 openssl 工具生成 dhparams

openssl dhparam -out /etc/ssl/certs/dhparams.pem 2048

5. 配置 Nginx

打开 nginx server 配置文件加入如下设置:

listen 443

ssl on;
ssl_certificate /etc/letsencrypt/live/网站域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/网站域名/privkey.pem;
ssl_dhparam /etc/ssl/certs/dhparams.pem;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

然后重启 nginx 服务就可以了 service nginx restart

6. 强制跳转 https

https 默认是监听 443 端口的,没开启 https 访问的话一般默认是 80 端口。如果你确定网站 80 端口上的站点都支持 https 的话加入下面的配件可以自动重定向到 https

server {
    listen 80;
    server_name your.domain.com;
    return 301 https://$server_name$request_uri;
}

笔者并没有尝试这种写法,大家可以尝试一下。我的写法是(配置后的xxx.conf)server {        listen  80;        server_name xxx.com;               rewrite ^(.*)$  https://$host$1 permanent;  }server {      listen   443 ;      server_name  xxx.com;  root /repo/web/laravel_cms2/public/;      index  index.php index.html;
    。。。。。。省略,和之前配置相同

7. 证书更新

免费证书只有 90 天的有效期,到时需要手动更新 renew。刚好 Let’s encrypt 旗下还有一个 Let’s monitor 免费服务,注册账号添加需要监控的域名,系统会在证书马上到期时发出提醒邮件,非常方便。收到邮件后去后台执行 renew 即可,如果提示成功就表示 renew 成功

./certbot-auto renew

猜你喜欢

转载自blog.csdn.net/qq_27295403/article/details/80403086