USDT 云服务器
🏠 首页 / 📚 USDT服务器资讯中心 / 📄 超详细 VPS 免费安装 HTTPS 全教程(Let’s Encrypt + Nginx/Apache)

超详细 VPS 免费安装 HTTPS 全教程(Let’s Encrypt + Nginx/Apache)

📅 2026-05-14 👁 81 📁 网络知识
超详细 VPS 免费安装 HTTPS 全教程(Let’s Encrypt + Nginx/Apache)

在当今网络安全备受重视的时代,为网站添加 HTTPS 加密至关重要。它不仅能保护用户数据传输安全,还能提升网站在搜索引擎中的排名。如果您使用 VPS 搭建网站,通过 Let’s Encrypt 配合 Nginx 或 Apache 服务器,可免费实现 HTTPS 安装。下面就为您带来详细教程。


一、准备工作


拥有VPS 服务器:确保您已购买并能正常登录 VPS,且已安装好操作系统,比如常见的 Linux 发行版(如 Ubuntu、CentOS 等)。不同的 Linux 系统在后续操作命令上可能略有差异,但整体思路一致。


安装服务器软件:根据您的选择,安装 Nginx 或 Apache。以 Ubuntu 系统为例,如果选择 Nginx,在终端输入 “sudo apt - get update && sudo apt - get install nginx”;


若选择 Apache,则输入 “sudo apt - get update && sudo apt - get install apache2”。安装过程中,系统可能会提示确认安装,按提示输入 “Y” 并回车即可。


二、安装 Let’s Encrypt


Certbot 安装:Let’s Encrypt 官方推荐使用 Certbot 来获取证书。对于不同的服务器软件和系统,安装方法稍有不同。


以 Ubuntu 系统且服务器为 Nginx 为例,先添加 Certbot 的官方软件源,输入 “sudo add - apt - repository ppa:certbot/certbot”,然后更新软件包列表 “sudo apt - get update”,最后安装 Certbot “sudo apt - get install python3 - certbot - nginx”。


如果是 Apache 服务器,则安装 “python3 - certbot - apache”。


获取证书:安装好 Certbot 后,就可以获取 HTTPS 证书了。假设您的网站域名是 “example.com”,对于 Nginx 服务器,运行 “sudo certbot --nginx - d example.com - d www.example.com”;


对于 Apache 服务器,运行 “sudo certbot --apache - d example.com - d www.example.com”。这里的 “-d” 参数用于指定域名,可根据实际情况添加多个域名。Certbot 会自动检测您的服务器配置,并尝试为您的域名申请证书。过程中可能会要求您输入邮箱地址,用于接收证书相关的通知,以及确认是否同意服务条款等,按提示操作即可。


三、配置服务器以使用 HTTPS


(一)Nginx 配置


备份原配置文件:为防止配置出错无法恢复,先备份 Nginx 的配置文件。在终端输入 “sudo cp /etc/nginx/sites - available/default/etc/nginx/sites - available/default.backup”。


编辑配置文件:使用文本编辑器打开 Nginx 的配置文件,输入 “sudo nano /etc/nginx/sites - available/default”。找到 HTTP 服务器块部分,一般类似这样:


server {
    listen 80;
    server_name example.com www.example.com;
    # 其他配置
}


在其下方添加 HTTPS 服务器块,内容如下:


server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    include /etc/letsencrypt/options - ssl - nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl - dhparams.pem;

    # 其他配置,与 HTTP 服务器块类似,但可根据需求调整
}

这里的证书路径要根据 Certbot 实际生成的证书路径填写,一般就在上述路径。编辑完成后,按 “Ctrl + X”,然后输入 “Y” 并回车保存退出。


测试与重启 Nginx:在终端输入 “sudo nginx - t” 测试配置文件是否有语法错误。如果显示 “syntax is ok” 等类似成功信息,说明配置无误,接着输入 “sudo systemctl restart nginx” 重启 Nginx 服务,使新配置生效。


(二)Apache 配置


备份原配置文件:同样先备份 Apache 的配置文件,输入 “sudo cp /etc/apache2/sites - available/000 - default.conf/etc/apache2/sites - available/000 - default.conf.backup”。


编辑配置文件:用文本编辑器打开配置文件 “sudo nano /etc/apache2/sites - available/000 - default.conf”。在文件开头添加以下内容:


<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    Include /etc/letsencrypt/options - ssl - apache.conf
    # 其他配置,与原配置文件结合,可按需调整
</VirtualHost>


注意证书路径要准确,编辑好后按 “Ctrl + X”,输入 “Y” 回车保存退出。


启用 SSL 模块与重启 Apache:在终端依次输入 “sudo a2enmod ssl” 和 “sudo a2ensite 000 - default.conf” 启用 SSL 模块和新配置的站点,然后输入 “sudo systemctl restart apache2” 重启 Apache 服务,使 HTTPS 配置生效。


四、验证 HTTPS 安装


打开浏览器,输入您的网站域名,如 “https://example.com”,如果能正常打开且浏览器地址栏显示安全锁标志,说明 HTTPS 已成功安装。若出现问题,可检查上述步骤是否操作正确,或者查看服务器的日志文件(Nginx 日志一般在 “/var/log/nginx/” 目录,Apache 日志在 “/var/log/apache2/” 目录),根据日志提示解决问题。


通过以上步骤,您就成功在 VPS 上利用 Let’s Encrypt 与 Nginx 或 Apache 免费安装了 HTTPS,为您的网站增添了安全保障。希望本教程对您有所帮助,让您的网站在安全的网络环境中更好地运行。如果你想学习更多网络知识,请前往本站的网络知识专区学习。

📖 相关说明

本文内容围绕云服务器、VPS、建站环境配置等技术展开, 适用于开发者、跨境电商及网站搭建用户参考学习。

我们会持续更新服务器选择、网络优化及建站实战经验,帮助用户快速上手云计算环境。