1.Apache的作用
Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源碼的網頁服務器,可以在大多數計算機操作系統中運行,由於其多平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一.
簡言而知,Apache是在web被訪問時,http:// (超文本傳輸協議)的提供軟件。
http:// 超文本傳輸協議提供軟件: Apache nginx stgw jfe Tengine
一些常見的網頁服務器軟件有:
2.Apache的安裝
環境:rhel8
ip:192.168.0.11
dnf install httpd.x86_64 -y
3.Apache的啓用
systemctl enable --now httpd
開啓服務並設定服務位開機啓動
firewall-cmd --list-all
查看火牆信息
firewall-cmd --permanent --add-service=http
在火牆中永久開啓http訪問
firewall-cmd --permanent --add-service=https
在火牆中永久開啓https訪問
firewall-cmd --reload
刷新火牆使設定生效
4.Apache的基本信息
服務名稱 | httpd |
---|---|
主配置文件 | /etc/httpd/conf/httpd.conf |
子配置文件 | /etc/httpd/conf.d/*.conf |
默認發佈目錄 | /var/www/html |
默認發佈文件 | index.html |
http端口 | 80 |
https端口 | 443 |
用戶 | apache |
日誌 | /etc/httpd/logs |
5.Apache的基本配置
5.1 Apache端口修改
vim /etc/httpd/conf/httpd.conf
Listen 6666 更改端口
firewall-cmd --permanent --add-port=6666/tcp
在火牆中添加端口
firewall-cmd --reload
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 6666
在selinux中添加端口
systemctl restart httpd
實驗一:更改端口號爲8080
注意:8080端口是被用於WWW代理服務的,可以實現網頁瀏覽,經常在訪問某個網站或使用代理服務器的時候,會加上“:8080”端口號。另外Apache Tomcat web server安裝後,默認的服務端口就是8080。
實驗二:更改端口號爲6060
注意:有些端口號由於瀏覽器安全限制最終會導致無法訪問,如火狐瀏覽器不能訪問6666端口
5.2 默認發佈文件
注意: 默認發佈文件如果不存在,訪問的內容爲測試儀; 默認發佈文件如果存在,訪問的內容爲文件內容。
可在配置文件中更改默認發佈文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex test.html index.html
(注意:這裏會先訪問前面的默認發佈文件,如果失效再訪問後面的)
systemctl restart httpd
實驗:更改默認發佈文件
5.3 默認發佈目錄
1.更改配置文件,指定默認發佈目錄位置並授權
vim /etc/httpd/conf/httpd.conf
<Directory "/test/html">
DocumentRoot "/test/html"
Require all granted
</ Directory>
`
systemctl restart httpd`
2.更改發佈目錄的安全上下文同默認安全上下文一致
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
restorecon -RvvF /westos/ systemctl restart httpd
firefox http://192.168.0.11
6.Apache的訪問控制
實驗素材
mkdir /var/www/html/usertest
vim /var/www/html/usertest/index.html
< h1>westosdir's page </ h1>
firefox http://192.168.0.11/westos
rhe8的ip:192.168.0.10
rhe7的ip:192.168.0.20
1.基於客戶端ip的訪問控制
vim /etc/httpd/conf/httpd.conf
ip白名單在配置文件中的更改
<Directory "/var/www/html/usertest">
Order Deny,Allow
Allow from 192.168.0.10
Deny from All
</Directory>
ip黑名單在配置文件中的更改
<Directory "/var/www/html/usertest">
Order Allow,Deny
Allow from All
Deny from 192.168.0.10
</Directory>
黑名單實驗:
白名單實驗:
2.基於用戶認證
1.在配置文件更改用戶認證信息
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/htpasswdfile ##指定認證文件
AuthName "Please input your name and password" ##認證提示語
AuthType basic ##認證類型
Require user admin ##允許通過的認證用戶 2選1
Require valid-user ##允許所有用戶通過認證 2選 1
</Directory>
<
font color=blue>2.生成認證文件
htpasswd -cm /etc/httpd/htpasswdfile admin
注意: 當/etc/httpd/htpasswdfile存在那麼在添加用戶時不要加-c參數否則會覆蓋源文件內容
實驗一:允許所有用戶驗證:
注意:再次登錄不需要輸入密碼,清空歷史後需要再次輸入密碼
實驗二:允許指定用戶驗證:
7.Apache的虛擬主機
1.一個IP建立多個站點
mkdir -p /var/www/westos.com/{news,wenku}
echo "wenku's page" >/var/www/westos.com/wenku/index.html
echo "news's page" > /var/www/westos.com/news/index.html
echo "default's page" > /var/www/html/index.html
2.在httpd的子配置文件中配置虛擬主機
vim /etc/httpd/Virtualhost.conf
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wenku.westos.com
DocumentRoot "/var/www/westos.com/wenku"
CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost :80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
systemctl restart httpd
3.在瀏覽器所在主機(客戶端)中設置地址解析
vim /etc/hosts
4.測試:在 firefox瀏覽器中測試
**http://www.test.com **
**http://wenku.test.com **
http://news.westos.com
8.Apache的支持語言
8.1 支持html(默認)
cd /var/www/html
##切換到默認發佈目錄
echo "default's page" > /var/www/html/index.html
##編輯默認發佈文件
8.2 支持php
dnf install php -y
安裝php
vim /var/www/html/index.php
z在默認發佈目錄中發佈php發佈文件
<?
php phpinfo();
?>
systemctl restart httpd
測試:firefox http://192.168.0.11/index.php
8.3 支持cgi
1.生成cgi腳本,並給與執行權限
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi
2.要允許對用戶目錄中以.cgi結尾的任何文件執行CGI程序
vim /etc/httpd/conf.d/Virtualhost.conf
<Directory “/var/www/html/cgidir”>
Options +ExecCGI AddHandler cgi-script .cgi
</ Directory>
3.測試: firefox http://192.168.0.11/cgidir/index.cgi(注意關閉selinux)
實驗:
9.Apache的加密訪問
當需要使用更加安全的瀏覽器,如在用信用卡進行網上付款需要輸入密碼時:顧客點擊鏈接建立TCP連接後,先進行瀏覽器和服務器之間的握手協議,完成加密算法的協商和會話密鑰的傳遞,然後進行安全數據傳輸。HTTP會調用SSL/TLS對整個網頁進行加密,爲此萬維網服務器使用SSL的默認服務端口443來取代普通萬維網服務的80端口,此時在網址欄原先顯示的http的地方會變成https。在協議明http後面加的s代表security,表明現在使用的是提供安全服務的HTTP協議。
9.1 生成apache加密證書
要求:安裝SSL插件
1.生成私鑰
openssl genrsa -out /etc/pki/tls/private/www.test.com.key 2048
2.生成證書籤名文件
openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.test.com.csr
3.生成證書
openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.test.com.csr -signkey /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.crt
注意:
x509 證書格式 ;-req 請求;-in 加載簽證名稱
-signkey /etc/pki/tls/private/www.westos.com.key 指定簽證Key文件
4.編輯ssl配置文件
vim /etc/httpd/conf.d/ssl.conf
9.2 僅對需要內容加密訪問
注意:加密訪問雖然安全,但是會影響傳輸速率,我們可用在登錄用戶需要交互信息時進行加密訪問。
1.建立登錄等站點
mkdir -p /www/test.com/{wenku,news,login}
2.在http子配置文件中授權
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName login.test.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 ##使網頁從80端口自動跳轉到443
</VirtualHost>
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot "/www/test.com/login"
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
systemctl restart httpd
注意:
^(/.*)$——客戶地址欄中輸入的地址
%{HTTP_HOST} ——客戶主機
$1 ——RewriteRule後面跟的第一串字符的值
測試:
10.Squid+Apache
10.1 squid 正向代理
實驗環境: 兩臺主機
單網卡主機rhel8_node1:設定ip不能上網 IP:172.25.254.10
雙網卡主機rhel8_node2:設定ip1可以連接單網卡主機,設定ip2可以上網
IP1:172.25.254.100
IP2:192.168.0.33(可以上網)
實驗操作: 在雙網卡主機中
dnf install squid -y
vim /etc/squid/squid.conf
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
**實驗效果: 在單網卡主機中設置瀏覽器中的 NetWork Proxy
** 讓單網卡主機不能上網但瀏覽器可以訪問互聯網頁
測試: 在單網卡主機中 ping www.baidu.com 不通 在瀏覽器中訪問www.baidu.com可以
測試: 在單網卡主機中 ping www.baidu.com 不通 在瀏覽器中訪問www.baidu.com可以
10.2 squid反向代理
實驗環境:
192.168.0.100 ##Apache服務器
192.168.0.33 ##squid,沒有數據負責緩存
實驗操作:
vim /etc/squid/squid.conf
http_port 80 vhost vport ##vhost 支持虛擬域名 vport 支持虛擬端口
#當192.168.0.12的80端口被訪問會從192.168.0.11的80端口緩存數據
cache_peer 192.168.0.11 parent 80 0 proxy-only
systemctl restart squid
測試: firefox http://192.168.0.12 訪問看到的時192.168.0.11上的數據