树莓派/Debian Apache2 安装腾讯云 SSL 证书

前言

前文 树莓派/Debian Apache2 实现 HTTPS(SSL) 服务 提到,Apache2 实现 HTTPS(SSL) 服务有两种方法以及之间的区别,这里讲述如何通过 腾讯云 申请免费 SSL 证书实现 HTTPS(SSL) 服务。

注:本来打算使用阿里云的 SSL 证书服务,但是实在太慢了,改用腾讯云。

准备

  • 已有域名且实名

  • 腾讯云账号

  • LAMP 服务器环境

    其他环境(Nginx等)的 SSL 证书同时下发,可以自己搜索配置方法。

申请免费 SSL 证书

  • “腾讯云 -> SSL 证书 -> 申请免费证书”

  • “确认证书类型 -> TRUSTASIA 免费版 DVSSL 证书”

  • 填写免费证书申请表单

  • 域名身份验证

    由于我是阿里云的服务器,我选择“手动DNS验证”,腾讯云的应该可以选择“自动DNS验证”。

  • “温馨提示 -> 查看证书详情”

  • 将证书信息填写到域名解析:“添加记录”

  • 回到腾讯云证书详情页,点击 “自动诊断” 然后 “验证”

  • 然后回邮件通知审核通过

下载免费 SSL 证书

  • 点击颁发下载

  • 将压缩包通过 WinSCP 等软件传输到服务器上

  • 解压压缩包

    腾讯云一般是zip格式的压缩包,输入解压命令即可:

    unzip xxxxx.zip
    

配置 Apache

编辑 SSL 配置文件 default-ssl.conf

  • 编辑 SSL 配置文件 default-ssl.conf

    sudo vim /etc/apache2/sites-enabled/default-ssl.conf
    

    如果忽略注释会显示:

    <IfModule mod_ssl.c>
            <VirtualHost _default_:443>
                    ServerAdmin webmaster@localhost
    
                    DocumentRoot /var/www/html
    
                    SSLEngine on
    
                    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
                    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
    
                    <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                    SSLOptions +StdEnvVars
                    </FilesMatch>
                    <Directory /usr/lib/cgi-bin>
                                    SSLOptions +StdEnvVars
                    </Directory>
                    </VirtualHost>
    </IfModule>
    
  • 添加 ServerName <域名> ,例:

    ServerAdmin webmaster@localhost
    ServerName www.example.com  # 你的域名
    DocumentRoot /var/www/html
    

    修改加载 SSL 证书位置到你解压的证书文件夹 ,例:

    SSLCertificateFile /etc/apache2/ownSSL/Apache/2_www.xxxxx.xxx.crt SSLCertificateKeyFile /etc/apache2/ownSSL/Apache/3_www.xxxxx.xxx.key 
    

    并添加

    SSLCertificateChainFile /etc/apache2/ownSSL/Apache/1_root_bundle.crt
    
  • :wq 保存退出

加载 SSL 配置文件 default-ssl.conf

  • 以 root 权限启用SSL模块:

    sudo a2enmod ssl
    

    如果执行成功显示:

    Considering dependency setenvif for ssl:
    Module setenvif already enabled
    Considering dependency mime for ssl:
    Module mime already enabled
    Considering dependency socache_shmcb for ssl:
    Enabling module socache_shmcb.
    Enabling module ssl.
    See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    
  • 它最后提醒你执行一条重启命令:

    sudo /etc/init.d/apache2 restart
    

    这里要以 root 权限执行这条命令,否则报错。

    注意:这里重启了 Apache2。

  • 以 root 权限执行命令:

    Copysudo a2ensite default-ssl
    

    正确返回结果:

    Copyyogile@debyogile:/etc/apache2# sudo a2ensite default-ssl
    a2ensite default-sslEnabling site default-ssl.
    To activate the new configuration, you need to run:
      systemctl reload apache2
    

    它提醒执行 systemctl reload apache2 ,先不管它,进行下一步 启动 Apache2 服务

  • 这时 启动 Apache2 服务

    sudo /etc/init.d/apache2 start
    

    启动成功显示:

    [ ok ] Starting apache2 (via systemctl): apache2.service.
    
  • 启动 Apache2 服务成功后,加载 SSL 配置文件 default-ssl.conf

    sudo systemctl reload apache2
    

    成功无显示。

重启 Apache2 服务

  • 输入命令重启 Apache2 服务

    Copysudo /etc/init.d/apache2 restart
    

    成功会显示:

    Copy[ ok ] Restarting apache2 (via systemctl): apache2.service.
    
  • 配置成功。

网页登录测试

在浏览器中输入 https://<域名> 即可验证

HTTP 强制重定向 HTTPS

  • 启动重定向

    sudo a2enmod rewrite
    

    重启 Apache 服务

    sudo systemctl restart apache2
    
  • /etc/apache2/sites-enabled/000-default.conf 文件的 <VirtualHost *:80></VirtualHost> 中写入以下内容

    RewriteEngine on
    RewriteCond   %{HTTPS} !=on
    RewriteRule   ^(.*)  https://%{SERVER_NAME}$1 [L,R=301]
    

    重启 Apache 服务

    sudo systemctl restart apache2
    
posted @ 2020-03-20 12:54  Yogile  阅读(1327)  评论(3编辑  收藏  举报