使用acme.sh创建和配置https证书
acme.sh很厉害,生成https证书的过程很简单,而且可以自动更新。一、生成过程
1、安装acme.sh(安装不了就自己百度)
curl https://get.acme.sh | sh
alias acme.sh=~/.acme.sh/acme.sh
echo 'alias acme.sh=~/.acme.sh/acme.sh' >>/etc/profile
2、生成证书
acme.sh --issue -d yizuotu.com --webroot /wwwroot/yizuotu.com
注意:
a、webroot 一定是通过yizuotu.com的域名能直接访问的目录,
比如说我用的thinkphp的框架,这个地方的目录需要配置成:/wwwroot/yizuotu.com/public
b、生成之前,如果原来的https已经失效了,或者过期了,要去掉nginx或者apache里面已经配置的https强制跳转和端口
(去掉后,要重载apache和nginx)。
两个配置都在conf/vhost里面,apache还有用.hataccess进行强制跳转的配置。
c、理论上,上面两步做了,只要网络正常,域名是解析到对应的目录的,那么应该是能正常生成的,如果报错,不要不停的执行,一个域名限制一个小时只能请求5次,无论是成功还是失败。
3、安装证书,生成好的证书路径,不建议直接使用,需要安装到nginx和apache统一调用的路径,方便配置nginx和apache
nginx: 红色是安装后的路径,acme会自动把生成好的证书安装到红色路径
acme.sh --install-cert -d zzzyk.com \
--key-file /usr/local/nginx/ssl_cert/zzzyk.com/zzzyk.com.key \
--fullchain-file /usr/local/nginx/ssl_cert/zzzyk.com/zzzyk.com.cer \
--reloadcmd "service nginx force-reload"
apache :同上
acme.sh --install-cert -d chandao.test.com \
--cert-file /path/zzzyk.com.key \
--key-file /path/key.pem \
--fullchain-file /path/zzzyk.com.cer \
--reloadcmd "service apache2 force-reload"
最后一个reloadcmd命令可有可无,自己手动重载是一样的
4、配置到nginx和apache
nginx: 参考,不会的话,百度把,挺简单的
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /etc/letsencrypt/live/www.xxxxx.com/4067022_www.xxxxx.com.pem;
ssl_certificate_key /etc/letsencrypt/live/www.xxxxx.com/4067022_www.xxxxx.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
apache:参考
<VirtualHost *:443>
ServerAdmin admin@example.com
DocumentRoot "/mnt/20180914.xxxxx.danews.cc/public"
ServerName www.xxxxx.com
ServerAlias xxxxxx.com
SSLEngine on
SSLCertificateFile "/usr/local/apache/conf/ssl/www.xxxxx.com/public.crt"
SSLCertificateKeyFile "/usr/local/apache/conf/ssl/www.xxxxx.com/key.key"
SSLCertificateChainFile "/usr/local/apache/conf/ssl/www.xxxxx.com/chain.crt"
ErrorLog "/data/wwwlogs/error_apache.log"
CustomLog "/dev/null" common
<Directory "/mnt/20180914.xxxxxx.xxxxx.cc/public">
SetOutputFilter DEFLATE
Options FollowSymLinks ExecCGI
Require all granted
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
配置后,重载就好了。
到此就配置完了,acme.sh会自动更新,如果更新失败,就要手动排查了。
附:有一种报错,提示无法访问acme的git仓库,这个时候,更新一下acme.sh的脚本即可,执行命令:
acme.sh --upgrade --auto-upgrade