acme.sh的nginx模式生成证书测试

版权声明:如需转载,请注明作者及出处 https://blog.csdn.net/qq_33317586/article/details/85214467

参考:https://github.com/Neilpang/acme.sh/wiki/说明

准备材料:

备案的域名一个:uscwifi.com.cn

带公网的服务器一台


优势:

1.简单,方便。一条命令就搞定。

2.会自动续期,每60天好像

缺点:

1.必须在服务器上进行,虚拟机不行

2.进行操作的服务器为自己的网站服务器

3.生成的证书不是通配符证书

适用于:

小型博客网站,一个服务器就跑一两个网站,基本不需要通配证书。如果公司里面一个主域名下面有大量的二级域名用于测试或者线上用途,随时有可能在某台测试机上测试,则使用通配证书。

例如:公司要搭建gitlab做代码仓库。gitlab会一直跑在该服务器上,所以使用nginx模式(或者指定webroot目录)生成证书就行,并且它会自动续期,此处不需要通配证书。

例如:开发人员需要某域名的证书文件。此时应使用通配证书,且最好是买的证书,因为开发部门可能需要将证书应用于各种线上测试环境,使用免费通配证书每3个月就得续签(开发不可能没事找运维换证书)。


先搭建好一个简单的web服务:

nginx配置文件:

root@cert-ssl:~# cat /etc/nginx/conf.d/uscwifi.com.cn.conf 
server {
      	listen 80 ;
      	server_name test.uscwifi.com.cn;
	root /var/www/uscwifi.com.cn;
	index index.html;
}

网站目录:

root@cert-ssl:~# ls /var/www/uscwifi.com.cn/
index.html
root@cert-ssl:~# cat /var/www/uscwifi.com.cn/index.html

DNS解析(IP打码):

reload

root@cert-ssl:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@cert-ssl:~# nginx -s reload

效果:


使用acme.sh的nginx生成证书

安装参考:https://github.com/Neilpang/acme.sh

错误的使用:指定的域名不是配置文件中指定的:

root@cert-ssl:~# acme.sh --issue --nginx -d uscwifi.com.cn
[Sat Dec 22 18:10:08 CST 2018] Creating domain key
[Sat Dec 22 18:10:08 CST 2018] The domain key is here: /root/.acme.sh/uscwifi.com.cn/uscwifi.com.cn.key
[Sat Dec 22 18:10:08 CST 2018] Single domain='uscwifi.com.cn'
[Sat Dec 22 18:10:08 CST 2018] Getting domain auth token for each domain
[Sat Dec 22 18:10:08 CST 2018] Getting webroot for domain='uscwifi.com.cn'
[Sat Dec 22 18:10:08 CST 2018] Getting new-authz for domain='uscwifi.com.cn'
[Sat Dec 22 18:10:12 CST 2018] The new-authz request is ok.
[Sat Dec 22 18:10:12 CST 2018] Verifying:uscwifi.com.cn
[Sat Dec 22 18:10:12 CST 2018] Nginx mode for domain:uscwifi.com.cn
[Sat Dec 22 18:10:12 CST 2018] Can not find conf file for domain uscwifi.com.cn
[Sat Dec 22 18:10:13 CST 2018] Please check log file for more details: /root/.acme.sh/acme.sh.log

正确的使用:指定的域名和配置文件中一样

root@cert-ssl:~# acme.sh --issue --nginx -d test.uscwifi.com.cn
[Sat Dec 22 18:10:52 CST 2018] Creating domain key
[Sat Dec 22 18:10:52 CST 2018] The domain key is here: /root/.acme.sh/test.uscwifi.com.cn/test.uscwifi.com.cn.key
[Sat Dec 22 18:10:52 CST 2018] Single domain='test.uscwifi.com.cn'
[Sat Dec 22 18:10:52 CST 2018] Getting domain auth token for each domain
[Sat Dec 22 18:10:52 CST 2018] Getting webroot for domain='test.uscwifi.com.cn'
[Sat Dec 22 18:10:52 CST 2018] Getting new-authz for domain='test.uscwifi.com.cn'
[Sat Dec 22 18:10:55 CST 2018] The new-authz request is ok.
[Sat Dec 22 18:10:55 CST 2018] Verifying:test.uscwifi.com.cn
[Sat Dec 22 18:10:55 CST 2018] Nginx mode for domain:test.uscwifi.com.cn
[Sat Dec 22 18:10:56 CST 2018] Found conf file: /etc/nginx/conf.d/uscwifi.com.cn.conf
[Sat Dec 22 18:10:56 CST 2018] Backup /etc/nginx/conf.d/uscwifi.com.cn.conf to /root/.acme.sh/test.uscwifi.com.cn/backup/test.uscwifi.com.cn.nginx.conf
[Sat Dec 22 18:10:56 CST 2018] Check the nginx conf before setting up.
[Sat Dec 22 18:10:56 CST 2018] OK, Set up nginx config file
[Sat Dec 22 18:10:56 CST 2018] nginx conf is done, let's check it again.
[Sat Dec 22 18:10:56 CST 2018] Reload nginx
[Sat Dec 22 18:11:02 CST 2018] Pending
[Sat Dec 22 18:11:05 CST 2018] Success
[Sat Dec 22 18:11:05 CST 2018] Restoring from /root/.acme.sh/test.uscwifi.com.cn/backup/test.uscwifi.com.cn.nginx.conf to /etc/nginx/conf.d/uscwifi.com.cn.conf
[Sat Dec 22 18:11:05 CST 2018] Reload nginx
[Sat Dec 22 18:11:05 CST 2018] Verify finished, start to sign.
[Sat Dec 22 18:11:08 CST 2018] Cert success.
-----BEGIN CERTIFICATE-----
......此处省略废话......
-----END CERTIFICATE-----
[Sat Dec 22 18:11:08 CST 2018] Your cert is in  /root/.acme.sh/test.uscwifi.com.cn/test.uscwifi.com.cn.cer 
[Sat Dec 22 18:11:08 CST 2018] Your cert key is in  /root/.acme.sh/test.uscwifi.com.cn/test.uscwifi.com.cn.key 
[Sat Dec 22 18:11:09 CST 2018] The intermediate CA cert is in  /root/.acme.sh/test.uscwifi.com.cn/ca.cer 
[Sat Dec 22 18:11:09 CST 2018] And the full chain certs is there:  /root/.acme.sh/test.uscwifi.com.cn/fullchain.cer 

配置好就可以用了:

复制证书使用(官方建议复制后使用):

root@cert-ssl:~# mkdir /etc/nginx/ssl
root@cert-ssl:~# cp .acme.sh/test.uscwifi.com.cn/fullchain.cer /etc/nginx/ssl/test.uscwifi.com.cn.cer
root@cert-ssl:~# cp .acme.sh/test.uscwifi.com.cn/test.uscwifi.com.cn.key /etc/nginx/ssl/
root@cert-ssl:~# ls /etc/nginx/ssl
test.uscwifi.com.cn.cer  test.uscwifi.com.cn.key

nginx配置:

root@cert-ssl:~# cat /etc/nginx/conf.d/uscwifi.com.cn.conf
server {
      listen 80 ;
      server_name test.uscwifi.com.cn;
      if ($host ~ test.uscwifi.com.cn)
      {
       rewrite ^/(.*)$ https://test.uscwifi.com.cn$request_uri? permanent;
      }
}
server {
       listen 443;
       server_name test.uscwifi.com.cn;
       root /var/www/uscwifi.com.cn;
       index index.html index.htm index.php;
       ssl on ;
       ssl_certificate /etc/nginx/ssl/test.uscwifi.com.cn.cer;
       ssl_certificate_key /etc/nginx/ssl/test.uscwifi.com.cn.key;
       ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
       ssl_prefer_server_ciphers on;
       ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
 
       location ~ \.(gif|jpg|jpeg|png|css|js|ico)$ {
 
       }
 
        location ~ /.git{
            deny all;
        }
}
 

root@cert-ssl:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@cert-ssl:~# nginx -s reload

访问测试:

猜你喜欢

转载自blog.csdn.net/qq_33317586/article/details/85214467