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

相关文章

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

Win安装MySQL8全过程

《Win安装MySQL8全过程》:本文主要介绍Win安装MySQL8全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Win安装mysql81、下载MySQL2、解压文件3、新建文件夹data,用于保存数据库数据文件4、在mysql根目录下新建文件my.ini

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删