linux系統學習第九天-<<工程師技術>>

兩臺虛擬機,均要檢測
1.IP地址
2.是否可以解析,server0.example.com
3.Yum是否可用

4.防火牆默認區域修改爲trusted

####################################################

• 基於 B/S (Browser/Server)架構的網頁服務
– 服務端提供網頁
– 瀏覽器下載並顯示網頁
• Hyper Text Markup Language,超文本標記語言
• Hyper Text Transfer Protocol,超文本傳輸協議

 

• 軟件包:httpd
• 系統服務:httpd


• 提供的默認配置
– Listen:監聽地址:端口(80)
– ServerName:本站點註冊的DNS名稱(空缺)
– DocumentRoot:網頁根目錄(/var/www/html)
– DirectoryIndex:起始頁/首頁文件名(index.html)

#############################################
一、搭建基本Web服務
虛擬機Server0上:
1.安裝httpd軟件
2.啓動httpd服務,並且設置 爲開機自起
3.書寫 網頁文件
# echo My First Web > /var/www/html/index.html

虛擬機Desktop0上:
# elinks -dump 172.25.0.11

#############################################

– ServerName:本站點註冊的DNS名稱(空缺)

虛擬機Server0上:
1.修改主配文件的內容/etc/httpd/conf/httpd.conf
  vim中命令行模式全文查找:   /ServerName

   ServerName  server0.example.com:80

2.  重起httpd服務

虛擬機Desktop0上:
# elinks -dump server0.example.com

##############################################

– DocumentRoot:網頁根目錄(/var/www/html)

虛擬機Server0上:
1.修改主配文件的內容/etc/httpd/conf/httpd.conf
DocumentRoot  "/var/www/abc"
2.創建目錄/var/www/abc
3.重起httpd服務
4.書寫網頁文件
# echo wo shi abc > /var/www/abc/index.html

虛擬機Desktop0上:
# elinks  -dump  server0.example.com     

##################################################

虛擬Web主機
    – 由同一臺服務器提供多個不同的Web站點


   • 區分方式
    – 基於域名的虛擬主機
    – 基於端口的虛擬主機
    – 基於IP地址的虛擬主機

• 配置文件路徑
– /etc/httpd/conf/httpd.conf   #主配置文件
– /etc/httpd/conf.d/*.conf     #從配置文件

 

  <VirtualHost *:80>
    ServerName www.qq.com
    DocumentRoot /var/www/qq
 </VirtualHost>

 幫助文檔:/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
1.建立從配置文件
[root@server0 ~]# vim /etc/httpd/conf.d/nsd01.conf
 <VirtualHost   *:80>
    DocumentRoot   /var/www/test
    ServerName   www0.example.com
 </VirtualHost>

 <VirtualHost   *:80>
    DocumentRoot   /var/www/nsd
    ServerName   webapp0.example.com
 </VirtualHost>

2. 建立目錄及相應的網頁內容
# echo wo shi test > /var/www/test/index.html
# echo wo shi nsd > /var/www/nsd/index.html
3. 重起httpd服務

#################################################
  啓用虛擬Web主機,所有的站點都必須用虛擬Web主機來實現

保證 server0.example.com可以訪問

1.修改從配置文件
[root@server0 ~]# vim /etc/httpd/conf.d/nsd01.conf
  追加寫入

 <VirtualHost   *:80>
    DocumentRoot   /var/www/abc
    ServerName   server0.example.com
 </VirtualHost>

2. 重起httpd服務

虛擬機Desktop0上:
# elinks  -dump  server0.example.com
 
################################################
 httpd服務訪問控制

虛擬機server0:
1.創建目錄與網頁文件
# mkdir  /var/www/abc/private
# echo 小蝌蚪找媽媽  >  /var/www/abc/private/index.html
# cat  /var/www/abc/private/index.html
2.建立新的配置文件
[root@server0 ~]# vim /etc/httpd/conf.d/nsd02.conf
 <Directory   "/var/www/abc/private">
    Require   ip  172.25.0.11   127.0.0.1    #僅允許本機訪問
 </Directory>

3 重起httpd服務

測試:  在Desktop0上訪問失敗,在Server0上訪問可以
   elinks  -dump  server0.example.com/private      
    
###################################################
思路:客戶端訪問服務端資源
    1.防火牆是否限制
    2.服務本身的訪問控制
  3.SELinux 是否限制


 SELinux策略保護

 1.安全上下文(標籤)
[root@server0 ~]# chcon -R --reference=/var/www /webroot

• 方式1:參照標準目錄,重設新目錄的屬性
– chcon [-R] --reference=模板目錄 新目錄

 使用自定Web根目錄
1.修改配置文件 /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
    DocumentRoot /webroot
    ServerName server0.example.com
</VirtualHost>

2.創建目錄與網頁文件
# mkdir /webroot
# echo wo shi webroot > /webroot/index.html

3.修改訪問控制配置文件,/etc/httpd/conf.d/nsd02.conf
 <Directory "/webroot">
    Require all granted
 </Directory>
 
4.修改SELinux標籤值
# chcon -R --reference=/var/www /webroot

5.重起httpd服務驗證
6.在Desktop0上: elinks  -dump  server0.example.com
############################################################  

 安全Web服務

    安全的超文本協議 https  端口爲:443

PKI公鑰基礎設施
• Public Key Infrastructure,公鑰基礎設施
    – 公鑰:主要用來加密數據
    – 私鑰:主要用來解密數據(與相應的公鑰匹配)
    – 數字證書:證明擁有者的合法性/權威性(單位名稱、
    有效期、公鑰、頒發機構及簽名、......)
    – Certificate Authority,數字證書授權中心:負責證書
    的申請/審覈/頒發/鑑定/撤銷等管理工作


• HTTPS 加密Web通信(TCP 443端口)
– Secure Sockets Layer ,安全套接字層
– Transport Layer Security,安全傳輸層協議
• 實現條件
– 啓用 SSL 模塊支持
– 部署好加密素材:網站服務器的數字證書、網站服務
器的私鑰、根證書(CA管理機構的證書)


虛擬機server0上操作:
1.安裝mod_ssl軟件包
[root@server0 ~]# yum -y install mod_ssl
[root@server0 ~]# ls /etc/httpd/conf.d/ssl.conf

2.部署網站的證書(營業執照)
# cd /etc/pki/tls/certs/
# wget http://172.25.254.254/pub/tls/certs/server0.crt

3.部署網站的根證書(公安局信息)
# cd /etc/pki/tls/certs/
# wget http://172.25.254.254/pub/example-ca.crt

4.部署私鑰(用於解密)
# cd /etc/pki/tls/private/
# wget http://172.25.254.254/pub/tls/private/server0.key

5.修改配置文件/etc/httpd/conf.d/ssl.conf

<VirtualHost _default_:443>
59行  DocumentRoot "/webroot"
60行  ServerName server0.example.com:443
100行 SSLCertificateFile /etc/pki/tls/certs/server0.crt
107行SSLCertificateKeyFile /etc/pki/tls/private/server0.key
122行SSLCACertificateFile /etc/pki/tls/certs/example-ca.crt

6.重起httpd服務

虛擬機desktop0上驗證:
[root@desktop0 ~]# firefox https://server0.example.com

######################################################
  部署動態網站

靜態網站的運行
• 服務端的原始網頁 = 瀏覽器訪問到的網頁


動態網站的運行
• 服務端的原始網頁 ≠ 瀏覽器訪問到的網頁
– 由Web服務軟件接受請求,動態程序轉後端模塊處理


案例6:部署並測試WSGI站點

1.安裝支持Python軟件
[root@server0 /]# yum -y install mod_wsgi

2.切換到相應/var/www/nsd目錄下載動態頁面

# cd /var/www/nsd/
# wget http://172.25.254.254/pub/materials/webinfo.wsgi
# cat webinfo.wsgi

3.在Desktop0上查看,可以看到源代碼
# firefox  webapp0.example.com/webinfo.wsgi

4.server0上修改 /etc/httpd/conf.d/nsd01.conf

            /var/www/nsd  ==== 客戶端的 /

 <VirtualHost  *:80>
    DocumentRoot  /var/www/nsd
    ServerName  webapp0.example.com
    alias  /  /var/www/nsd/webinfo.wsgi
 </VirtualHost>

5.重起httpd服務驗證
6.驗證
# elinks  -dump   webapp0.example.com


7.server0上修改 /etc/httpd/conf.d/nsd01.conf

            /var/www/nsd  ==== 客戶端的 /

 <VirtualHost  *:80>
    DocumentRoot  /var/www/nsd
    ServerName  webapp0.example.com
    wsgiscriptalias  /  /var/www/nsd/webinfo.wsgi
 </VirtualHost>
8.重起httpd服務驗證
9.驗證
# elinks  -dump   webapp0.example.com

  UINX時間戳:自1970-1-1  0:0:0到達當前所經過的秒數
10.修改webapp0.example.com監聽端口8909
   server0上修改 /etc/httpd/conf.d/nsd01.conf

 Listen 8909
 <VirtualHost *:8909>
     DocumentRoot /var/www/nsd
     ServerName webapp0.example.com
     wsgiscriptalias / /var/www/nsd/webinfo.wsgi
 </VirtualHost>

11. SELinux 限制非默認端口  8909

# semanage port -l  |  grep http       #查看允許的端口
# semanage port -a  -t  http_port_t  -p tcp 8909  #添加端口
# semanage port -l | grep http
# elinks -dump webapp0.example.com:8909


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章