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/

相關文章
相關標籤/搜索