Network: 申请免费数字证书以及强制使用https访问

为了避免网页被劫持而显示一些不可预测的内容(广告:貌似国内的网络运营商经常会干这种事情,你还必须打电话去让他们停止这种行为,但他们当时还不承认或者是美名其曰增值服务)。

难道国外也有这样的问题(https://css-tricks.com/moving-to-https-on-wordpress/):

  • The site as it is intended. I’ve heard of examples like hotel WiFi system and even ISPs that mess with HTTP traffic and do things like insert their own advertising code. Can’t do that over HTTPS.

开始加入Google AdSense, 在网站上投放一些广告,希望能够审批通过。当然更是希望能够盈利啦。

  • Let’s Encrypt

从这里开始:https://letsencrypt.org/getting-started/

不需要注册, 只需要在http服务器上执行官网提供的客户端工具,就可以进行相关环境的配置,生成apache所需要的数字证书

1. 安装:

# mkdir -pv ~/bin && cd ~/bin
# wget https://dl.eff.org/certbot-auto
# chmod a+x certbot-auto

2. 生成证书(http服务器使用的是Apache)

# ~/bin/certbot-auto --apache

所需的工具更新之后 会进入交互式的文本界面,选择所需要使用https进行访问的域名。

NOTE: 这里可能需要翻墙。

3. 使用cron命令自动更新数字证书

由于其所生成的数字证书只有90天,所以需要在这规定的时间内再通过cron对证书进行更新:

# echo << EOF > /etc/cron.weekly/certboot-renew
#! /bin/bash
~/bin/certbot-auto -q renew
EOF
# chmod +x /etc/cron.weekly/certboot-renew

https://certbot.eff.org/docs/using.html#renewal

since certificates are only renewed when they’re determined to be near expiry, the command can run on a regular basis, like every week or every day.

  • StartSSL免费SSL证书

StartSSL可以提供有效期为1年的免费数字证书,这个证书可以包含5个域名

申请数字证书时需要先进行注册,使用openssl命令生成csr文件

将csr文件上传到官网上,很快就可以得到我们所需要的证书文件:

由于本网站使用的是的ubuntu 14.04系统,由apache提供http(https)服务:

2016_06_25_network_ssl

  • 强制使用https访问

可以看到本网站现在已经在使https服务了并且通过http的访问会自动跳转到https。

本网站当时所使用的是StartSSL使用的证书,证书存放在云服务器的/etc/brobwind.com/目录下。

Apache服务相关的配置如下:

1. /etc/apache2/sites-enabled/000-default.conf文件:

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

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2. /etc/apache2/sites-enable/000-default-le-ssl.conf文件

由名字可以看出这个文件其实是由certbot-auto命令生成的,为什么要选用StartSSL? 貌似有些StartSSL用户得知StartSSL与国内的Qihu 360有关转而使用Let’s Encrypt提供的证书了。

<VirtualHost _default_:443>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/blog
    ServerName brobwind.com
    ServerAlias www.brobwind.com

    ErrorLog ${APACHE_LOG_DIR}/error.log

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuit ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL
    SSLCertificateFile /etc/brobwind.com/2_www.brobwind.com.crt
    SSLCertificateKeyFile /etc/brobwind.com/brobwind.com.key
    SSLCertificateChainFile /etc/brobwind.com/1_root_bundle.crt
    CustomLog ${APACHE_LOG_DIR}/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

使用了数字证书之后,如果使用的是Chrome浏览器,可以看到在地址栏的最左边有一个锁的标志,意味着当前的整个网页都是通过ssl获取的。当然,如果没有出现锁的标志,那就意味的当前网页的内容存在从http获取的,可以通过Chome的developer tools查看哪些资源不是通过https获取的(Mixed Content):

2016_06_26_ssl_mixed_content

NOTE: 可以看到在developer tools中的Security那一栏,最后显示Mixed Content – The site includes HTTP resources. 这就需要我们将页面中存在的http链接替换成https的链接。

在wordpress中资源链接所使用的http协议,相关的trace id在这里:

1. https://core.trac.wordpress.org/ticket/25449

2. https://crowdfavorite.com/avoid-ssl-mixed-content-errors-in-wordpress/

  • 相关的参考文档:
  1. https://letsencrypt.org/
  2. https://certbot.eff.org/
  3. https://www.startssl.com/
  4. https://managewp.com/wordpress-ssl-settings-and-how-to-resolve-mixed-content-warnings

 

发表评论

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