Linux下Apache服務(wù)的部署和配置
1 Apache的作用
- 解析網(wǎng)頁語言,如html,php,jsp等
- 接收web用戶的請求,并給予一定的響應(yīng)
2 Apache的安裝
安裝apche軟件:dnf install httpd.x86_64 -y
3 apache的啟用
- 開啟apache服務(wù)并設(shè)置開機(jī)啟動:
systemctl enable --now
httpd - 查看apache服務(wù)的狀態(tài):
systemctl enable --now httpd
- 查看火墻信息:firewall-cmd --list-all 在火墻中永久開啟http服務(wù):
firewall-cmd --permanent --add-service=http
- 在火墻中永久開啟https服務(wù): f
irewall-cmd --permanent --add-service=https
- 在不改變當(dāng)前火墻狀態(tài)的情況下刷新防火墻:
firewall-cmd --reload
4 apache的基本信息
apche的基本信息
- 服務(wù)名稱:
httpd
- 主配置文件:
/etc/httpd/conf/httpd.conf
- 子配置文件:
/etc/httpd/conf.d/*.conf
- 默認(rèn)發(fā)布目錄:
/var/www/html
- 默認(rèn)端口:80 (http) ,443(https)
- 日志文件:
/etc/httpd/logs
- 開啟apche服務(wù)后,輸入ip查看默認(rèn)發(fā)布頁面:
(1)更改apche服務(wù)的端口號
- 查看httpd服務(wù)的默認(rèn)端口號:
netstat -antlupe |grep httpd
- 編輯配置文件:
/etc/httpd/conf/httpd.conf
,修改端口號
- 重啟httpd服務(wù):
systemctl restart httpd
- 查看httpd服務(wù)的端口號:
netsat -antlupe | grep httpd
- 更改端口號后,輸入ip后無法正常連接,原因是8080端口未添加在火墻中
- 在防火墻里添加888端口號:
firewall-cmd --permanent --add-port=888/tcp
- 在不改變當(dāng)前火墻狀態(tài)的情況下刷新防火墻:
firewall-cmd --reload
- 輸入IP地址:端口號,可以正常訪問
(2)修改apche的默認(rèn)發(fā)布文件
- 默認(rèn)目錄:
cd /var/www/html
- 在文件默認(rèn)發(fā)布目錄下新建一個文件
index.html
- 輸入:http://172.25.254.144查看
默認(rèn)發(fā)布文件就是訪問apache時沒有指定文件名,即默認(rèn)訪問的文件,此文件可以指定多個,但有訪問順序。
- 新建文件并編輯:
westo.html
- 編輯配置文件:
/etc/httpd/conf/httpd.conf
- 重啟httpd服務(wù):
systemctl restart httpd
(3)修改apche的默認(rèn)發(fā)布目錄
- 新建目錄:
mkdir -p /westos/html/
- 創(chuàng)建文件:
vim /westos/html/index.html
- 編輯apche配置文件:
/etc/httpd/conf/httpd.conf
- 重啟服務(wù):
systemctl restart httpd
- 測試:瀏覽器中輸入http://172.25.254.144, 看到的是/westos/html/目錄內(nèi)的默認(rèn)發(fā)布文件
- 新建發(fā)布目錄:mkdir /var/www/html/westos
- 新建發(fā)布文件:vim /var/www/html/westos/index.html
- 編輯配置文件:vim /etc/httpd/conf/httpd.conf
- 重啟服務(wù):
systemctl restart httpd
- 測試:
http://172.25.254.144/westos/
5 apache的訪問控制
5.1 基于客戶端ip的訪問控制
- 基于ip的訪問,規(guī)定了哪些ip可以訪問,那些ip不能訪問,其中配置文件中order中的deny和Allow哪一個順序在前直接決定了黑白名單的屬性
(1)白名單
- ip白名單:只有名單內(nèi)的用戶可以訪問
- 編輯配置文件:
vim /etc/httpd/conf/httpd.conf
- 重啟服務(wù):
systemctl restart httpd
- 測試: ip=172.25.254.44在ip白名單,可以正常訪問http://172.25.254.44/westos
ip=172.25.254.144的主機(jī)不在白名單內(nèi),無法訪問http://172.25.254.44/westos
(2)ip黑名單
- ip黑名單:只有名單內(nèi)的用戶不可以訪問
- 編輯配置文件:
vim /etc/httpd/conf/httpd.conf
- 測試:ip=172.25.254.44在ip黑名單中,可以正常訪問http://172.25.254.44/westos
ip=172.25.254.144不在黑名單內(nèi),可以正常訪問http://172.25.254.44/westos
5.2 基于用戶認(rèn)證的訪問控制
(1)允許部分用戶通過認(rèn)證訪問共享目錄
- 生成認(rèn)證文件,建立admin用戶:
htpasswd -cm /etc/httpd/htpasswdfile linux
- 建立linux用戶,-c會重新建立用戶認(rèn)證文件,覆蓋之前的admin用戶,入密碼會覆蓋之前的用戶:
htpasswd -m /etc/httpd/htpasswdfile westos
- 只允許部分用戶可以通過認(rèn)證,編輯配置文件:
vim /etc/httpd/conf/httpd.conf
- 重啟服務(wù):systemctl restart httpd
- 測試:只有通過認(rèn)證的用戶可以訪問共享目錄
(2)允許所有用戶通過認(rèn)證訪問共享目錄
- 編輯配置文件:
vim /etc/httpd/conf/httpd.conf
- 重啟服務(wù):
systemctl restart httpd
- 測試:所有用戶都可以通過認(rèn)證訪問共享目錄
6 apache的虛擬主機(jī)
虛擬主機(jī):在一臺真實主機(jī)上建立多個站點(多個域名),通過域名訪問一臺主機(jī)的不同網(wǎng)頁,從網(wǎng)絡(luò)地址看似乎有多個主機(jī),這些主機(jī)被稱為虛擬主機(jī)
DNS解析域名的ip
建立linux,news,media的默認(rèn)發(fā)布目錄: mkdir /var/www/westos.com/{linux,news,media} inux的默認(rèn)發(fā)布文件: echo "<h1>hello linux</h1>" > /var/www/westos.com/news/index.html news的默認(rèn)發(fā)布文件:echo "<h1>hello news </h1>" > /var/www/westos.com/news/index.html media的默認(rèn)發(fā)布文件:echo "<h1>hello media </h1>" > /var/www/westos.com/media/index.html
- 新建apche的子配置文件并編輯:
/etc/httpd/conf.d/vhost.conf
- 編輯本地域名解析文件:
/etc/hosts
- 測試實驗效果:
7 apache的加密訪問
(1)安裝加密插件
- 查看apache的加密插件: dnf search apache
- 安裝加密插件
(2)生成私鑰: openssl genrsa -out /etc/pki/tls/private/www.westos.com.key
(3)生成證書簽名文件:openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/cert/www.westos.com.csr
(4)生成證書:
openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.westos.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/httpd/conf.d/ssl.conf
- 編輯apche的子配置文件并編輯:
/etc/httpd/conf.d/vhost.conf
- 重啟服務(wù):
systemctl restart httpd
- 測試:此時就可以正常使用https加密服務(wù)
8 網(wǎng)頁重寫
在瀏覽器中輸入media.westos.com會自動跳轉(zhuǎn)到如下界面
- 如果要使輸入media.westos.com后跳轉(zhuǎn)到https://media.westos.com,可以通過網(wǎng)頁重寫實現(xiàn),也就是當(dāng)訪問http(80端口)時自動跳轉(zhuǎn)到https(443端口)
- 實現(xiàn)網(wǎng)頁重寫的步驟
(1)apche的子配置文件并編輯:/etc/httpd/conf.d/vhost.conf
(2)重啟服務(wù):systemctl restart httpd
(3)測試,輸入域名后會自動加載https
9 正向代理
(1) 配置squid客戶端(該主機(jī)可以上網(wǎng))
- 編輯配置文件:
/etc/squid/squid.conf
- 啟動squid服務(wù):
systemctl start squid.service
(2)客戶端:在不能上網(wǎng)的主上測試,在瀏覽器中輸入:www.baidu.com不能訪問
- 加入代理:Preference——>Network settings——>Manual proxy configuration
- 填寫squid服務(wù)的主機(jī)和squid服務(wù)的端口號,設(shè)置完成后,該主機(jī)雖然沒有聯(lián)網(wǎng)但是可以通過代理訪問www.baidu.com及其他網(wǎng)站
- 在客戶端測試,能正常訪問www.baidu.com
但是在客戶端主機(jī)在仍然不能ping通www.baidu.com
10 反向代理
node1:沒有apache服務(wù)的虛擬機(jī)172.25.254.244
node2:能正常使用apache服務(wù)的虛擬機(jī)172.25.254.193,配置apache的發(fā)布文件
- 下載代理: dnf install squid -y
- 編輯配置文件:vim /etc/squid/squid.conf
- 重啟squid服務(wù):systemctl restart squid
原本沒有apache服務(wù)的主機(jī)172.25.254.244可以通過172.25.254.193的80端口(http)緩存數(shù)據(jù)
11 apache 支持的語言
php
重啟apache服務(wù):systemctl restart httpd.service
測試:http://172.25.254.144/index.php
cgi
mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi
cd /var/www/html/cgi
chmod +x index.cgi
./var/www/html/cgi/index.cgi
測試:http://172.25.254.144/cgi/index.cgi
編輯虛擬主機(jī)的配置文件:vim /etc/httpd/conf.d/vhost.conf
重啟服務(wù):systemctl restart httpd.service
測試:http://172.25.254.144/cgi/index.cgi
到此這篇關(guān)于Linux下Apache服務(wù)的部署和配置的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。