{"id":12188,"date":"2025-02-17T17:46:16","date_gmt":"2025-02-18T01:46:16","guid":{"rendered":"https:\/\/www.xh86.me\/?p=12188"},"modified":"2025-02-17T17:46:28","modified_gmt":"2025-02-18T01:46:28","slug":"linux%e4%b8%ad%e5%ae%89%e8%a3%85ssl%e8%af%81%e4%b9%a6%ef%bc%8c%e5%b9%b6%e4%b8%94%e8%87%aa%e5%8a%a8%e7%bb%ad%e8%ae%a2","status":"publish","type":"post","link":"https:\/\/www.xh86.me\/?p=12188","title":{"rendered":"Linux\u4e2d\u5b89\u88c5SSL\u8bc1\u4e66\uff0c\u5e76\u4e14\u81ea\u52a8\u7eed\u8ba2"},"content":{"rendered":"<h2><strong>1. \u5b89\u88c5 Certbot<\/strong><\/h2>\n<p>\u4e0d\u540c Linux \u53d1\u884c\u7248\u5b89\u88c5 Certbot \u7684\u65b9\u6cd5\u7565\u6709\u4e0d\u540c\uff1a<\/p>\n<h3><strong>Debian\/Ubuntu<\/strong><\/h3>\n<pre><code class=\"language-bash\">sudo apt update\r\nsudo apt install certbot python3-certbot-nginx -y  # \u5982\u679c\u4f7f\u7528Nginx\r\nsudo apt install certbot python3-certbot-apache -y # \u5982\u679c\u4f7f\u7528Apache\r\n<\/code><\/pre>\n<h3><strong>CentOS\/RHEL (\u4f7f\u7528 EPEL)<\/strong><\/h3>\n<pre><code class=\"language-bash\">sudo dnf install epel-release -y\r\nsudo dnf install certbot python3-certbot-nginx -y  # \u5982\u679c\u4f7f\u7528Nginx\r\nsudo dnf install certbot python3-certbot-apache -y # \u5982\u679c\u4f7f\u7528Apache\r\n<\/code><\/pre>\n<h3><strong>Rocky Linux\/AlmaLinux<\/strong><\/h3>\n<pre><code class=\"language-bash\">sudo dnf install epel-release -y\r\nsudo dnf install certbot python3-certbot-nginx -y\r\n<\/code><\/pre>\n<hr \/>\n<h2><strong>2. \u751f\u6210 SSL \u8bc1\u4e66<\/strong><\/h2>\n<p>Certbot \u4e3b\u8981\u652f\u6301\u4ee5\u4e0b\u65b9\u5f0f\u7533\u8bf7 SSL \u8bc1\u4e66\uff1a<\/p>\n<h3><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 Web \u670d\u52a1\u5668\u81ea\u52a8\u914d\u7f6e\uff08\u63a8\u8350\uff09<\/strong><\/h3>\n<h4><strong>Nginx \u81ea\u52a8\u7533\u8bf7<\/strong><\/h4>\n<pre><code class=\"language-bash\">sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com\r\n<\/code><\/pre>\n<h4><strong>Apache \u81ea\u52a8\u7533\u8bf7<\/strong><\/h4>\n<pre><code class=\"language-bash\">sudo certbot --apache -d yourdomain.com -d www.yourdomain.com\r\n<\/code><\/pre>\n<blockquote><p><strong>\u8bf4\u660e\uff1a<\/strong> Certbot \u4f1a\u81ea\u52a8\u68c0\u6d4b\u670d\u52a1\u5668\u5e76\u4fee\u6539 Nginx\/Apache \u914d\u7f6e\u6587\u4ef6\uff0c\u540c\u65f6\u7533\u8bf7\u8bc1\u4e66\u3002<\/p><\/blockquote>\n<hr \/>\n<h3><strong>\u65b9\u6cd5 2\uff1a\u624b\u52a8 DNS \u9a8c\u8bc1\uff08\u9002\u7528\u4e8e\u65e0 Web \u670d\u52a1\u5668\u6216\u901a\u914d\u7b26\u8bc1\u4e66\uff09<\/strong><\/h3>\n<p>\u4f7f\u7528 DNS TXT \u8bb0\u5f55\u8fdb\u884c\u57df\u540d\u9a8c\u8bc1\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo certbot certonly --manual --preferred-challenges dns --email admin@yourdomain.com -d \"*.yourdomain.com\" -d yourdomain.com\r\n<\/code><\/pre>\n<p>Certbot \u4f1a\u63d0\u793a\u4f60\u6dfb\u52a0\u4e00\u6761 <code>_acme-challenge.yourdomain.com<\/code> \u7684 TXT \u8bb0\u5f55\uff0c\u6dfb\u52a0\u540e\u7b49\u5f85 DNS \u89e3\u6790\u751f\u6548\u518d\u7ee7\u7eed\u3002<\/p>\n<hr \/>\n<h2><strong>3. \u914d\u7f6e\u81ea\u52a8\u7eed\u8ba2<\/strong><\/h2>\n<p>Let&#8217;s Encrypt \u8bc1\u4e66\u6709\u6548\u671f\u4e3a <strong>90 \u5929<\/strong>\uff0c\u63a8\u8350\u6bcf 60 \u5929\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<h3><strong>\u68c0\u67e5 Certbot \u662f\u5426\u5b89\u88c5\u5b9a\u65f6\u4efb\u52a1<\/strong><\/h3>\n<p>Certbot \u9ed8\u8ba4\u4f1a\u5b89\u88c5 <code>systemd<\/code> \u8ba1\u5212\u4efb\u52a1\uff0c\u68c0\u67e5\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl list-timers | grep certbot\r\n<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u81ea\u52a8\u4efb\u52a1\uff0c\u53ef\u4ee5\u624b\u52a8\u6dfb\u52a0 <code>cronjob<\/code>\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo crontab -e\r\n<\/code><\/pre>\n<p>\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-bash\">0 3 * * * certbot renew --quiet\r\n<\/code><\/pre>\n<p>\u8868\u793a\u6bcf\u5929\u51cc\u6668 3 \u70b9\u68c0\u67e5\u5e76\u66f4\u65b0 SSL \u8bc1\u4e66\u3002<\/p>\n<p>\u6216\u8005\u4f7f\u7528 <code>systemd<\/code> \u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl enable certbot.timer\r\nsudo systemctl start certbot.timer\r\n<\/code><\/pre>\n<p>\u624b\u52a8\u6d4b\u8bd5\u7eed\u8ba2\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo certbot renew --dry-run\r\n<\/code><\/pre>\n<hr \/>\n<h2><strong>4. Certbot \u5e38\u7528\u547d\u4ee4<\/strong><\/h2>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>certbot --nginx -d yourdomain.com<\/code><\/td>\n<td>\u81ea\u52a8\u4e3a Nginx \u7533\u8bf7\u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot --apache -d yourdomain.com<\/code><\/td>\n<td>\u81ea\u52a8\u4e3a Apache \u7533\u8bf7\u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot certonly --standalone -d yourdomain.com<\/code><\/td>\n<td>\u7533\u8bf7\u8bc1\u4e66\u4f46\u4e0d\u4fee\u6539 Web \u914d\u7f6e\uff08\u9700\u8981\u505c\u6389 Web \u670d\u52a1\u5668\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>certbot certonly --manual -d yourdomain.com<\/code><\/td>\n<td>\u624b\u52a8\u7533\u8bf7 SSL \u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot renew<\/code><\/td>\n<td>\u7eed\u8ba2\u6240\u6709\u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot certificates<\/code><\/td>\n<td>\u67e5\u770b\u5df2\u5b89\u88c5\u7684 SSL \u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot revoke --cert-name yourdomain.com<\/code><\/td>\n<td>\u64a4\u9500\u8bc1\u4e66<\/td>\n<\/tr>\n<tr>\n<td><code>certbot delete --cert-name yourdomain.com<\/code><\/td>\n<td>\u5220\u9664\u8bc1\u4e66<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2><strong>5. \u914d\u7f6e HTTPS\uff08Nginx \u793a\u4f8b\uff09<\/strong><\/h2>\n<p>\u5982\u679c Certbot \u6ca1\u6709\u81ea\u52a8\u4fee\u6539 Nginx \u914d\u7f6e\uff0c\u53ef\u4ee5\u624b\u52a8\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-nginx\">server {\r\n    listen 80;\r\n    server_name yourdomain.com www.yourdomain.com;\r\n    return 301 https:\/\/$host$request_uri;\r\n}\r\n\r\nserver {\r\n    listen 443 ssl;\r\n    server_name yourdomain.com www.yourdomain.com;\r\n\r\n    ssl_certificate \/etc\/letsencrypt\/live\/yourdomain.com\/fullchain.pem;\r\n    ssl_certificate_key \/etc\/letsencrypt\/live\/yourdomain.com\/privkey.pem;\r\n\r\n    location \/ {\r\n        root \/var\/www\/html;\r\n        index index.html index.php;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>\u7136\u540e\u91cd\u542f Nginx\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl restart nginx\r\n<\/code><\/pre>\n<hr \/>\n<h2><strong>6. \u9a8c\u8bc1 SSL \u8bc1\u4e66<\/strong><\/h2>\n<p>\u786e\u8ba4\u8bc1\u4e66\u751f\u6548\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo openssl x509 -in \/etc\/letsencrypt\/live\/yourdomain.com\/fullchain.pem -text -noout\r\n<\/code><\/pre>\n<p>\u8bbf\u95ee <code>https:\/\/yourdomain.com<\/code> \u68c0\u67e5\u662f\u5426\u6210\u529f\u542f\u7528\u4e86 HTTPS\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5b89\u88c5 Certbot \u4e0d\u540c Linux \u53d1\u884c\u7248\u5b89\u88c5 Certbot \u7684\u65b9\u6cd5\u7565\u6709\u4e0d\u540c\uff1a Debian\/Ub [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,3],"tags":[],"class_list":["post-12188","post","type-post","status-publish","format-standard","hentry","category-linux","category-system"],"_links":{"self":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12188"}],"version-history":[{"count":2,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12188\/revisions"}],"predecessor-version":[{"id":12190,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12188\/revisions\/12190"}],"wp:attachment":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}