基于Amazon Linux使用pip安装certbot并使用Apache配置证书的完整步骤

2024-06-08 06:44

本文主要是介绍基于Amazon Linux使用pip安装certbot并使用Apache配置证书的完整步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

配置证书

1. 更新系统和安装必要的软件包

首先,确保系统和包管理器是最新的:

sudo dnf update -y
sudo dnf install -y python3 python3-pip python3-virtualenv httpd mod_ssl
2. 创建并激活虚拟环境

为了避免依赖冲突,使用virtualenv创建一个隔离的Python环境:

python3 -m venv myenv
source myenv/bin/activate
3. 安装certbot和Apache插件

在虚拟环境中安装certbot和certbot-apache插件:

pip install certbot certbot-apache
4. 配置Apache

确保Apache已经安装并运行:

sudo systemctl enable httpd --now
5. 使用certbot获取SSL证书

运行certbot命令以获取SSL证书。请将yourdomain.com替换为您的实际域名:

sudo myenv/bin/certbot --apache -d yourdomain.com -d www.yourdomain.com

certbot将自动配置Apache以使用获取的SSL证书。

6. 验证SSL证书

验证SSL证书是否已正确安装:

sudo myenv/bin/certbot certificates
7. 配置自动更新

为了确保SSL证书自动更新,设置一个cron job:

sudo crontab -e

添加以下行来每天检查并更新证书:

0 2 * * * /path/to/myenv/bin/certbot renew --quiet --deploy-hook "systemctl reload httpd"

确保将/path/to/myenv替换为您的虚拟环境的实际路径。

8. 重新启动Apache以应用更改

确保所有更改都已正确应用并重新启动Apache:

sudo systemctl reload httpd
9. 验证自动更新

您可以手动运行以下命令以测试自动更新过程:

sudo /home/ec2-user/myenv/bin/certbot renew --dry-run

这将模拟续订过程而不会实际更改证书,以确保一切正常工作。

报错
(myenv) [ec2-user@ip-172-31-29-84 ~]$ sudo myenv/bin/certbot --apache -d yourdomain.com -d www.yourdomain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The apache plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError('Problem in Augeas installation')
解决Augeas依赖问题

由于Augeas安装问题导致Apache插件无法工作,您可以尝试以下步骤来解决这个问题。

安装Augeas依赖

首先,确保Augeas库已安装在系统上:

sudo dnf install augeas-libs augeas -y

然后再尝试重新运行certbot命令。

确认Augeas库路径

如果Augeas依赖安装后问题依旧,可以尝试手动设置Augeas库路径。

使用–webroot方法生成SSL证书

如果Augeas问题仍然存在,可以尝试使用--webroot方法来获取SSL证书。

确定您的webroot路径

您的webroot路径是您网站文件的根目录,例如/var/www/html。确保您的Apache配置指向该目录。

获取SSL证书

使用以下命令来获取SSL证书:

sudo myenv/bin/certbot certonly --webroot -w /var/www/html -d yourdomain.com -d www.yourdomain.com(myenv) [ec2-user@ip-172-31-24-7 ~]$ sudo myenv/bin/certbot certonly --webroot -w /var/www/html -d xxxxxx.asia -d www.xxxxxx.asia
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)(Enter 'c' to cancel): 邮箱地址- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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
Account registered.
Requesting a certificate for xxxxxx.asia and www.xxxxxx.asiaSaving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for xxxxxx.asia and www.xxxxxx.asiaSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/xxxxxx.asia/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/xxxxxx.asia/privkey.pem
This certificate expires on 2024-09-05.
These files will be updated when the certificate renews.NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
配置Apache以使用SSL证书

获取SSL证书后,您需要手动编辑Apache配置以使用这些证书。

编辑Apache配置

打开您的Apache配置文件,通常位于/etc/httpd/conf.d/ssl.conf或您的虚拟主机配置文件中。

添加或修改以下行以指向您的SSL证书和密钥文件:

<VirtualHost *:443>ServerName yourdomain.comServerAlias www.yourdomain.comDocumentRoot /var/www/htmlSSLEngine onSSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pemSSLCertificateChainFile /etc/letsencrypt/live/yourdomain.com/chain.pem<Directory /var/www/html>Options Indexes FollowSymLinksAllowOverride AllRequire all granted</Directory>
</VirtualHost>

确保将yourdomain.com替换为您的实际域名。

重新加载Apache
sudo systemctl reload httpd
验证SSL证书

您可以使用以下命令来验证证书是否已正确安装:

sudo myenv/bin/certbot certificates
配置自动更新

为了确保SSL证书自动更新,您可以设置一个cron job:

  1. 编辑cron配置

    sudo crontab -e
    
  2. 添加以下行来每天检查并更新证书

    0 2 * * * /home/ec2-user/myenv/bin/certbot renew --quiet --deploy-hook "systemctl reload httpd"
    

确保将/home/ec2-user/myenv替换为您的虚拟环境的实际路径。

10. AWS添加安全组HTTPS端口443

下载证书文件

Certbot生成的证书文件通常存储在 /etc/letsencrypt/live/yourdomain.com/ 目录中,其中 yourdomain.com 是您的实际域名。您可以通过以下步骤下载这些文件:

  1. 找到证书文件路径

    Certbot生成的证书文件通常包括以下几个主要文件:

    • privkey.pem:私钥文件
    • fullchain.pem:完整的证书链文件(包括域名证书和中间证书)
    • cert.pem:域名证书文件
    • chain.pem:中间证书文件

    您可以使用以下命令列出所有证书及其路径:

    sudo myenv/bin/certbot certificates
    
  2. 复制证书文件

    使用 scp(Secure Copy Protocol)或 rsync 等工具将证书文件从服务器复制到您的本地机器。

    例如,使用 scp

    scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/privkey.pem /path/to/local/directory/
    scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/fullchain.pem /path/to/local/directory/
    

    确保将 ec2-user@your-ec2-ip 替换为您的实际服务器用户名和IP地址,将 /path/to/local/directory/ 替换为您本地机器上的目标目录。

  3. 设置适当的权限

    下载到本地机器后,确保这些文件具有适当的权限。特别是私钥文件,应该仅对您自己可读:

    chmod 600 /path/to/local/directory/privkey.pem
    
示例步骤

以下是一个完整的示例,假设您要将证书文件下载到本地目录 /home/user/certificates/

  1. 连接到您的EC2实例

    ssh ec2-user@your-ec2-ip
    
  2. 查找证书文件路径

    sudo myenv/bin/certbot certificates
    
  3. 从服务器复制证书文件到本地机器

    在本地终端上运行:

    scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/privkey.pem /home/user/certificates/
    scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/fullchain.pem /home/user/certificates/
    
  4. 设置适当的权限

    chmod 600 /home/user/certificates/privkey.pem
    

删除证书

  1. 列出所有证书

    首先,您可以列出所有已经管理的证书:

    sudo myenv/bin/certbot certificates
    

    这将显示所有已安装的证书及其详细信息。

  2. 删除特定证书

    使用以下命令删除特定证书:

    sudo myenv/bin/certbot delete --cert-name xxxxxx.asia
    

    确保将 xxxxxx.asia 替换为您想要删除的证书名称。

更新Apache配置

删除证书后,您需要更新Apache配置以移除对已删除证书的引用。

  1. 编辑Apache配置文件

    打开您的Apache配置文件,通常位于 /etc/httpd/conf.d/ssl.conf 或您的虚拟主机配置文件中:

    sudo vim /etc/httpd/conf.d/ssl.conf
    
  2. 移除或修改以下行

    移除或修改指向已删除证书文件的配置行:

    SSLCertificateFile /etc/letsencrypt/live/xxxxxx.asia/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/xxxxxx.asia/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/xxxxxx.asia/chain.pem
    
  3. 重新加载Apache

    保存文件并退出编辑器,然后重新加载Apache:

    sudo systemctl reload httpd
    
验证删除

确保证书已成功删除,您可以再次使用以下命令确认:

sudo myenv/bin/certbot certificates

确认已删除的证书不再列出。

这篇关于基于Amazon Linux使用pip安装certbot并使用Apache配置证书的完整步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041444

相关文章

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

PyCharm 接入 DeepSeek最新完整教程

《PyCharm接入DeepSeek最新完整教程》文章介绍了DeepSeek-V3模型的性能提升以及如何在PyCharm中接入和使用DeepSeek进行代码开发,本文通过图文并茂的形式给大家介绍的... 目录DeepSeek-V3效果演示创建API Key在PyCharm中下载Continue插件配置Con

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API