【Web】网站由http如何设置为https

   |   3 minute read   |   Using 563 words

网站由http如何设置为https

使用 certbot 将网站转为 https

更多关于 certbot

https://certbot.eff.org/

具体实践

1. 安装 certbot

sudo apt-get install certbot

2. 进入网页的根目录 创建 .well-known 文件夹

cd /var/www/html 
mkdir .well-known

3. 更改根目录权限

chgrp  www-data /var/www/html 
chmod g+s /var/www/html 
 

4. 申请 SSL 认证

certbot certonly --webroot -w /var/www/html -d YourDomain.com, 你就会得到 3 个 .gem 文件(下面会给出 path)

➜  ~ certbot certonly --webroot -w /var/www/html -d YourDomain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):YourEmail@mali.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017-w-v1.3-notice.pdf.
You must agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for YourDomain.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/YourDomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/YourDomain.com/privkey.pem
   Your cert will expire on 2022-12-04. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


5. 为 SSL 设置 Virtaulhost

在你的 网站的 .conf 中添加 virtualhost 并添加 SSLcertificateFile 的路径

➜  ~ cat /etc/apache2/sites-available/000-default.conf 
<VirtualHost *:80>

        ServerAdmin webmaster@localhost
    	ServerName YourDomain.com
        DocumentRoot /var/www/html



        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>


<VirtualHost *:443>
    ServerName YourDomain.com
        DocumentRoot /var/www/html

    SSLEngine on
    SSLcertificateFile /etc/letsencrypt/live/YourDomain.com/cert.pem
    SSLcertificateKeyFile /etc/letsencrypt/live/YourDomain.com/privkey.pem
    SSLcertificateChainFile /etc/letsencrypt/live/YourDomain.com/chain.pem

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>



6. 启动 SSL

a2enmod ssl

7. 重启 Apache

systemctl restart apache2

8. 将 对网站的 http 请求 重定向到 https

.conf 中添加

 Redirect permanent / https://YourDomain.com

9. 设置自动更新 SSL 证书

手动更新

certbot renew

自动更新

 crontab -e

编辑 crontab

0 8 */88 * * /usr/bin/certbot renew


© 2025 by clayliu. All Rights Reserved.