当前位置 博文首页 > 缘来侍你的博客:Linux生成SSL证书 配置https证书 自动续签

    缘来侍你的博客:Linux生成SSL证书 配置https证书 自动续签

    作者:[db:作者] 时间:2021-09-16 13:33

    官方文档

    安装acme.sh

    curl https://get.acme.sh | sh

    如果此方式安装失败,可以使用 git 安装

    git clone https://github.com/acmesh-official/acme.sh.git
    cd ./acme.sh
    ./acme.sh --install
    

    安装过程中会自动为你创建 crontab 定时任务, 每天 0:15 自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书crontab -l

    15 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
    

    创建 一个 bash 的 alias, 方便使用:?alias acme.sh=~/.acme.sh/acme.sh

    生成证书

    单域名:

    acme.sh  --issue  -d yourdomain.com --webroot  /home/wwwroot/blog/
    

    可以设置多个域名:

    acme.sh  --issue  -d yourdomain.com -d www.yourdomain.com  --webroot  /home/wwwroot/blog/
    

    只需要指定域名,并指定域名所在的网站根目录。acme.sh 会全自动的生成验证文件,并放到网站的根目录,然后自动完成验证, 最后又自动删除验证文件。

    安装证书

    生成证书后,将证书安装到Nginx。
    默认生成的证书都放在安装目录下:?~/.acme.sh/,这个目录一般来说不能让nginx直接使用,所以我们需要将证书放到一个指定的目录:/usr/local/nginx/ssl

    mkdir /usr/local/nginx/ssl
    
    acme.sh --installcert -d yourdomain.com --key-file /usr/local/nginx/ssl/yourdomain.com.key --fullchain-file /usr/local/nginx/ssl/yourdomain.com.crt --reloadcmd "service nginx force-reload"
    

    请注意:reloadcmd非常重要。该证书可以自动更新,但是如果没有正确的"reloadcmd",该证书可能无法刷新到您的服务器,那么您的网站将无法在60天内显示更新的证书。

    配置Nginx

    server {
      listen       443;
      server_name yourdomain.com;
      index index.html index.htm index.php;
      root /data/wwwroot/blog;
    
      #ssl on;   #如果硬性要求全部走https协议,这里开启ssl on
      ssl_certificate   /usr/local/nginx/ssl/yourdomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/ssl/yourdomain.com.key;
    
      #ssl性能调优
      #nginx 1.13.0支持了TLSv1.3,TLSv1.3相比之前的TLSv1.2、TLSv1.1等性能大幅提升
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
      ssl_prefer_server_ciphers on;
      ssl_session_timeout 10m;
      #使用ssl_session_cache优化https下Nginx的性能
      ssl_session_cache builtin:1000 shared:SSL:10m;
      #OCSP Stapling 开启。OCSP是用于在线查询证书吊销情况的服务,使用OCSP Stapling能将证书有效状态的信息缓存到服务器,提高 TLS 握手速度
      ssl_stapling on;
      #OCSP Stapling 验证开启
      ssl_stapling_verify on;
    
      location ~ [^/]\.php(/|$) {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
      }
    
      location / {
      	if (!-e $request_filename) {
      		rewrite  ^(.*)$  /index.php?s=/$1  last;
      		break;
      	}
      }
    	location /nginx_status
    	{
    	stub_status on;
    		access_log   off;
    	}
    
    	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    	{
    		expires      30d;
    	}
    
    	location ~ .*\.(js|css)?$
    	{
    		expires      12h;
    	}
    
    	location ~ /.well-known {
    		allow all;
    	}
    
    	location ~ /\.
    	{
    		deny all;
    	}
    
    	error_log  /home/wwwlogs/error.www.codelife.log;
    
    }
    

    配置完后验证 nginx 配置有没有错误:
    nginx -t

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    没有错误重启 nginx :service nginx restart

    手动更新证书:acme.sh --cron -f

    目前由于 acme 协议和 letsencrypt CA 都在频繁的更新, 因此 acme.sh 也经常更新以保持同步.所以为了省心省力,最好还是设置一下软件的自动更新:acme.sh --upgrade --auto-upgrade

    cs
    下一篇:没有了