ubuntu

Ubuntu Apache怎样配置SSL证书

小樊
36
2025-06-11 19:44:01
栏目: 云计算

在Ubuntu上配置Apache以使用SSL证书,可以按照以下步骤进行:

1. 安装Apache和SSL模块

首先,确保你已经安装了Apache和SSL模块。你可以使用以下命令来安装它们:

sudo apt update
sudo apt install apache2
sudo apt install apache2-mod_ssl

2. 获取SSL证书

你可以从Let’s Encrypt免费获取SSL证书,或者购买一个商业证书。以下是使用Let’s Encrypt的步骤:

使用Certbot获取证书

Certbot是一个自动化的工具,可以帮助你获取和续订Let’s Encrypt证书。

  1. 安装Certbot:

    sudo apt install certbot python3-certbot-apache
    
  2. 运行Certbot来获取证书:

    sudo certbot --apache
    

    Certbot会引导你完成以下步骤:

    • 选择是否重定向所有HTTP流量到HTTPS。
    • 输入你的域名。
    • 验证你对域名的控制权(通常是通过HTTP-01挑战)。
    • 输入电子邮件地址(用于接收证书续订通知)。
    • 同意条款和条件。

3. 配置Apache虚拟主机

Certbot会自动为你创建一个SSL虚拟主机配置文件。你可以在/etc/apache2/sites-available/目录下找到这个文件,通常命名为yourdomain.com-le-ssl.conf

你可以编辑这个文件来进一步自定义你的SSL配置。例如:

<VirtualHost *:443>
    ServerAdmin webmaster@yourdomain.com
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

4. 启用SSL虚拟主机

确保你的SSL虚拟主机配置文件已启用:

sudo a2ensite yourdomain.com-le-ssl.conf

然后重新加载Apache服务以应用更改:

sudo systemctl reload apache2

5. 测试SSL配置

打开浏览器并访问你的域名,确保它通过HTTPS提供服务,并且浏览器显示安全锁标志。

6. 自动续订证书

Let’s Encrypt证书通常每90天过期一次。Certbot会自动处理续订过程,但你需要确保cron任务已设置:

sudo systemctl status certbot.timer

如果没有运行,可以启动并启用它:

sudo systemctl start certbot.timer
sudo systemctl enable certbot.timer

通过以上步骤,你应该能够在Ubuntu上成功配置Apache以使用SSL证书。

0
看了该问题的人还看了