温馨提示×

Ubuntu Apache2配置SSL步骤

小樊
54
2025-05-22 12:48:12
栏目: 云计算

在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工具的步骤:

  • 安装Certbot和Apache插件:

    sudo apt install certbot python3-certbot-apache 
  • 获取并安装证书:

    sudo certbot --apache -d yourdomain.com -d www.yourdomain.com 

    在提示时,选择是否重定向HTTP流量到HTTPS。Certbot会自动配置Apache2以使用SSL证书。

3. 配置Apache2以使用SSL证书

Certbot会自动创建一个SSL配置文件,通常位于 /etc/apache2/sites-available/yourdomain.com-le-ssl.conf。你可以编辑这个文件来进一步自定义你的SSL配置。

  • 打开配置文件:

    sudo nano /etc/apache2/sites-available/yourdomain.com-le-ssl.conf 
  • 确保配置文件包含以下内容:

    <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 </VirtualHost> 
  • 保存并关闭文件。

4. 启用SSL站点

启用新创建的SSL站点配置:

sudo a2ensite yourdomain.com-le-ssl.conf 

禁用默认的HTTP站点(可选):

sudo a2dissite 000-default.conf 

5. 重启Apache2

重启Apache2以应用更改:

sudo systemctl restart apache2 

6. 测试SSL配置

打开浏览器并访问 https://yourdomain.com,你应该能够看到一个安全的连接(浏览器地址栏会显示锁形图标)。

7. 自动续期证书

Let’s Encrypt证书通常有效期为90天。Certbot可以自动续期证书。你可以设置一个cron任务来自动执行续期:

sudo crontab -e 

添加以下行:

0 0,12 * * * certbot renew --post-hook "systemctl reload apache2" 

保存并关闭文件。

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

0