{"id":53135,"date":"2024-10-09T08:36:15","date_gmt":"2024-10-09T00:36:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/53135\/"},"modified":"2024-11-14T10:35:56","modified_gmt":"2024-11-14T02:35:56","slug":"%e5%9c%a8-ubuntu-18-04-%e4%b8%8a%e4%bd%bf%e7%94%a8-lets-encrypt-%e4%bf%9d%e8%ad%b7-nginx","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/53135\/","title":{"rendered":"\u5728 Ubuntu 18.04 \u4e0a\u4f7f\u7528 Let&#8217;s Encrypt \u4fdd\u8b77 Nginx"},"content":{"rendered":"<h1 id=\"%e5%9c%a8-ubuntu-18-04-%e4%b8%8a%e4%bd%bf%e7%94%a8-lets-encrypt-%e4%bf%9d%e8%ad%b7-nginx-sBzyLfshTU\">\u5728 Ubuntu 18.04 \u4e0a\u4f7f\u7528 Let&#8217;s Encrypt \u4fdd\u8b77 Nginx<\/h1>\n<p>\u96a8\u8457\u7db2\u7d61\u5b89\u5168\u7684\u91cd\u8981\u6027\u65e5\u76ca\u589e\u52a0\uff0c\u4f7f\u7528 SSL\/TLS \u8b49\u66f8\u4f86\u4fdd\u8b77\u7db2\u7ad9\u5df2\u6210\u70ba\u4e00\u9805\u5fc5\u8981\u63aa\u65bd\u3002Let&#8217;s Encrypt \u662f\u4e00\u500b\u514d\u8cbb\u7684\u8b49\u66f8\u9812\u767c\u6a5f\u69cb\uff0c\u63d0\u4f9b\u81ea\u52d5\u5316\u7684 SSL \u8b49\u66f8\u7533\u8acb\u548c\u66f4\u65b0\u670d\u52d9\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Ubuntu 18.04 \u4e0a\u4f7f\u7528 Let&#8217;s Encrypt \u4f86\u4fdd\u8b77 Nginx \u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-sBzyLfshTU\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u60a8\u5df2\u7d93\u5b89\u88dd\u4e86 Nginx \u4e26\u4e14\u64c1\u6709\u4e00\u500b\u6709\u6548\u7684\u57df\u540d\u3002\u60a8\u9084\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd Certbot\uff0c\u9019\u662f\u4e00\u500b\u7528\u65bc\u7372\u53d6\u548c\u7ba1\u7406 Let&#8217;s Encrypt \u8b49\u66f8\u7684\u5de5\u5177\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-nginx-sBzyLfshTU\">\u5b89\u88dd Nginx<\/h3>\n<pre><code>sudo apt update\nsudo apt install nginx\n<\/code><\/pre>\n<h3 id=\"%e5%ae%89%e8%a3%9d-certbot-sBzyLfshTU\">\u5b89\u88dd Certbot<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5b89\u88dd Certbot \u53ca\u5176 Nginx \u63d2\u4ef6\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt install certbot python3-certbot-nginx\n<\/code><\/pre>\n<h2 id=\"%e7%8d%b2%e5%8f%96-ssl-%e8%ad%89%e6%9b%b8-sBzyLfshTU\">\u7372\u53d6 SSL \u8b49\u66f8<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Certbot \u7372\u53d6 SSL \u8b49\u66f8\u3002\u8acb\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com\n<\/code><\/pre>\n<p>\u8acb\u5c07 &#8220;yourdomain.com&#8221; \u66ff\u63db\u70ba\u60a8\u7684\u5be6\u969b\u57df\u540d\u3002Certbot \u5c07\u81ea\u52d5\u914d\u7f6e Nginx \u4e26\u7372\u53d6\u8b49\u66f8\u3002\u5728\u904e\u7a0b\u4e2d\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u4ee5\u63a5\u6536\u91cd\u8981\u7684\u901a\u77e5\uff0c\u4e26\u540c\u610f\u670d\u52d9\u689d\u6b3e\u3002<\/p>\n<h2 id=\"%e8%87%aa%e5%8b%95%e6%9b%b4%e6%96%b0%e8%ad%89%e6%9b%b8-sBzyLfshTU\">\u81ea\u52d5\u66f4\u65b0\u8b49\u66f8<\/h2>\n<p>Let&#8217;s Encrypt \u7684\u8b49\u66f8\u6709\u6548\u671f\u70ba 90 \u5929\uff0c\u56e0\u6b64\u5efa\u8b70\u8a2d\u7f6e\u81ea\u52d5\u66f4\u65b0\u3002Certbot \u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u9ede\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u4f86\u6e2c\u8a66\u81ea\u52d5\u66f4\u65b0\uff1a<\/p>\n<pre><code>sudo certbot renew --dry-run\n<\/code><\/pre>\n<p>\u5982\u679c\u6e2c\u8a66\u6210\u529f\uff0c\u60a8\u53ef\u4ee5\u5c07\u81ea\u52d5\u66f4\u65b0\u4efb\u52d9\u6dfb\u52a0\u5230 crontab \u4e2d\u3002\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f86\u7de8\u8f2f crontab\uff1a<\/p>\n<pre><code>sudo crontab -e\n<\/code><\/pre>\n<p>\u7136\u5f8c\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u6bcf\u5929\u904b\u884c\u81ea\u52d5\u66f4\u65b0\uff1a<\/p>\n<pre><code>0 0 * * * \/usr\/bin\/certbot renew &gt;&gt; \/var\/log\/letsencrypt\/letsencrypt.log\n<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-sBzyLfshTU\">\u914d\u7f6e Nginx<\/h2>\n<p>\u5728\u7372\u53d6\u8b49\u66f8\u5f8c\uff0cCertbot \u6703\u81ea\u52d5\u914d\u7f6e Nginx\u3002\u5982\u679c\u60a8\u9700\u8981\u624b\u52d5\u914d\u7f6e\uff0c\u8acb\u7de8\u8f2f Nginx \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/default\n<\/code><\/pre>\n<p>\u78ba\u4fdd\u60a8\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name yourdomain.com www.yourdomain.com;\n    return 301 https:\/\/$host$request_uri;\n}\n\nserver {\n    listen 443 ssl;\n    server_name yourdomain.com www.yourdomain.com;\n\n    ssl_certificate \/etc\/letsencrypt\/live\/yourdomain.com\/fullchain.pem;\n    ssl_certificate_key \/etc\/letsencrypt\/live\/yourdomain.com\/privkey.pem;\n\n    location \/ {\n        proxy_pass http:\/\/localhost:3000; # \u6839\u64da\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u8abf\u6574\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n        proxy_cache_bypass $http_upgrade;\n    }\n}\n<\/code><\/pre>\n<p>\u5b8c\u6210\u5f8c\uff0c\u6aa2\u67e5 Nginx \u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t\n<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u6709\u932f\u8aa4\uff0c\u91cd\u65b0\u555f\u52d5 Nginx \u4ee5\u61c9\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sBzyLfshTU\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 Let&#8217;s Encrypt \u70ba Nginx \u914d\u7f6e SSL \u8b49\u66f8\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u5f0f\u4f86\u63d0\u9ad8\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u5728 Ubuntu 18.04 \u4e0a\u8f15\u9b06\u5be6\u73fe HTTPS \u4fdd\u8b77\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u5b89\u5168\u7684\u7db2\u7ad9\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u500b\u4eba\u7db2\u7ad9\u9084\u662f\u5546\u696d\u61c9\u7528\uff0c\u78ba\u4fdd\u60a8\u7684\u4f3a\u670d\u5668\u5b89\u5168\u662f\u6bcf\u500b\u7db2\u7ad9\u7ba1\u7406\u54e1\u7684\u8cac\u4efb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Ubuntu 18.04 \u4e0a\u4f7f\u7528 Let&#8217;s Encrypt \u70ba Nginx \u914d\u7f6e SSL \u8b49\u66f8\uff0c\u78ba\u4fdd\u7db2\u7ad9\u5b89\u5168\u8207\u6578\u64da\u52a0\u5bc6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-53135","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=53135"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53135\/revisions"}],"predecessor-version":[{"id":53136,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53135\/revisions\/53136"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=53135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=53135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=53135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}