目錄

安裝免費 SSL

如何在 CentOS 7 上安裝 Let’s Encrypt SSL 證書

SSL 的全名是 Secure Sockets Layer,即安全通訊端層,簡而言之,這是一種標準的技術,用於保持網際網路連線安全以及防止在兩個系統之間發送的所有敏感資料被罪犯讀取及修改任何傳輸的資訊,包括潛在的個人詳細資料。兩個系統可以是伺服器與用戶端 (例如購物網站與瀏覽器),或者伺服器至伺服器 (例如,含有個人身份資訊或含有薪資資訊的應用程式)。

通常,電子商務或線上金融服務預計將具有此協議,因為它們存儲敏感資訊,例如信用卡詳細資訊。

然而,SSL證書實際上對所有網站都是強制性的,以保護用戶的機密資料,例如登錄憑據、社會安全號碼、身份證等。

現在您可以輕鬆地從  Let’s Encrypt 提供商處獲得 SSL 證書,這將在我們的安裝教程中使用。

在本文章中,您將學習如何在CentOS 7 上安裝免費 SSL 證書。

手動安裝 Apache

在安裝 SSL 之前,請確保您的 CentOS 上已經安裝了Apache (網頁伺服器軟體)。

通過 SSH 訪問伺服器時,您可以通過輸入 httpd -v 輕鬆測試這一點。如果安裝了 Apache,您將看到類似於下面的結果。

				
					[root@testvps ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Mar 24 2022 14:57:57
[root@testvps ~]#


				
			

如果沒有安裝 Apache,您將會見到以下資訊:

				
					[root@testvps ~]# httpd -v
-bash: httpd:命令找不到
[root@testvps ~]#


				
			

在 CentOS 7 上,可以通過軟體存儲庫安裝 Apache,方法是在終端機上輸入以下指令:

				
					yum install httpd -y


				
			

現在開始啟用 Apache,請輸入以下指令:

				
					systemctl start httpd


				
			

如果您希望 Apache 在系統開機時自動啟動 Apache 服務,請使用以下指令啟用它:

				
					systemctl enable httpd


				
			

以下是利用乾淨的 CentOS 7 來操作以上指令過程,過程如下:

				
					[root@testvps ~]# yum install httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirror.lzu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.el7.centos.5 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos.5 for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos.5 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch         Version                     Repository     Size
================================================================================
Installing:
 httpd             x86_64       2.4.6-97.el7.centos.5       updates       2.7 M
Installing for dependencies:
 apr               x86_64       1.4.8-7.el7                 base          104 k
 apr-util          x86_64       1.5.2-6.el7                 base           92 k
 httpd-tools       x86_64       2.4.6-97.el7.centos.5       updates        94 k
 mailcap           noarch       2.1.41-2.el7                base           31 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Is this ok [y/d/N]: Y
Downloading packages:
(1/5): httpd-tools-2.4.6-97.el7.centos.5.x86_64.rpm        |  94 kB   00:00
(2/5): apr-1.4.8-7.el7.x86_64.rpm                          | 104 kB   00:00
(3/5): apr-util-1.5.2-6.el7.x86_64.rpm                     |  92 kB   00:00
(4/5): mailcap-2.1.41-2.el7.noarch.rpm                     |  31 kB   00:00
(5/5): httpd-2.4.6-97.el7.centos.5.x86_64.rpm              | 2.7 MB   00:01
--------------------------------------------------------------------------------
Total                                              2.0 MB/s | 3.0 MB  00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                       1/5
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5
  Installing : httpd-tools-2.4.6-97.el7.centos.5.x86_64                     3/5
  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5
  Installing : httpd-2.4.6-97.el7.centos.5.x86_64                           5/5
  Verifying  : apr-1.4.8-7.el7.x86_64                                       1/5
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  2/5
  Verifying  : httpd-tools-2.4.6-97.el7.centos.5.x86_64                     3/5
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  4/5
  Verifying  : httpd-2.4.6-97.el7.centos.5.x86_64                           5/5

Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos.5

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7                      apr-util.x86_64 0:1.5.2-6.el7
  httpd-tools.x86_64 0:2.4.6-97.el7.centos.5    mailcap.noarch 0:2.1.41-2.el7

Complete!
[root@testvps ~]# systemctl start httpd
[root@testvps ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@testvps ~]#



				
			

如何使用 Let's Encrypt 安裝免費 SSL 證書

Let’s Encrypt是一個於2015年三季度推出的數位憑證認證機構,旨在以自動化流程消除手動建立和安裝憑證的複雜流程,並推廣使全球資訊網伺服器的加密連接無所不在,為安全網站提供免費的傳輸層安全性協定憑證。

就因為 Let’s Encrypt 提供免費的 SSL 證書,對於一般使用者而言來說非常方便,主要用於部落格或是企業網站的加密,但是電子商務就不建議使用。就讓我們看看如何在您的伺服器上安裝它。

我們建議先檢查系統,以確保系統已更新到最新。

				
					yum update -y


				
			

接下來,您需要 mod_ssl 來配置 Let’s Encrypt,所以我們先來安裝 mod_ssl。

				
					yum install mod_ssl -y
				
			

現在,我們來為網站新增一個文件根目錄文件夾來配置 Apache。

				
					mkdir /var/www/testvps.taki.tw
				
			

以下是這三個指令的流程:

				
					[root@testvps ~]# yum update -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirror.lzu.edu.cn
No packages marked for update
[root@testvps ~]# yum install mod_ssl -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirror.lzu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mod_ssl.x86_64 1:2.4.6-97.el7.centos.5 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package       Arch         Version                         Repository     Size
================================================================================
Installing:
 mod_ssl       x86_64       1:2.4.6-97.el7.centos.5         updates       115 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 115 k
Installed size: 224 k
Downloading packages:
mod_ssl-2.4.6-97.el7.centos.5.x86_64.rpm                   | 115 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mod_ssl-2.4.6-97.el7.centos.5.x86_64                       1/1
  Verifying  : 1:mod_ssl-2.4.6-97.el7.centos.5.x86_64                       1/1

Installed:
  mod_ssl.x86_64 1:2.4.6-97.el7.centos.5

Complete!
[root@testvps ~]# mkdir /var/www/testvps.taki.tw
[root@testvps ~]#
				
			

此步驟需要一個主機設定檔文件。您可以使用 nano 新增並輸入以下行:

				
					nano /etc/httpd/conf.d/testvps.taki.tw.conf
				
			

開啟nano後填入以下編碼(請確認好您的資料,別以我們做demo的資料填入):

				
					<VirtualHost *:80>
ServerAdmin admin@taki.tw
DocumentRoot "/var/www/testvps.taki.tw"
ServerName testvps.taki.tw
ServerAlias www.testvps.taki.tw
ErrorLog "/var/log/httpd/test.error_log"
CustomLog "/var/log/httpd/test.access_log" common
</VirtualHost>
				
			
開啟nano後填入以下編碼

certbot 安裝

在安裝 certbot 之前,請確保您已通過輸入以下指令安裝並啟用 EPEL 存儲庫:

				
					yum install epel-release -y
				
			

下一個步驟是安裝 yum-utils

				
					yum install yum-utils -y
				
			

接下來您可以為您的 Apache 安裝 certbot:

				
					yum install certbot-apache -y
				
			

安裝完 certbot 後,透過以下指令執行:

				
					certbot
				
			

這裡會有一個提示詢問您要在其上啟動 https 的名稱:

				
					[root@testvps ~]# certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: testvps.taki.tw
2: www.testvps.taki.tw
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 
				
			

只需按 Enter,testvps.taki.tw 和www.testvps.taki.tw 都會被重定向到 https。

這時會出現另一個提示,選擇 2 將您的網站名稱都重定向到 https。

設定自動更新 Let's Encrypt SSL 證書

使用 Let’s Encrypt SSL 證書的一個好處是您可以設定自動更新 SSL 證書。

要設置自動續訂,請輸入以下指令:

				
					export EDITOR=/bin/nano
				
			

它將 nano 設定變為默認編輯器,現在它可以編輯 crontab:

				
					crontab -e
				
			

實際上,Let’s Encrypt 建議每天執行兩次自動更新 cron 作業。因此,我們需要設定 cron 作業,請粘貼以下指令並保存 crontab:

				
					* */12 * * * /usr/bin/certbot renew >/dev/null 2>&1
				
			

結論

需要 SSL 來保護您網站上的流量,尤其是在它處理交易時。 Let’s Encrypt 可以讓您在 CentOS 7 上安裝 SSL 證書的提供程式。雖然安裝它過程可能很複雜,但是這是值得的。

TAKI 雲端主機
TAKI Cloud 雲端主機
TAKI Cloud 實體主機 Dedicated Server
TAKI Cloud 實體主機 只要4,500起
中華電信國分機房 主機代管服務
TAKI Cloud 主機代管

By taki

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *