?網站需求:
?一、基于域名[www.openlab.com](http://www.openlab.com)可以訪問網站內容為 welcome to openlab
? 二、給該公司創建三個子界面分別顯示學生信息,教學資料和繳費網站,基于[www.openlab.com/student](http://www.openlab.com/student) 網站訪問學生信息,[www.openlab.com/data](http://www.openlab.com/data)網站訪問教學資料
? [www.openlab.com/money網站訪問繳費網站](http://www.openlab.com/money網站訪問繳費網站)。
??三、(1)學生信息網站只有song和tian兩人可以訪問,其他用戶不能訪問。
? ???????(2)訪問繳費網站實現數據加密基于https訪問
服務端工作:
1.關閉防火墻,selinux調整至寬容模式,安裝nginx并設置開機自啟
[root@node1-server ~]# setenforce 0
[root@node1-server ~]# getenforce
Permissive
[root@node1-server ~]# systemctl stop?firewalld # 已做,省略
[root@node1-server ~]# systemctl disable?firewalld # 已做,省略
[root@node1-server ~]# systemctl status firewalld
[root@node1-server ~]# yum install nginx -y
[root@node1-server ~]# nginx -V # 查看版本
[root@node1-server ~]# systemctl start nginx
[root@node1-server ~]# systemctl enable nginx
[root@node1-server ~]# systemctl status nginx
一、基于域名[www.openlab.com](http://www.openlab.com)可以訪問網站內容為 welcome to openlab
1.進入 /etc/nginx/nginx.conf 主配置文件并找到定義網站文件的根目錄
[root@node1-server ~]# vim /etc/nginx/nginx.conf
?41 ? ? ? ? server_name ?www.openlab.com;???# 定義當前服務器塊對應的域名
?42 ? ? ? ? root ? ? ? ? /usr/share/nginx/html;??#?定義網站文件的根目錄,Nginx 會從該目錄讀取網頁文件
2.進入定義網站文件的根目錄cd /usr/share/nginx/html/
[root@node1-server ~]# cd /usr/share/nginx/html/? #?Nginx 會從該目錄讀取網頁文件
[root@node1-server html]# ll
total 12
-rw-r--r--. 1 root root 3971 Feb 13 20:22 404.html
-rw-r--r--. 1 root root 4020 Feb 13 20:22 50x.html
drwxr-xr-x. 2 root root ? 27 Apr 11 10:13 icons
lrwxrwxrwx. 1 root root ? 25 Feb 13 20:23 index.html -> ../../testpage/index.html
-rw-r--r--. 1 root root ?368 Feb 13 20:22 nginx-logo.png
lrwxrwxrwx. 1 root root ? 14 Feb 13 20:23 poweredby.png -> nginx-logo.png
lrwxrwxrwx. 1 root root ? 37 Feb 13 20:23 system_noindex_logo.png -> ../../pixmaps/system-noindex-logo.png
3.將原初始網頁文件備份
[root@node1-server html]# cp index.html index.html.bak
4.將新內容寫入 /usr/share/nginx/html/index.html 文件中
[root@node1-server html]# echo "Welcome to Openlab!!!" > index.html
echo "Welcome to Openlabll!" > index.html
[root@node1-server html]# nginx -t?# 檢查配置文件的語法錯誤,無錯返回ok
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1-server html]# cat index.html
Welcome to Openlabll!
- nginx # 啟動nginx
- nginx restart # 重啟服務
- nginx -s reload # 重新加載配置文件
- nginx -s stop # 強行停止服務
- nginx -s quit # 優雅停止服務,即所有請求處理完后退出服務
- nginx -v # 查看版本號
- nginx -t # 檢查配置文件的語法錯誤,無錯返回ok
5.向Windows中C:\Windows\System32\drivers\etc\hosts文件中寫入域名,重啟nginx并測試
192.168.11.135 ? www.openlab.com # 此處文件不能保存為文本文檔(文件后綴為.txt)!
[root@node1-server html]# systemctl restart nginx.service
# 瀏覽器直接訪問 http://www.openlab.com?
二、給該公司創建三個子界面分別顯示學生信息,教學資料和繳費網站,基于[www.openlab.com/student](http://www.openlab.com/student) 網站訪問學生信息,[www.openlab.com/data](http://www.openlab.com/data)網站訪問教學資料
? [www.openlab.com/money網站訪問繳費網站](http://www.openlab.com/money網站訪問繳費網站)。
1.創建三個子界面分別顯示學生信息,教學資料和繳費網站
[root@node1-server ~]# mkdir /www
[root@node1-server ~]# cd /www
[root@node1-server www]# mkdir data student money
[root@node1-server www]# ls
data ?money ?student
[root@node1-server www]# touch data/index.html
[root@node1-server www]# touch student/index.html
[root@node1-server www]# touch money/index.html
[root@node1-server www]# echo "Teaching materials" > data/index.html
[root@node1-server www]# echo "Student Information" > student/index.html
[root@node1-server www]# echo "Payment Website" > money/index.html
[root@node1-server www]# cat data/index.html
Teaching materials
[root@node1-server www]# cat student/index.html
Student Information
[root@node1-server www]# cat money/index.html
Payment Website
2./etc/nginx/nginx.conf文件中增添子網頁內容
[root@node1-server ~]# vim /etc/nginx/nginx.conf
?38 ? ? server {
?39 ? ? ? ? listen ? ? ? 80;? # 監聽 IPv4 地址的 80 端口
?40 ? ? ? ? listen ? ? ? [::]:80;??# 監聽 IPv6 地址的 80 端口([::]?表示所有 IPv6 地址)
?41 ? ? ? ? server_name ?www.openlab.com;??# 定義當前服務器塊對應的域名
?42 ? ? ? ? root ? ? ? ? /usr/share/nginx/html;
?43 ? ? ? ? location /data {
?44 ? ? ? ? ? ? ? ? ? ? ? ? alias /www/data/;??# 將 URL 路徑 /data/?映射到本地目錄 /www/data/
?45 ? ? ? ? ? ? ? ? ? ? ? ? index index.html index.htm;??# 定義該目錄的默認索引文件(訪問目錄時優先查找的文件)
?46 ? ? ? ? ? ? ? ? ? ? ? ? }
?47 ? ? ? ? location /student {
?48 ? ? ? ? ? ? ? ? ? ? ? ? alias /www/student/;??# 將 URL 路徑 /student/?映射到本地目錄 /www/student/
?49 ? ? ? ? ? ? ? ? ? ? ? ? index index.html index.htm;
?50 ? ? ? ? ? ? ? ? ? ? ? ? }
?51 ? ? ? ? location /money {
?52 ? ? ? ? ? ? ? ? ? ? ? ? alias /www/money/;??# 將 URL 路徑 /money/?映射到本地目錄 /www/money/
?53 ? ? ? ? ? ? ? ? ? ? ? ? index index.html index.htm;
?54 ? ? ? ? ? ? ? ? ? ? ? ? }
3.測試語法是否有誤,無誤則重啟nginx服務
[root@node1-server ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1-server ~]# systemctl restart nginx.service
4.Windows測試
??三、(1)學生信息網站只有song和tian兩人可以訪問,其他用戶不能訪問。
1.安裝httpd-tools,創建song、tian兩位用戶
[root@node1-server ~]# yum install httpd-tools -y
[root@node1-server ~]# cd /www
[root@node1-server www]# htpasswd -c .stupasswd song? #?創建一個名為 .stupasswd 的密碼文件,并在其中添加一個名為 song 的用戶,同時為該用戶設置登錄密碼(通過交互式輸入),常用于 HTTP 基本認證(如 Nginx、Apache 服務器的權限控制),確保只有授權用戶能訪問特定資源。
New password:? # 123,此處隱藏
Re-type new password:??# 123,此處隱藏
Adding password for user song
[root@node1-server www]# htpasswd -b .stupasswd tian 123? #?向名為 .stupasswd 的密碼文件中添加一個名為 tian 的用戶,并設置其密碼為 123,無需手動輸入密碼,直接在命令中指定
Adding password for user tian
[root@node1-server www]# ll -a
total 4
drwxr-xr-x. ?5 root root ?64 Apr 11 17:21 .
dr-xr-xr-x. 20 root root 257 Apr 11 15:28 ..
drwxr-xr-x. ?2 root root ?24 Apr 11 15:31 data
drwxr-xr-x. ?2 root root ?24 Apr 11 15:31 money
drwxr-xr-x. ?2 root root ?24 Apr 11 15:31 student
-rw-r--r--. ?1 root root ?86 Apr 11 17:21 .stupasswd
1. htpasswd
- 工具名稱:Apache 自帶的密碼文件管理工具,用于生成和管理認證用戶的密碼文件。
- 適用場景:配置服務器(如 Apache、Nginx)的訪問權限時,通過密碼文件驗證用戶身份。
- 命令格式:htpasswd <選項>?<密碼文件> <用戶名>?
2. 選項 -c
- 作用:-c?是?create?的縮寫,表示創建一個新的密碼文件。
- 如果指定的密碼文件(.stupasswd)不存在,會創建新文件;
- 如果文件已存在,會提示是否覆蓋(需謹慎,避免刪除已有用戶)。
- 首次創建密碼文件時必須使用?-c,后續添加用戶時無需再加(否則會覆蓋原有文件)。
3. 選項 -b
- 作用:用于非交互方式設置用戶密碼,允許在命令行中直接指定用戶名和密碼,而無需手動輸入,適用于腳本自動化或批量處理。
4. 密碼文件路徑 .stupasswd
- 文件名稱:.stupasswd(隱藏文件)。
- 存儲內容:加密后的用戶名和密碼。
- 存儲位置:當前命令執行的目錄(可通過絕對路徑指定其他位置,如?/etc/nginx/.stupasswd)。
2.更新/etc/nginx/nginx.conf文件配置,判斷語法,重啟服務,Windows進行測試
[root@node1-server ~]# vim /etc/nginx/nginx.conf
?47 ? ? ? ? location /student {
?48 ? ? ? ? ? ? ? ? ? ? ? ? alias /www/student/;
?49 ? ? ? ? ? ? ? ? ? ? ? ? index index.html index.htm;
?50 ? ? ? ? ? ? ? ? ? ? ? ? auth_basic "Restricted";? #?啟用 HTTP 基本認證,提示用戶輸入用戶名和密碼;"Restricted" 是顯示給用戶的認證提示信息(瀏覽器彈出的登錄框會顯示此文本)
?51 ? ? ? ? ? ? ? ? ? ? ? ? auth_basic_user_file /www/.stupasswd;? #?指定存儲認證用戶和密碼的文件路徑
[root@node1-server www]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1-server www]# systemctl restart nginx.service
(2)訪問繳費網站實現數據加密基于https訪問
1.創建私鑰
[root@node1-server ~]# openssl genrsa -aes128 2048 > /etc/nginx/money.key
# 作用:生成一個 2048 位的 RSA 私鑰,并使用 AES-128 算法對私鑰進行加密保護,最終將私鑰保存到文件 /etc/nginx/money.key 中。
# 核心目的:為 HTTPS 服務器(如 Nginx)創建私鑰,用于加密通信數據,同時通過 AES 加密私鑰本身,防止未經授權的訪問。
Enter PEM pass phrase:? ?# 123456,此處隱藏
Verifying - Enter PEM pass phrase:? ?# 123456,此處隱藏
1. openssl
工具名稱:OpenSSL 是一個開源的密碼學工具包,用于實現安全通信所需的加密、解密、證書管理等功能。
應用場景:生成私鑰、證書簽名請求(CSR)、創建自簽名證書等。2. genrsa
子命令:genrsa 用于生成 RSA 私鑰(RSA 是一種非對稱加密算法,用于密鑰交換和數字簽名)。3. -aes128
加密算法與密鑰長度:-aes128 表示使用 AES-128 對稱加密算法對生成的私鑰進行加密。
AES是目前廣泛使用的安全加密算法,128 表示密鑰長度為 128 位(安全性與性能的平衡選擇,也可使用 -aes256 增強安全性)。
關鍵作用:私鑰本身會被加密存儲,后續使用時需輸入密碼解鎖,防止私鑰文件泄露后被直接利用。4. 2048
RSA 密鑰長度:指定生成的 RSA 私鑰位數為 2048 位。
安全標準:2048 位是目前推薦的最小長度(2016 年后,1024 位被認為不夠安全)。
更高位數(如 4096 位)安全性更強,但會增加計算開銷。5. > /etc/nginx/money.key
輸出路徑:將生成的私鑰重定向到文件 /etc/nginx/money.key。
目錄選擇:/etc/nginx/ 是 Nginx 的配置目錄,用于存放證書和私鑰(需確保 Nginx 服務有讀取該文件的權限)。文件名 money.key 可自定義,但建議包含業務相關名稱(如 server.key)。
2.制作證書
[root@node1-server ~]# openssl req -utf8 -new -key /etc/nginx/money.key -x509 -days 365 -out /etc/nginx/money.crt
#?作用:利用已有的 RSA 私鑰(/etc/nginx/money.key)生成一個 自簽名的 X.509 證書,有效期為 365 天,并保存到文件 /etc/nginx/money.crt 中。
# 核心目的:為 HTTPS 服務器(Nginx)提供證書文件,實現加密通信(TLS/SSL 協議),同時通過自簽名方式(無需 CA 機構)快速生成證書(適用于測試環境或內部系統)。
Enter pass phrase for /etc/nginx/money.key:? ?# 123456,私鑰密碼,此處隱藏
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]:86? ?# 國家代碼(如 CN)
State or Province Name (full name) []:shh? ?# 省/州
Locality Name (eg, city) [Default City]:xi'an? ?# 城市
Organization Name (eg, company) [Default Company Ltd]:openlab? ?# 組織名稱
Organizational Unit Name (eg, section) []:RHCE? ?# 部門
Common Name (eg, your name or your server's hostname) []:server? ?# 域名(必須與 server_name 一致)或填寫服務器的hostname,此處server為主機名;將 Common Name 填寫為服務器主機名僅適用于內網測試環境
Email Address []:zhao@qq.com? ?# 郵箱(可選)
[root@node1-server ~]# cd /etc/nginx/
[root@node1-server nginx]# cp money.key money.key.bak? # 備份私鑰
[root@node1-server nginx]# openssl rsa -in money.key.bak -out money.key? #?將加密的私鑰轉換為未加密的私鑰(或直接復制私鑰內容),主要用于簡化測試環境中的私鑰使用或恢復備份。
Enter pass phrase for money.key.bak:? ?# 123456,私鑰密碼,此處隱藏
writing RSA key
1. openssl req
子命令:
req:用于處理證書簽名請求(CSR)和自簽名證書生成。2. 選項 -utf8
作用:確保命令執行過程中的輸入輸出使用 UTF-8 編碼,支持中文等多字節字符(如證書中的組織名稱包含中文時必需)。
示例:若證書申請信息(如組織名稱)包含中文,不加此選項可能導致亂碼。3. 選項 -new
作用:創建一個 新的證書簽名請求(CSR) 或自簽名證書(結合 -x509 時為自簽名)。
核心邏輯:基于私鑰生成公鑰,并生成證書所需的元數據(如組織信息、域名等)。4. 選項 -key /etc/nginx/money.key
作用:指定用于生成證書的 私鑰文件路徑(本例中為之前生成的 money.key)。
依賴關系:
私鑰必須提前存在(如通過 openssl genrsa 生成)。
證書與私鑰需匹配(即證書由該私鑰對應的公鑰生成)。5. 選項 -x509
作用:生成 自簽名的 X.509 證書(而非 CSR)。
關鍵區別:
自簽名證書:由服務器自身生成,無需 CA 機構簽名(瀏覽器會顯示安全警告,適用于測試)。
CSR:需提交給 CA 機構簽名,生成受信任的證書(用于生產環境)。6. 選項 -days 365
作用:指定證書的有效期(天數)
安全建議:
生產環境建議設置更短的有效期(如 90 天),遵循現代安全實踐(如 Let's Encrypt 的證書有效期為 90 天)。
過期后需重新生成證書并更新服務器配置。7. 選項 -out /etc/nginx/money.crt
作用:指定生成的證書文件的輸出路徑和名稱。
文件格式:PEM 格式(文本形式,包含證書內容和元數據)。8. 選項 -in money.key.bak
作用:指定 輸入文件路徑,即待處理的私鑰文件(本例中為備份文件 money.key.bak)。9. 選項 -out money.key
作用:指定 輸出文件路徑,即處理后的私鑰保存位置(本例中為 money.key)。10. openssl rsa
子命令:
rsa:專門用于處理 RSA 私鑰(如轉換格式、去除密碼保護、提取公鑰等)。
3./etc/nginx/nginx.conf中增添新的server,啟用https
[root@node1-server nginx]# vim /etc/nginx/nginx.conf #?通過 server_name 和 listen 443 ssl 提供 HTTPS 服務,需要刪除監聽80端口部分的?location /money
?71 ? ? server {
?72
?73 ? ? ? ? listen ? ? ? 443 ssl;? #?聲明服務器監聽 443 端口(HTTPS 協議默認端口);ssl 標志表示啟用 SSL/TLS 加密,確保客戶端與服務器之間的通信數據加密傳輸。
?74 ? ? ? ? server_name ?www.openlab.com;? #?定義當前 server 塊對應的域名
?75
?76 ? ? ? ? ssl_certificate ? ? ?/etc/nginx/money.crt;? ?# SSL 證書文件路徑(公鑰證書)
?77 ? ? ? ? ssl_certificate_key ?/etc/nginx/money.key;? ?# 私鑰文件路徑(與證書匹配的 RSA 私鑰)
?78
?79 ? ? ? ? ssl_session_cache ? ?shared:SSL:1m;??# SSL 會話緩存(共享內存,容量 1MB)
?80 ? ? ? ? ssl_session_timeout ?5m;??# 會話緩存超時時間(5 分鐘)
?81 ? ? ? ? ssl_ciphers ?HIGH:!aNULL:!MD5;??# 允許的加密算法(高強度,排除不安全選項)
?82 ? ? ? ? ssl_prefer_server_ciphers ?on;??# 優先使用服務器端的加密算法列表
?83
?84 ? ? ? ? location /money {? ?# 匹配以 /money 開頭的 URL(https://www.openlab.com/money/)
?85 ? ? ? ? ? ? ? ? ? ? ? ? alias ? /www/money/;???# 將 URL 路徑 /money/ 映射到本地目錄 /www/money/
?86 ? ? ? ? ? ? ? ? ? ? ? ? index ?index.html index.htm;??# 定義目錄的默認索引文件
?87 ? ? ? ? ? ? ? ? ? ? ? ? }
?88 ? ? ? ? ? ?}
4.判斷語句是否無誤,重啟服務
[root@node1-server nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1-server nginx]# systemctl restart nginx.service
5.Windows測試
通過 https://www.openlab.com/money/ 訪問