Linux--RH254---unit 9 apache web服務

unit 9 apache
一、apache的安裝

yum install httpd -y          ##安裝apache
systemctl start httpd         ##開啓apache
systemctl enable httpd        ##開機啓動apache
systemctl stop firewalld      ##關閉防火牆
systemctl disable firewalld   ##開機關閉防火牆



二、apache基本信息
1.apache的默認發佈文件
index.html
2.apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
3.apache的默認發佈目錄
/var/www/html
4.apache的默認端口
80

三、apache的基本配置
1.修改默認發佈文件

vim /var/www/html/westos.html    ##編輯westos.html文件

<h1>westos</h1>


vim /var/www/html/index.html     ##編輯index.html文件

<h1>linux</h1>


vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件

164     DirectoryIndex westos.html index.html    ##默認發佈文件修改爲westos.html


systemctl restart httpd          ##重啓httpd

測試

當訪問172.25.254.131時,訪問結果是/var/www/html/westos.html文件的內容



2.修改默認發佈目錄
##當selinux是disable狀態##
mkdir /westos/www/test -p          ##創建/westos/www/test目錄
vim /westos/www/test/westos.html   ##編輯/westos/www/test目錄下westos.html文件
<h1>hello</h1>
vim /etc/httpd/conf/httpd.conf      ##修改apache配置文件
119 #DocumentRoot "/var/www/html"   ##註釋掉原來的默認發佈目錄
120 DocumentRoot "/westos/www/test" ##指定默認發佈目錄爲/westos/www/test
121 <Directory "/westos/www/test">  
122     Require all granted
123 </Directory>

systemctl restart httpd


測試

當訪問172.25.254.131時,訪問結果是/westos/www/test/westos.html文件的內容


##當selinux是enforcing狀態##
vim /etc/httpd/conf/httpd.conf      ##修改apache配置文件
119 #DocumentRoot "/var/www/html"   ##註釋掉原來的默認發佈目錄
120 DocumentRoot "/westos/www/test" ##指定默認發佈目錄爲/westos/www/test
121 <Directory "/westos/www/test">  
122     Require all granted
123 </Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##修改默認的文件上下文

restorecon -RvvF /westos                                    ##刷新



測試

當訪問172.25.254.131時,訪問結果是/westos/www/test/westos.html文件的內容



3.apache的訪問控制
vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
DocumentRoot "/var/www/html"             ##使用原來的默認發佈目錄
#DocumentRoot "/westos/www/test"         ##註釋掉/westos/www/test發佈目錄
systemctl restart httpd                  ##重啓httpd
cd /var/www/html/                        ##切換至/var/www/html/目錄下
mkdir admin                              ##創建目錄admin
cd admin/                                ##切換至該目錄下
vim index.html                           ##編輯index.html文件

<h1>admin</h1>



測試

當訪問172.25.254.131/admin時,訪問結果是/var/www/html/admin/index.html文件的內容



##設定ip的訪問
*)vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     Order Allow,Deny                       ##先執行Allow,再執行Deny
126     Allow from All                         ##Allow所有用戶訪問
127     Deny from 172.25.254.131               ##拒絕172.24.254.131用戶訪問
128 </Directory>

systemctl restart httpd                  ##重啓httpd


測試

當訪問172.25.254.131/admin時,允許所有用戶訪問但是拒絕172.25.254.131用戶訪問



*)vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     Order Deny,Allow                       ##先執行Deny,再執行Allow
126     Allow from 172.25.254.131              ##允許172.24.254.131用戶訪問
127     Deny from All                          ##拒絕所有用戶訪問
128 </Directory>

systemctl restart httpd                  ##重啓httpd



測試

當訪問172.25.254.131/admin時,只允許172.25.254.131用戶訪問



##設定用戶的訪問##
htpasswd -cm /etc/httpd/accessuser admin               ##用admin賬戶創建Apache密碼文件(第一次創建密碼文件需要有c參數,m表示使用md5加密)
cat /etc/httpd/accessuser                              ##查看用戶賬戶和密碼
vim /etc/httpd/conf/httpd.conf                         ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     AuthUserFile /etc/httpd/accessuser                 ##用戶認證文件
126     AuthName "please input your name and password!!"   ##用戶認證提示信息
127     AuthType basic                                     ##認證類型
128     Require valid-user                                 ##認證用戶,認證文件中的所有用戶均可訪問
        [Require user admin]                               ##認證用戶,只允許認證文件中的admin用戶訪問,和上面一行二者選其一即可
129 </Directory>

systemctl restart httpd                  ##重啓httpd



測試

當訪問172.25.254.131/admin時,需填寫用戶認證信息



4.apache語言支持
*)php html cgi
默認支持html語言
*)php語言
cd /var/www/html/             ##切換至/var/www/html/
vim index.php                 ##編輯index.php
<?php
        phpinfo();
?>
yum install php -y            ##安裝php

systemctl restart httpd       ##重啓httpd


測試

當訪問172.25.254.131/index.php時,訪問結果爲


*)cgi語言
cd /var/www/html              ##切換至/var/www/html/
mkdir /var/www/html/cgi       ##創建cgi目錄
cd /var/www/html/cgi          ##切換到cgi目錄下
vim index.cgi                 ##編寫index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod +x index.cgi               ##給index.cgi添加可執行權限
vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件
130 <Directory "/var/www/html/cgi">
131         Options +ExecCGI
132         AddHandler cgi-script .cgi
133 </Directory>

systemctl restart httpd          ##重啓httpd

測試

當訪問172.25.254.131/cgi/index.cgi時,訪問結果爲



四、apache虛擬主機
1.定義:

可以讓我們的一臺apache服務器在被訪問不同域名的時候顯示不同的主頁

2.建立測試頁
cd /var/www/                    ##切換至/var/www/
mkdir virtual                   ##創建virtual目錄
mkdir virtual/news.westos.com -p    
mkdir virtual/news.westos.com/html -p

echo "news.westos.com's page" > virtual/news.westos.com/html/index.html



3.配置

*)默認

cd /etc/httpd/conf.d/
vim default.conf                        ##未指定域名的訪問都訪問default
<Virtualhost    _default_:80>           ##虛擬主機開啓的端口
        DocumentRoot "/var/www/html"    ##虛擬主機的默認發佈目錄
        CustomLog "logs/default.log" combined    ##虛擬主機日誌
</Virtualhost>

systemctl restart httpd                 ##重啓httpd


測試:
在客戶主機中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com


訪問news.wesstos.com時,訪問結果的內容是/var/www/html/westos.html下的內容



*)指定

vim news.conf                            ##指定域名news.westos.com的訪問到指定默認發佈目錄

<Virtualhost    *:80>                    ##虛擬主機開啓的端口
        ServerName "news.westos.com"     ##指定域名
        DocumentRoot "/var/www/virtual/news.westos.com/html"   ##虛擬主機的指定默認發佈目錄
        CustomLog "logs/news.log" combined                     ##虛擬主機日誌
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html">            ##默認發佈目錄的訪問授權
        Require all granted
</Directory>

systemctl restart httpd                  ##重啓httpd



測試:
在客戶主機中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com


訪問news.wesstos.com時,訪問結果的內容是/var/www/virtual/news.westos.com/html/下的內容



五、HTTPS
1.定義:

Hyper text transfer protocol over Secure socker layer

2.安裝

yum install mod_ssl crypto-utils -y            ##安裝mod_ssl,crypto-utils


genkey www.westos.com                          ##調用genkey,同時爲生成的文件指定唯一名稱


記錄生成的證書(www.westos.com.crt)和關聯的私鑰(www.westos.com.key)的位置


繼續使用對話框,並選擇合適的密鑰大小。(默認的2048位密鑰爲推薦值)


在生成隨機數時比較慢,敲鍵盤和移動鼠標可以加速


拒絕向認證機構(CA)發送證書請求(CSR)。


拒絕加密私鑰


爲服務器提供合適的身份。Common Name必須與服務器的主機全名完全匹配。(注意,任何逗號都應使用前導反斜線[\]進行轉義)


vim /etc/httpd/conf.d/login.conf               ##編輯login.conf
<Virtualhost    *:443>
        ServerName "login.westos.com"          ##指定域名
        DocumentRoot "/var/www/virtual/login.westos.com/html"   ##虛擬主機的指定默認發佈目錄
        CustomLog "logs/login.log" combined                     ##虛擬主機日誌
        SSLEngine on                                            ##開始https功能
        SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt       ##SSLCertificateFile指向生成證書的位置
        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key  ##SSLCertificateKeyFile指向關聯的私鑰的位置
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html">
        Require all granted
</Directory>
<Virtualhost    *:80>                         ##網頁重寫,把所有80端口的請求全部重定向由https來處理
        ServerName "login.westos.com"
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
### ^(/.*)$         客戶主機在地址欄中寫入的所有字符,不好看換行符
### https://        定向成爲的訪問協議
### %{HTTP_HOST}    客戶請求主機
### $1              $1的值就表示^(/.*)$的值
### [redirect=301]  臨時重定向,302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
echo "login.westos.com's page" > /var/www/virtual/login.westos.com/html/index.html

systemctl restart httpd




3.測試:
在客戶主機中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com login.westos.com


訪問http://login.westos.com會自動跳轉到https://login.westos.com實現網頁數據加密傳輸


Web客戶端可能會發出它不認可證書發行者的警告。這種情況適用自簽名證書。要求Web客戶端
繞過證書認證。(對於Firefox,請選擇“I Understand the Risks” [我瞭解風險]、“Add Exception” [

添加例外]和“Confirm Security Exception”[確認安全例外]。)



六、
1.正向代理
服務端:

yum install squid.x86_64 -y                  ##安裝squid


systemctl start squid                        ##開啓squid
netstat -antlpe | grep squid                 ##查看squid端口
vim /etc/squid/squid.conf                    ##編輯配置文件
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 57
 58 # Squid normally listens to port 3128
 59 http_port 3128
 60
 61 # Uncomment and adjust the following to add a disk cache directory.
 62 cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                      ##重啓squid


客戶端:
firefox

Edit-->Preferences-->Advanced-->Network-->Settings-->Manual proxy configuration-->HTTP Proxy(172.25.254.60)-->Port(3128)-->勾上Use this proxy server for all protocols--OK


在客戶端未開啓網絡的情況下可以通過服務端訪問www.baidu.com



2.反向代理
在做這個實驗之前,應確保環境純淨
rht-vmctl reset server                      ##重置虛擬機

rht-vmctl view server                       ##開啓虛擬機


vim /etc/sysconfig/network-scripts/ifcfg-eth0   ##配置網絡
DEVICE=eth0
BOOTPROTO=none
IPADDR=172.25.254.231
PREFIX=24
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no

PERSISTENT_DHCLIENT=1


systemctl restart network                   ##重啓網絡
vim /etc/yum.repos.d/rhel_dvd.repo          ##配置yum源
# Created by cloud-init on Thu, 10 Jul 2014 22:19:11 +0000
[rhel_dvd]
gpgcheck = 0
enabled = 1

baseurl = http://172.25.254.60/rhel7



yum install squid.x86_64 -y                 ##安裝squid


systemctl start squid                       ##開啓squid
vim /etc/squid/squid.conf                   ##編輯配置文件
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 57
 58 # Squid normally listens to port 3128
 59 http_port 80 vhost vport
 60 cache_peer 172.25.254.131 parent 80 0 no-query
 61 # Uncomment and adjust the following to add a disk cache directory.
 62 cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                     ##重啓squid


測試
vim /etc/hosts

172.25.254.231 www.westos.com


在測試端訪問172.25.254.131,是通過訪問172.25.254.231來訪問172.25.254.131的



3.輪叫機制
在真機中執行:
linux.sh linux                ##新建linux虛擬機
在linux中:

vim /etc/sysconfig/network-scripts/ifcfg-eth0          ##配置網絡


systemctl restart network                   ##重啓網絡


vim /etc/yum.repos.d/rhel_dvd.repo          ##配置yum源

yum install httpd -y                        ##安裝httpd

systemctl start httpd                       ##開啓httpd

systemctl stop firewalld                    ##關閉火牆


mkdir /var/www                        ##創建/var/www目錄
mkdir /var/www/html                   ##創建/var/www/html目錄
vim /var/www/html/westos.html    ##編輯westos.html文件

<h1>linux</h1>


vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件

164     DirectoryIndex westos.html index.html    ##默認發佈文件修改爲westos.html


systemctl restart httpd          ##重啓httpd
在server中:
vim /etc/squid/squid.conf                        ##編輯配置文件
# And finally deny all other access to this proxy
http_access allow all
# Squid normally listens to port 3128
http_port 80 vhost vport
cache_peer 172.25.254.131 parent 80 0 no-query originserver name=web1 round-robin
cache_peer 172.25.254.132 parent 80 0 no-query originserver name=web2 round-robin
cache_peer_domain www.westos.com web1 web2
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                         ##重啓squid


測試
vim /etc/hosts

172.25.254.231 www.westos.com


在測試端訪問www.westos.com,是通過訪問172.25.254.231來輪流訪問web1,web2


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