基于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实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是