兩臺虛擬機,均要檢測
1.IP地址 ifconfig
2.是否可以解析,nslookup server0.example.com
3.Yum是否可用 yum repolist
4.防火牆默認區域修改爲trusted
-------------------------------------------------------------------------------------------
HTTP服務基礎
Web通信基本概念
基於 B/S (Browser/Server)架構的網頁服務
B/S服務是C/S服務衍生出的
– 服務端提供網頁(下載提供相關服務的軟件)
– 瀏覽器下載並顯示網頁
HTML(Hyper Text Markup Language),超文本標記語言
編寫網頁的語言
作用:是靜態網頁文件的標記規範。
HTTP(Hyper Text Transfer Protocol),超文本傳輸協議,默認端口:80
客戶端訪問服務端網頁需要通過此協議
作用:主要爲網站服務器程序與瀏覽器之間傳輸網頁定義相關的標準。
客戶端通過瀏覽器訪問,服務端通過軟件包(httpd,nginx,tomcat等)提供相應服務
RHEL7的Web服務
軟件包:httpd
系統服務:httpd
提供的默認配置
– Listen:監聽地址:端口(80)
– ServerName:本站點註冊的DNS名稱(空缺)
– DocumentRoot:網頁根目錄(/var/www/html):存放網頁程序的
舉例https://zhidao.baidu.com/question/20112828.html
https://zhidao.baidu.com/就是根目錄
https://zhidao.baidu.com/question/20112828.html根目錄下的子目錄
– DirectoryIndex:起始頁/首頁文件名(index.html)
默認站點域名的網頁根目錄是/var/www/html
-------------------------------------------------------------------------------------------
一、搭建基本Web服務
虛擬機Server0上:
1.安裝httpd軟件
2.啓動httpd服務,並且設置 爲開機自起
3.書寫 網頁文件
elinks:純字符命令的瀏覽器
elinks-dump(純字符)
# echo Kobe Bryant > /var/www/html/index.html #書寫網頁內容爲Kobe Bryant
虛擬機Desktop0上:
# yum -y install elinks #安裝elinks軟件包
# elinks -dump 172.25.0.11/server0.example.com #IP或域名都行
Kobe Bryant
------------------------------------------------------------------------------------------
– ServerName:本站點註冊的DNS名稱(空缺)
cla***oom默認分配的域名:server0.example.com
www0.example.com
webapp0.example.com
虛擬機Server0上:
1.修改主配置文件的內容/etc/httpd/conf/httpd.conf
vim中命令行模式全文查找:/ServerName
95行: ServerName server0.example.com:80
2. 重起httpd服務
虛擬機Desktop0上:
# elinks -dump server0.example.com
補充: vim 命令模式
4yy:複製光標以下的4行
p:粘貼
------------------------------------------------------------------------------------------
– 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
通過server0.example.com此域名將/var/www/abc/index.html此目錄提供給客戶端提供網頁內容
補充: ::1 #ipv6的本機迴環測試
-------------------------------------------------------------------------------------------
虛擬Web主機
– 由同一臺服務器提供多個不同的Web站點
區分方式
– 基於域名的虛擬主機
– 基於端口的虛擬主機
– 基於IP地址的虛擬主機
配置文件路徑
– /etc/httpd/conf/httpd.conf #主配置文件
– /etc/httpd/conf.d/*.conf #從配置文件(從主配置文件調用)
主配置文件內容太多,龐雜,修改起來比較麻煩,可以用從配置文件調用主配置文件,簡潔高效
man幫助文檔:
#man /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
<VirtualHost *:@@Port@@>
ServerAdmin webmaster@dummy‐host2.example.com
DocumentRoot "@@ServerRoot@@/docs/dummy‐host2.example.com"
ServerName dummy‐host2.example.com
ErrorLog "/var/log/httpd/dummy‐host2.example.com‐error_log"
CustomLog "/var/log/httpd/dummy‐host2.example.com‐access_log"
common </VirtualHost>
在從配置文件中修改:
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. 建立目錄及相應的網頁內容
#mkdir /var/www/test
#mkdir /var/www/nsd
# 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服務訪問控制(ACL)
虛擬機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
若要desktop0也訪問成功,則在新建的從配置文件裏添加ip:
Require ip 172.25.0.11 172.25.0.10 127.0.0.1
-------------------------------------------------------------------------------------------
思路:客戶端訪問服務端資源
1.防火牆是否限制
2.服務本身的訪問控制
3.SELinux 是否限制
SELinux策略保護
1.安全上下文(標籤) chcon修改對象的安全上下文(標籤)
從配置文件裏修改
[root@server0 ~]# chcon -R(遞歸) --reference=/var/www /webroot #參考/var/www目錄爲模板設爲新目錄/webroot
方式1:參照標準目錄,重設新目錄的屬性
– chcon [-R] --reference=模板目錄 新目錄
使用自定Web根目錄 /Webroot
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標籤值,只允許訪問目錄/webroot的內容
# 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.部署網站的證書(營業執照) certs(證書)
# 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.部署私鑰(用於解密) private(密鑰)
# 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軟件mod_wsgi
[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上查看,可以看到源代碼
# yum -y install mod_wsgi
# 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 #跳轉,/var/www/nsd相當於 客戶端的根/,意思是可以直接輸入域名就可以訪問/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 #顯示時間戳
UNIX EPOCH time is now: 1509760256.37
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
拒絕連接的原因:要檢查SElinux訪問權限,防火牆權限和服務端的訪問權限