博客已经写了4年了,新买了新的服务器今天做了迁移,数据库和网站都放到了新的服务器上,想着顺手把https配置上吧,现在详细记录配置过程
首先,我为我博客申请的https证书是免费的,时效是90天,当然,我们可以免费renew,也就是说只要我记得在到期之前记得重新申请并且配置,我可以一直用下去。而且renew非常简单,一个条命令,全自动帮你配置好
我的环境:
centos 7 + apache + Python 2.7.5
使用的工具是:certbot
工具地址:
https://certbot.eff.org/lets-encrypt/centosrhel7-apache
首先按照工具地址中的选择好的centos7/rhel7 +apache 安装相应的包
1 |
yum install python2-certbot-apache |
按照道理说下边就应该是一条命令的事情了,按照教程来说
1 |
certbot --apache |
但是,这只是噩梦的开始…..
这尼玛教程根本不好用!!!!!,好吧,无数次尝试后如下命令可能会帮助你:
1 2 3 4 5 |
pip uninstall urllib3 pip uninstall requests pip uninstall chardet pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3 |
这个是保证你的版本是certbot里的版本是要求的版本
1 |
yum install python-certbot-apache |
这个是安装对应的apache插件官网文档中的python2-certbot-apache貌似不太好用
然后执行:
1 |
certbot --apache |
剩下的基本就是全自动了,需要你输入你的邮箱用来紧急情况的renew
然后让你选择域名:这个地方需要注意,首先你的apache里的网站需要是配置好的,也就是说80端口网站访问正常没问题后才能来走这个过程(直接回车选中的就是所有的域名)
然后回问你一下自动跳转的问题,就是自动把http的请求跳转到Https
然后就结束了(certbot帮你把配置文件修改好了,httpd服务都重启了)
然后,我们需要测试下renew好用不:
1 |
certbot renew --dry-run |
一般情况下都是好用的
然后加入cronjob ,完工
1 2 3 |
crontab -e 0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew |
————
补充
弄完这些后你可能发现一些页面显示混乱,是因为其中的图像,样式表css,js等文件的链接仍然是http,不是https
报错信息一般为:Mixed Content(可以通过chrome的开发者调试工具看到)
你需要修改如下地方:
1:网站设置中,将域名修改为https://www.503error.com(例子)
2:修改数据库中的链接为https,此步骤可以使用插件Better Search Replace,注意要去掉最下边的勾选(选中的情况下是只查询,不替换)
另外,如果你是aws elb上放证书,然后服务器上用的80,你很可能会遇到无限redrect的问题
原因就是,elb发过去的请求,wordpress检测到不是Https,但是你网站写了https,他就会无限给你转到https,可以在
.htaccess 中加入如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
php_value max_input_nesting_level 256 php_value max_input_time 300 php_value max_input_vars 3000 php_value max_execution_time 300 #php_value post_max_size 64M ## make sure wordpress knows if we are on https SetEnvIfNoCase X-FORWARDED-PROTO "^https$" HTTPS SetEnvIfNoCase X-FORWARDED-PROTOCOL "^https$" HTTPS ## Rewrite everything to HTTPS RewriteEngine on RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] ## Add a header to all requests explaining that we should always try to use HTTPS, ## even if the original request is over HTTP. This is to prevent any mixed content warnings ## See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests Header set Content-Security-Policy "upgrade-insecure-requests" |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021