ubuntu服务器中使用apache创建免费SSL证书

avatar
作者
猴君
阅读量:2

Certbot 是一个用于自动化获取和安装 Let’s Encrypt SSL 证书的工具。以下是使用 certbot certonly 命令生成 SSL 证书的基本步骤:

安装Certbot:

  1. 对于基于Debian的系统(如Ubuntu),可以使用以下命令安装:
sudo apt-get update sudo apt-get install certbot 
  1. 安装Apache插件
    Certbot 提供了一个 Apache 插件,可以帮助自动配置 Apache 使用 SSL 证书:
sudo apt-get install certbot-apache  # Debian/Ubuntu sudo yum install mod_ssl             # Red Hat/CentOS 
  1. 获取证书
sudo certbot --apache -d xx.xxx.com 
  1. 配置 Apache 使用 SSL 证书
    如果你已经通过 Certbot 成功获取了证书,并且想要配置 Apache 使用这些证书,你需要按照以下步骤操作:
    找到证书文件:
    Certbot 通常将证书文件存放在 /etc/letsencrypt/live/your_domain/ 目录中。对于 xxxx.com,路径将是 /etc/letsencrypt/live/xxxx.com/。

  2. 配置Apache:
    你需要编辑 Apache 的配置文件,以便它指向正确的证书文件。这通常涉及到编辑位于 /etc/apache2/sites-available/ 目录下的虚拟主机文件。

打开你的网站的配置文件,例如 /etc/apache2/sites-available/xxxx.com.conf,并确保你有以下 SSL 相关的配置:

<VirtualHost *:443>     ServerName shjkdt.com     DocumentRoot /var/www/xxxx.com/html      SSLEngine on     SSLCertificateFile /etc/letsencrypt/live/xxxx.com/cert.pem     SSLCertificateKeyFile /etc/letsencrypt/live/xxxx.com/privkey.pem     SSLCertificateChainFile /etc/letsencrypt/live/xxxx.com/chain.pem      # 其他配置... </VirtualHost> 

请根据你的实际文件路径和网站设置调整上述配置。

  1. 启用SSL模块:
    确保 Apache 的 SSL 模块被启用:
sudo a2enmod ssl 
  1. 重启Apache:
    应用更改并重启 Apache:
sudo systemctl restart apache2 
  1. 测试配置:
    使用浏览器或 SSL 测试工具(如 SSL Labs)来检查你的 HTTPS 配置是否正确。

如果执行sudo apt-get install certbot-apache后提示:

 E: Unable to locate package certbot-apache 

请使用以下命令:
Certbot 提供了多种插件,用于不同的服务器和环境。要查看可用的插件,你可以使用以下命令:

这将列出所有可用的Certbot插件。 apt-cache search certbot | grep -i plugin 

以下截图是我可用的Certbot插件
在这里插入图片描述

使用正确的插件
如果你的系统是Debian或Ubuntu,通常 certbot-apache 包是可用的。如果你在更新软件包列表后仍然找不到该包,可能需要安装 python3-certbot-apache 包,这是一个提供 Apache 插件的Python 3版本:

sudo apt-get install python3-certbot-apache 
  1. 手动配置(如果第二步成功了的话,可直接跳过到)
    如果以上步骤都不起作用,你可能需要手动配置 Apache 以使用 Certbot 生成的证书。首先,使用 certbot 命令获取证书,不指定任何插件:
sudo certbot certonly 

在提示选择验证方式时,你可以选择 “Standalone” 模式,这不需要 Web 服务器的配合。获取证书后,你需要手动编辑 Apache 配置文件,指向 Certbot 生成的证书文件。证书文件通常位于 /etc/letsencrypt/live/your_domain/ 目录中。

6.检查软件源
如果你仍然遇到问题,可能需要检查你的软件源列表。确保你有正确的软件源,并且它们是最新的。你可以查看 /etc/apt/sources.list 文件和 /etc/apt/sources.list.d/ 目录下的文件来检查你的软件源设置。

7.重启服务
在安装了正确的Certbot插件并配置了 Apache 之后,重启 Apache 服务以应用更改:

sudo systemctl restart apache2 

如果你按照上述步骤操作,Apache 应该能够正确地使用 Certbot 生成的 SSL 证书。如果你遇到任何问题,请检查 Apache 的错误日志文件,通常位于 /var/log/apache2/error.log,它可能会提供导致问题的错误信息。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!