Apache2 Ubuntu如何配置SSL

472
2025/3/3 3:32:11
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Ubuntu上配置Apache2以使用SSL,您需要遵循以下步骤:

  1. 安装Apache2和SSL模块: 打开终端并运行以下命令来安装Apache2和SSL模块:

    sudo apt update
    sudo apt install apache2
    sudo a2enmod ssl
    
  2. 获取SSL证书: 您可以从Let’s Encrypt免费获取SSL证书。要安装Certbot并获取证书,请运行以下命令:

    sudo apt install certbot python3-certbot-apache
    sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
    

    yourdomain.comwww.yourdomain.com替换为您的实际域名。Certbot将自动配置Apache2以使用SSL证书。

  3. 重定向HTTP到HTTPS(可选): 如果您希望将所有HTTP流量重定向到HTTPS,可以使用以下命令创建一个新的Apache配置文件:

    sudo nano /etc/apache2/sites-available/redirect-to-https.conf
    

    将以下内容粘贴到新文件中,将yourdomain.com替换为您的实际域名:

    <VirtualHost *:80>
        ServerName yourdomain.com
        ServerAlias www.yourdomain.com
        Redirect permanent / https://yourdomain.com/
    </VirtualHost>
    

    保存并关闭文件。然后运行以下命令启用新站点并重启Apache2:

    sudo a2ensite redirect-to-https.conf
    sudo systemctl restart apache2
    
  4. 测试SSL配置: 在浏览器中访问https://yourdomain.com,您应该看到一个绿色的锁图标,表示您的网站已成功配置SSL。

  5. (可选)自动更新证书: Certbot可以自动更新您的SSL证书。要设置自动更新,请运行以下命令:

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

现在,您已经成功在Ubuntu上配置了Apache2以使用SSL。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: ubuntu中docker-compose的用法是什么