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

相关文章

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

在React中引入Tailwind CSS的完整指南

《在React中引入TailwindCSS的完整指南》在现代前端开发中,使用UI库可以显著提高开发效率,TailwindCSS是一个功能类优先的CSS框架,本文将详细介绍如何在Reac... 目录前言一、Tailwind css 简介二、创建 React 项目使用 Create React App 创建项目

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.