基于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

相关文章

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

linux服务之NIS账户管理服务方式

《linux服务之NIS账户管理服务方式》:本文主要介绍linux服务之NIS账户管理服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、所需要的软件二、服务器配置1、安装 NIS 服务2、设定 NIS 的域名 (NIS domain name)3、修改主

NGINX 配置内网访问的实现步骤

《NGINX配置内网访问的实现步骤》本文主要介绍了NGINX配置内网访问的实现步骤,Nginx的geo模块限制域名访问权限,仅允许内网/办公室IP访问,具有一定的参考价值,感兴趣的可以了解一下... 目录需求1. geo 模块配置2. 访问控制判断3. 错误页面配置4. 一个完整的配置参考文档需求我们有一

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题

《Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题》:本文主要介绍Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录一、前言二、系统架构检测三、卸载旧版 Go四、下载并安装正确版本五、配置环境变量六、验证安装七、常见

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.