ubuntu 本地生成被浏览器信任的证书

vhosts添加https证书两步:

1:生成证书:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt

关于参数:node

  • openssl: 基本命令行工具,用于建立并管理OpenSSL证书、密钥与其它文件。
  • req: 此子命令指定咱们但愿使用X.509证书签名请求(简称CSR)管理。其中X.509是一项公钥基础设施标准,SSL与TLS将其指定为密钥与证书管理机制。
  • -x509: 进一步修改前一条子命令,告知工具咱们须要的是自签名证书,而非生成普通的证书签名请求。
  • -nodes: 告知OpenSSL跳过利用密码保护证书的选项。咱们的Apache须要在服务器启动时可以在不经用户许可的状况下读取该文件,而密码会影响到这一流程。
  • -days 365: 此选项选定证书的有效时限。咱们将其设定为一年。
  • -newkey rsa:2048: 指定咱们但愿同时生成一份新证书与新密钥。咱们在上一步设定中无需建立密钥便可签署证书,所以这里须要将密钥与证书一同建立。其中rsa:2048部分指定生成RSA密钥,长度为2048位。
  • -keyout: 此行告知OpenSSL将生成的密钥文件保存在何处。
  • -out: 告知OpenSSL将咱们建立的证书保存在何处。

参考:https://blog.csdn.net/zstack_org/article/details/70048745linux

 

2:添加带证书的虚拟主机:

sudo lnmp ssl add

响应:nginx

+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              https://lnmp.org             |
+-------------------------------------------+
Please enter domain(example: www.lnmp.org): localhost
 Your domain: localhost
Enter more domain name(example: lnmp.org *.lnmp.org): 
Please enter the directory for domain localhost: /home/wwwroot/www
Allow access log? (y/n) y
Enter access log filename(Default:localhost.log): 
You access log filename: localhost.log
Please enter Administrator Email Address: ××××@163.com
Server Administrator Email:××××@163.com
1: Use your own SSL Certificate and Key
2: Use Let's Encrypt to create SSL Certificate and Key
Enter 1 or 2: 1
Please enter full path to SSL Certificate file: /etc/ssl/certs/apache-selfsigned.crt
Please enter full path to SSL Certificate Key file: /etc/ssl/private/apache-selfsigned.key
Please enter full path to SSL Chain file: 
SSL Chain file will not set.Test Apache configure file...

 

访问localhost

 

 

重启电脑apache 没法访问,可经过命令进行查看错误。git

httpd -k start
AH00112: Warning: DocumentRoot [/home/wwwroot/default] does not exist
AH00526: Syntax error on line 23 of /usr/local/apache/conf/vhost/localhost.conf:
SSLCertificateKeyFile: file '/etc/ssl/private/apache-selfsigned.key' does not exist or is empty

解决:github

/home/wwwroot/default 目录不存在,vhost若是没有指定path,在/usr/local/apache/conf/extra/httpd-vhosts.conf 中修改。
/etc/ssl/private/apache-selfsigned.key 不存在,是由于www-data 组没有读取/etc/ssl的全选。apache

sudo chmod -755 /etc/ssl/*

 

 

生成被浏览器信任的证书

 

自签证书很简单,但生成的不被浏览器承认,在地址栏显示为 “不安全”。浏览器

有一个开源工具,mkcert  ,由google Go 团队开发,生成可被浏览器信任的证书。原理是生成证书的同时生成本身的证书颁发机构CA, 在系统里插入一个本身的 CA root,信任这个 CA root,而后用这个根生成 SAN 证书。安全

 github地址: https://github.com/FiloSottile/mkcert服务器

可经过项目github的Releases打包文件直接运行生成;app

./mkcert-v1.3.0-linux-amd64 

是步骤帮助说明:

Using the local CA at "/home/×××/.local/share/mkcert" ✨
Usage of mkcert:

    $ mkcert -install
    Install the local CA in the system trust store.

    $ mkcert example.org
    Generate "example.org.pem" and "example.org-key.pem".

    $ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
    Generate "example.com+4.pem" and "example.com+4-key.pem".

    $ mkcert "*.example.it"
    Generate "_wildcard.example.it.pem" and "_wildcard.example.it-key.pem".

    $ mkcert -uninstall
    Uninstall the local CA (but do not delete it).

For more options, run "mkcert -help".

 

执行 ./mkcert-v1.3.0-linux-amd64 --install 就会安装本地的私有CA,并添加系统信任此CA。

执行 ./mkcert-v1.3.0-linux-amd64 localhost 生成 The certificate is at "./localhost.pem" and the key at "./localhost-key.pem" ✅ 将证书添加nginx或apache便可。

 

参考: https://blog.filippo.io/mkcert-valid-https-certificates-for-localhost/

相关文章
相关标签/搜索