首頁  >  文章  >  運維  >  如何在Apache中建立和安裝自簽名憑證

如何在Apache中建立和安裝自簽名憑證

不言
不言原創
2019-03-30 15:15:283278瀏覽

SSL對於使用者和Web伺服器之間的安全通訊很有用。憑證在公共線路上傳輸時會對資料進行加密,這樣它就不會受到駭客的攻擊。自簽名憑證是免費使用的,但不在生產環境中使用,例如使用信用卡、Paypal資訊的機密資料。本篇文章將介紹在Linux系統上的Apache伺服器中建立和安裝自簽名憑證。

如何在Apache中建立和安裝自簽名憑證

步驟1:安裝mod_ssl套件

要設定SSL證書,請確保在系統上安裝了mod_ssl。如果尚未安裝,則需要使用以下命令進行安裝。另外,安裝openssl套件來建立憑證。

$ sudo apt-get install openssl          # Debian based systems 
$ sudo yum install mod_ssl openssl      # Redhat / CentOS systems 
$ sudo dnf install mod_ssl openssl      # Fedora 22+ systems

步驟2:建立自簽名憑證

安裝mod_ssl和openssl後,使用下列指令為你的網域建立一個自簽章憑證。

$ sudo mkdir -p /etc/pki/tls/certs
$ sudo cd /etc/pki/tls/certs

現在建立SSL憑證

$ sudo openssl req -x509 -nodes -newkey rsa:2048 -keyout example.com.key -out example.com.crt

輸出

Generating a 2048 bit RSA private key
....................................+++
...................................+++
writing new private key to 'example.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]: IN
State or Province Name (full name) []: Delhi
Locality Name (eg, city) [Default City]: Delhi
Organization Name (eg, company) [Default Company Ltd]: TecAdmin
Organizational Unit Name (eg, section) []: blog
Common Name (eg, your name or your server's hostname) []: www.example.com
Email Address []: admin@example.com

上面的指令將在目前目錄中建立一個ssl金鑰檔案example.com.key和一個憑證檔案example .com.crt。

步驟3:在Apache中安裝自簽名憑證

現在擁有了自簽SSL憑證和金鑰檔案。接下來編輯Apache SSL設定檔並按照以下指令進行編輯/更新。

Apache虛擬主機設定:


    ServerAdmin admin@example.com
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.crt
    SSLCertificateKeyFile /etc/pki/tls/certs/example.com.key

步驟4:重新啟動Apache

如果上面的指令沒有顯示任何錯誤,請重新啟動Apache服務。

$ sudo systemctl restart apache2       # Debian based systems   
$ sudo systemctl restart httpd         # Redhat based systems

步驟5:使用https測試網站

最後,使用https在你的網頁瀏覽器中開啟你的網站。它需要打開連接埠443才能使用HTTPS存取站點。

 https://www.example.com

當我們使用自簽名憑證時,你將在瀏覽器中收到一條警告訊息,忽略此訊息就可以了。

#

以上是如何在Apache中建立和安裝自簽名憑證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn