ubuntu

ubuntu apache2 SSL证书配置教程

小樊
35
2025-06-30 01:56:20
栏目: 云计算

在Ubuntu系统上为Apache2配置SSL证书的步骤如下:

1. 安装Apache2和SSL模块

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

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

2. 获取SSL证书

你可以选择以下几种方式获取SSL证书:

3. 配置Apache2使用SSL证书

/etc/apache2/sites-available/ 目录下找到默认的SSL配置文件 default-ssl.conf,使用文本编辑器打开它。修改以下参数:

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

示例配置:

<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    ServerName example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    <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 default-ssl

5. 重启Apache2

重启Apache2以应用更改:

sudo systemctl restart apache2

6. 验证SSL配置

打开浏览器,访问你的网站(例如 https://example.com),如果一切正常,你应该会看到浏览器地址栏中的锁图标,表示SSL证书已正确安装。

7. 可选:设置HTTP重定向为HTTPS

为了确保所有HTTP请求都被重定向到HTTPS,你可以在Apache配置文件中添加重定向规则。编辑 /etc/apache2/sites-available/000-default.conf 文件,在 VirtualHost *:80 标签内添加以下内容:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

完成后,重启Apache服务以使更改生效:

sudo systemctl restart apache2

完成这些步骤后,你的Ubuntu服务器应该已经成功配置了SSL for Apache2。

0
看了该问题的人还看了