HTTP證書申請,設置應用程序服務器使用HTTPS


HTTP證書申請,設置應用程序服務器使用HTTPS

https://certs.godaddy.com/repository/ 根證書和中級證書下載地址(godaddy)

######Godaddy購買證書生成csr文件##########

  1. *.reportide.com一個根域名的證書
openssl req -new -newkey rsa:2048 -nodes -keyout reportide.key -out reportide.csr

Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BeiJing Locality Name (eg, city) [Default City]:BeiJing Organization Name (eg, company) [Default Company Ltd]:reportide Organizational Unit Name (eg, section) []:devops Common Name (eg, your name or your server's hostname) []:*.reportide.com Email Address []:wangyp@me.net Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
  1. san證書:(一個證書綁定多個域名的方式)
openssl req -out reassurepay.csr -newkey rsa:2048 -nodes -keyout reassurepay.key -config san.conf

san.conf 文件內容
[ req ]
default_bits = 2048 distinguished_name = req_distinguished_name req_extensions = req_ext [ req_distinguished_name ] countryName = CN stateOrProvinceName = Beijing localityName = Beijing organizationName = reassurepay commonName = www.reassurepay.com [ req_ext ] subjectAltName = @alt_names [alt_names] DNS.1 = www.reassurepay.com DNS.2 = www1.reassurepay.com DNS.3 = www.safetyboxpay.com

 生成.csr文件,去Godaddy買證書,會得到兩個.crt文件。

  • haproxy 配置ssl證書

Haproxy使用.pem格式證書,所以我們要把證書轉換為.pem格式。
從godaddy下載apache的兩個.crt格式證書,將兩個證書合成一個.crt格式證書

cat cea.crt gd.crt > server.crt

在web服務器上創建證書時會生成一個key,保存為server.key(如果有這個.key文件可以跳過這一步,如果是自己按上面Godaddy購買證書生成csr文件就已經有了這個key文件

注:如果證書已經在iis服務器上使用且之前沒有保存key.可以從iis服務器導出一份證書.server.pfx
把server.pfx證書放入linux系統中執行:(執行前請確定已安裝了openssl)

openssl pkcs12 -in server.pfx -nocerts -nodes -out server.key

導出key server.key

最后把server.crt 和server.key 合成一個.pem格式證書

cat server.crt server.key | tee server.pem

把server.pem放入haproxy指定ssl證書目錄。重啟haproxy

  • tomcat 配置ssl證書

1.從godaddy下載apache證書解壓出兩個.crt格式證書,然后將兩個.crt證書合成一個tomcat.crt證書

cat cea.crt gd.crt > tomcat.crt

2.從godaddy下載根證書和中級證書(https://certs.godaddy.com/repository/ & https://ssl-ccp.godaddy.com/repository/?origin=CALLISTO),然后將中級證書和根證書合並成一個ca.crt證書

cat gdig2_.crt gdroot-g2.crt > ca.crt

3.從godaddy獲取key,如果證書已認證,從安裝服務器上下載key,保存為tomcat.key(如果有這個.key文件可以跳過這一步,如果是自己按上面Godaddy購買證書生成csr文件就已經有了這個key文件

注:如果證書在iis服務器上使用.可以從iis服務器導出一份證書,tidebuy.pfx然后提取key

openssl pkcs12 -in tidebuy.pfx -nocerts -nodes -out tomcat.key

4.密鑰和證書轉換為PKCS12格式的證書

openssl pkcs12 -export -in tomcat.crt -inkey tomcat.key -out tomcat.p12 -name tomcat -chain -CAfile ca.crt

注:(運行命令后,請按照提示輸入一個你可以記得住的密碼。

關於這行命令的說明:
a.
tomcat.crt CA頒發的SSL證書,tomcat.key是pem格式的密鑰,tomcat.p12是轉換后的PKCS12格式證書。
b. tomcat
tomcat通過這個別名在keystore搜索對應的證書應用到網站上。
c. ca.crt是中級證書和根證書的合並文件,這樣才能保證完整的證書鏈。)

5.導入PKCS12格式的證書和密鑰 得到Java keystore

keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore tomcat.keystore -srckeystore tomcat.p12 -srcstoretype PKCS12 -srcstorepass 123456 -alias tomcat

6.將生成的tomcat.keystore證書放入tomcat配置文件指定的證書目錄。重啟tomcat

注:(第5條:導入PKCS12格式的證書和密鑰 得到Java keystore

keytool -importkeystore -deststorepass <a_password_for_your_java_keystore> -destkeypass <a_password_for_the_key_in_the_keystore> -destkeystore tomcat.keystore -srckeystore <exported_private_key_and_cert.p12> -srcstoretype PKCS12 -srcstorepass <the_password_I_told_you_to_remember> -alias tomcat

說明:

運行命令的時候請不要包含任何括號<>;
<a_password_for_your_java_keystore> 這里替換成你自己的密碼;
<a_password_for_the_key_in_the_keystore> 這里替換成你自己的密碼 如果擔心混淆, 可以都設置成相同的密碼;
<exported_private_key_and_cert.p12> 剛才在第一步生成的PKCS12格式文件;
<the_password_I_told_you_to_remember> 剛才在第一步設置的密碼;
通過這一步我們會生成keystore文件: tomcat.keystore )

https://sg.godaddy.com/zh/help/tomcat-csr-5239

/data/tomcat/conf/server.xml

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" keystoreFile="/data/cert/tomcat.keystore" keystorePass="qwe123"
           ciphers="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA"
           URIEncoding="UTF-8"
    />
  • Nginx 配置ssl證書

1.從godaddy下載apache證書並解壓出兩個crt證書,然后將兩個crt證書合並為一個tidebuy.crt證書

cat cea.crt gd.crt > tidebuy.crt

2.從godaddy獲取key,如果證書已認證,從安裝服務器上下載key,保存為tidebuy.key(如果有這個.key文件可以跳過這一步,如果是自己按上面Godaddy購買證書生成csr文件就已經有了這個key文件

注:如果證書在iis服務器上使用.可以從iis服務器導出一份證書,tidebuy.pfx然后提取key

openssl pkcs12 -in tidebuy.pfx -nocerts -nodes -out tidebuy.key

3.將tidebuy.crt 和tidebuy.key放入nginx.conf配置文件中。 重啟nginx

/etc/nginx/nginx.conf

server {
        listen       443 ssl;
        server_name  localhost:8443;

        ssl on;
        ssl_certificate /data/cert/tidebuy.crt;
        ssl_certificate_key /data/cert/tidebuy.key;

        location / {
        proxy_pass https://localhost:8443;
        }
    }
  • crt轉換pem
openssl x509 -in tidebuy.crt -out tidebuy.pem
  • key轉換pem
openssl rsa -in tidebuy.key -out tidebuy.pem
  • crt轉換pfx
openssl pkcs12 -export -inkey reassurepay.key -in reassurepay.crt -out reassurepay.pfx

 


 自制證書

 nginx代理方式訪問倉庫

 在部署 Nginx 部分,需要先生成自簽名 SSL 證書,因為后面不想在 docker pull 的時候還要帶一個端口!這里需要 2 個域名,一個用來展示 nexus 前台,另一個用做 docker 倉庫,比如:

  • nexus 前台:repo.ald.com
  • docker 倉庫:idocker.io

1.安裝nginx

yum -y install nginx

2.生成證

這里推薦一個一鍵生成工具,大家可以嘗試使用:https://github.com/Fishdrowned/ssl ,使用方法請參考作者說明。

Ps:如果你打算做外網倉庫服務,那也可以去申請一個免費SSL證書,我這邊是內部oa域名使用,所以只能用自簽名證書了。

創建證書方式如下:

#直接切換到應用目錄
# cd /etc/nginx/conf.d/
 
#下載工具
# git clone https://github.com/Fishdrowned/ssl.git
Cloning into 'ssl'...
remote: Enumerating objects: 106, done.
remote: Total 106 (delta 0), reused 0 (delta 0), pack-reused 106
Receiving objects: 100% (106/106), 171.53 KiB | 286.00 KiB/s, done.
Resolving deltas: 100% (48/48), done.
 
#生成證書
# cd ssl
# ./gen.cert.sh idocker.io

Removing dir out
Creating output structure
Done
Generating a 2048 bit RSA private key
......+++
......................................................................................................................+++
writing new private key to 'out/root.key.pem'
-----
Generating RSA private key, 2048 bit long modulus
...............................................................................+++
.................................+++
e is 65537 (0x10001)
Using configuration from ./ca.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'CN'
stateOrProvinceName   :ASN.1 12:'Guangdong'
localityName          :ASN.1 12:'Guangzhou'
organizationName      :ASN.1 12:'Fishdrowned'
organizationalUnitName:ASN.1 12:'idocker.io'
commonName            :ASN.1 12:'*.idocker.io'
Certificate is to be certified until Jun 12 04:29:18 2022 GMT (730 days)

Write out database with 1 new entries
Data Base Updated

Certificates are located in:
lrwxrwxrwx 1 root root 37 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.bundle.crt -> ./20200612-1229/idocker.io.bundle.crt
lrwxrwxrwx 1 root root 30 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.crt -> ./20200612-1229/idocker.io.crt
lrwxrwxrwx 1 root root 15 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.key.pem -> ../cert.key.pem
lrwxrwxrwx 1 root root 11 6月  12 12:29 /etc/nginx/conf.d/ssl/out/idocker.io/root.crt -> ../root.crt

3.配置nginx

vim /etc/nginx/nginx.conf   #默認nginx配置文件位置

# ip地址可以換成內網ip upstream nexus_docker_get { server
127.0.0.1:8089; } upstream nexus_docker_put { server 127.0.0.1:8088; } server { listen 80; listen 443 ssl; server_name idocker.io; access_log /var/log/nginx/idocker.io.log; # 證書 ssl_certificate /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.crt; # 證書路徑根據上面生成的來定 ssl_certificate_key /etc/nginx/conf.d/ssl/out/idocker.io/idocker.io.key.pem; ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:'; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; # disable any limits to avoid HTTP 413 for large image uploads client_max_body_size 0; # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486) chunked_transfer_encoding on; # 設置默認使用推送代理 set $upstream "nexus_docker_put"; # 當請求是GET,也就是拉取鏡像的時候,這里改為拉取代理,如此便解決了拉取和推送的端口統一 if ( $request_method ~* 'GET') { set $upstream "nexus_docker_get"; } # 只有本地倉庫才支持搜索,所以將搜索請求轉發到本地倉庫,否則出現500報錯 if ($request_uri ~ '/search') { set $upstream "nexus_docker_put"; } index index.html index.htm index.php; location / { proxy_pass http://$upstream; proxy_set_header Host $host; proxy_connect_timeout 3600; proxy_send_timeout 3600; proxy_read_timeout 3600; proxy_set_header X-Real-IP $remote_addr; proxy_buffering off; proxy_request_buffering off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; } }

檢查沒有問題的話,就可以啟動nginx了。重啟報錯:nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx/nginx.pid"

nginx -t 
nginx -c /etc/nginx/nginx.conf #指定一個特定的nginx加載配置文件。
nginx -s reload

 

4.客戶端配置

部署完成之后,我們就可以找一台測試機器進行測試了,不過因為我們剛剛定義的是內部使用的域名,所以需要在測試機器上寫hosts解析,並將證書拷貝過去,否則會報不信任的錯誤。

在上文介紹的一鍵生成自簽名工具中,會生成一個根證書,名稱為/etc/nginx/conf.d/ssl/out/idocker.io/root.crt,我們將這個文件上傳到客戶端服務器的 /etc/docker/certs.d/idocker.io 目錄即可(注意目錄需要創建,最后的文件夾名稱和倉庫域名保持一致:idocker.io)。

docker和nginx在同一台機器:

#本地機器
echo "127.0.0.1 idocker.io" >> /etc/hosts
mkdir -p /etc/docker/certs.d/idocker.io
cp /etc/nginx/conf.d/ssl/out/idocker.io/root.crt /etc/docker/certs.d/idocker.io

docker和nginx不在同一台機器,現在到一台新主機192.168.75.10上測試:

# 主機192.168.75.10上的操作
echo "192.168.75.11 idocker.io" >> /etc/hosts
mkdir -p /etc/docker/certs.d/idocker.io

# 然后去nexus主機上,將剛才的證書拷過來
scp root.crt root@192.168.75.10:/etc/docker/certs.d/idocker.io

接下來,就可以開始真正的使用了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM