apach2站点配置SSL及non-www自动跳转到www子域


这个wordpress站点已经有几年没有没有打理了,整理资料,发现wordpress系统升级到6.0,然后系统检测提示,未使用ssl。

so, just do it~

那么,这个事情分几步:

  • 为域名申请SSL证书
  • 配置web server使用证书
  • http请求自动跳转到https

为域名申请SSL证书:目前已经非常成熟和便捷,推荐大家使用DNSPOD(腾讯云)或者阿里云提供的免费SSL证书,我在使用DNSPOD的解析服务,所以在DNSPOD申请了亚洲诚信的免费证书

总共分为5步,需要验证域名

在dnspod的好处就是自动验证了(自动解析到了验证地址),证书签发很快,一个工作日内一定收到了。

证书到手

配置web server使用证书:这里需要注意自己使用的webserver,选择自己的webserver适用的证书下载,我这里是apache,so下载了Apache的版本

有了证书压缩包,直接可以参考dnspod提供的帮助配置,点击这里

我这里简单讲把大象装进冰箱分为三小步:

  • 打开冰箱门-开启apache服务器的mod_ssl模组
  • 把大象塞进去-复制下载的压缩包里面的证书链文件、私钥文件、证书文件三个文件到Apache server的服务器,放在任何地方都可以,记住路径
  • 关闭冰箱门并开启冷冻模式-修改对应站点的VirtualHost配置文件,调整端口、开启ssl、指定证书路径
a2enmod ssl
systemctl reload apache2.service
scp nishizhen.cn_apache.zip [email protected]:~/
ssh [email protected]
unzip nishizhen.cn_apache.zip
mv nishizhen.cn_apache /etc/apache2/ssl
<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nishizhen-cn
        ServerName nishizhen.cn
        ServerAlias www.nishizhen.cn
        ErrorLog ${APACHE_LOG_DIR}/www-nishizhen.cn-error.log
        CustomLog ${APACHE_LOG_DIR}/www-nishizhen.cn-access.log combined
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nishizhen-cn
        ServerName nishizhen.cn
        ServerAlias www.nishizhen.cn
        ErrorLog ${APACHE_LOG_DIR}/www-nishizhen.cn-error.log
        CustomLog ${APACHE_LOG_DIR}/www-nishizhen.cn-access.log combined

        #enable ssl
        SSLEngine on
        # crt path
        SSLCertificateFile /etc/apache2/ssl/nishizhen.cn.crt
        # private key path
        SSLCertificateKeyFile /etc/apache2/ssl/nishizhen.cn.key
        # root bundle crt path
        SSLCertificateChainFile /etc/apache2/ssl/root_bundle.crt
</VirtualHost>
systemctl reload apache2.service

OK,可以访问https://www.nishizhen.cn啦~

http请求自动跳转到https

开启rewrite模组,然后修改virtualhost配置,增加跳转

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nishizhen-cn
        ServerName nishizhen.cn
        ServerAlias www.nishizhen.cn
        ErrorLog ${APACHE_LOG_DIR}/www-nishizhen.cn-error.log
        CustomLog ${APACHE_LOG_DIR}/www-nishizhen.cn-access.log combined

        # auto redirect 80 to 443
        RewriteEngine on
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nishizhen-cn
        ServerName nishizhen.cn
        ServerAlias www.nishizhen.cn
        ErrorLog ${APACHE_LOG_DIR}/www-nishizhen.cn-error.log
        CustomLog ${APACHE_LOG_DIR}/www-nishizhen.cn-access.log combined

        #enable ssl
        SSLEngine on
        # crt path
        SSLCertificateFile /etc/apache2/ssl/nishizhen.cn.crt
        # private key path
        SSLCertificateKeyFile /etc/apache2/ssl/nishizhen.cn.key
        # root bundle crt path
        SSLCertificateChainFile /etc/apache2/ssl/root_bundle.crt
</VirtualHost>

搞定,自动跳转http到https啦

后记:这时,出现了一个有趣的问题:

https://nishizhen.cn可以跳转到https://www.nishizhen.cn

https://www.nishizhen.cn可以跳转到https://www.nishizhen.cn

但是http://nishizhen.cn不会跳转到https://nishizhen.cn并提示Forbidden,当然,这个分析过去就知道,这个不是这次配置证书的问题,这个是一直存在的问题,apache配置的站点里面有另外一个默认的站点没有配置域名,导致的会访问到那个站点,并提示forbidden,直接关闭那个站点的配置,使http://nishizhen.cn匹配到上面代码中的virtualhost配置,恢复正常~~


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

验证码 * Time limit is exhausted. Please reload CAPTCHA.