Linux下Apache服務的部署和配置
1 Apache的作用
- 解析網(wǎng)頁語言,如html,php,jsp等
- 接收web用戶的請求,并給予一定的響應
2 Apache的安裝
安裝apche軟件:dnf install httpd.x86_64 -y

3 apache的啟用
- 開啟apache服務并設置開機啟動:
systemctl enable --nowhttpd - 查看apache服務的狀態(tài):
systemctl enable --now httpd

- 查看火墻信息:firewall-cmd --list-all 在火墻中永久開啟http服務:
firewall-cmd --permanent --add-service=http - 在火墻中永久開啟https服務: f
irewall-cmd --permanent --add-service=https - 在不改變當前火墻狀態(tài)的情況下刷新防火墻:
firewall-cmd --reload

4 apache的基本信息
apche的基本信息
- 服務名稱:
httpd - 主配置文件:
/etc/httpd/conf/httpd.conf - 子配置文件:
/etc/httpd/conf.d/*.conf - 默認發(fā)布目錄:
/var/www/html - 默認端口:80 (http) ,443(https)
- 日志文件:
/etc/httpd/logs - 開啟apche服務后,輸入ip查看默認發(fā)布頁面:

(1)更改apche服務的端口號
- 查看httpd服務的默認端口號:
netstat -antlupe |grep httpd

- 編輯配置文件:
/etc/httpd/conf/httpd.conf,修改端口號

- 重啟httpd服務:
systemctl restart httpd - 查看httpd服務的端口號:
netsat -antlupe | grep httpd

- 更改端口號后,輸入ip后無法正常連接,原因是8080端口未添加在火墻中

- 在防火墻里添加888端口號:
firewall-cmd --permanent --add-port=888/tcp - 在不改變當前火墻狀態(tài)的情況下刷新防火墻:
firewall-cmd --reload

- 輸入IP地址:端口號,可以正常訪問

(2)修改apche的默認發(fā)布文件
- 默認目錄:
cd /var/www/html - 在文件默認發(fā)布目錄下新建一個文件
index.html

- 輸入:http://172.25.254.144查看

默認發(fā)布文件就是訪問apache時沒有指定文件名,即默認訪問的文件,此文件可以指定多個,但有訪問順序。
- 新建文件并編輯:
westo.html

- 編輯配置文件:
/etc/httpd/conf/httpd.conf

- 重啟httpd服務:
systemctl restart httpd

(3)修改apche的默認發(fā)布目錄
- 新建目錄:
mkdir -p /westos/html/ - 創(chuàng)建文件:
vim /westos/html/index.html

- 編輯apche配置文件:
/etc/httpd/conf/httpd.conf

- 重啟服務:
systemctl restart httpd - 測試:瀏覽器中輸入http://172.25.254.144, 看到的是/westos/html/目錄內(nèi)的默認發(fā)布文件

- 新建發(fā)布目錄:mkdir /var/www/html/westos
- 新建發(fā)布文件:vim /var/www/html/westos/index.html

- 編輯配置文件:vim /etc/httpd/conf/httpd.conf

- 重啟服務:
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

- 重啟服務:
systemctl restart httpd - 測試: 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

(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 基于用戶認證的訪問控制
(1)允許部分用戶通過認證訪問共享目錄
- 生成認證文件,建立admin用戶:
htpasswd -cm /etc/httpd/htpasswdfile linux

- 建立linux用戶,-c會重新建立用戶認證文件,覆蓋之前的admin用戶,入密碼會覆蓋之前的用戶:
htpasswd -m /etc/httpd/htpasswdfile westos

- 只允許部分用戶可以通過認證,編輯配置文件:
vim /etc/httpd/conf/httpd.conf

- 重啟服務:systemctl restart httpd
- 測試:只有通過認證的用戶可以訪問共享目錄




(2)允許所有用戶通過認證訪問共享目錄
- 編輯配置文件:
vim /etc/httpd/conf/httpd.conf

- 重啟服務:
systemctl restart httpd - 測試:所有用戶都可以通過認證訪問共享目錄


6 apache的虛擬主機
虛擬主機:在一臺真實主機上建立多個站點(多個域名),通過域名訪問一臺主機的不同網(wǎng)頁,從網(wǎng)絡地址看似乎有多個主機,這些主機被稱為虛擬主機
DNS解析域名的ip
建立linux,news,media的默認發(fā)布目錄: mkdir /var/www/westos.com/{linux,news,media}
inux的默認發(fā)布文件: echo "<h1>hello linux</h1>" > /var/www/westos.com/news/index.html
news的默認發(fā)布文件:echo "<h1>hello news </h1>" > /var/www/westos.com/news/index.html
media的默認發(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

- 重啟服務:
systemctl restart httpd




- 測試:此時就可以正常使用https加密服務

8 網(wǎng)頁重寫
在瀏覽器中輸入media.westos.com會自動跳轉到如下界面

- 如果要使輸入media.westos.com后跳轉到https://media.westos.com,可以通過網(wǎng)頁重寫實現(xiàn),也就是當訪問http(80端口)時自動跳轉到https(443端口)
- 實現(xiàn)網(wǎng)頁重寫的步驟
(1)apche的子配置文件并編輯:/etc/httpd/conf.d/vhost.conf

(2)重啟服務:systemctl restart httpd
(3)測試,輸入域名后會自動加載https

9 正向代理
(1) 配置squid客戶端(該主機可以上網(wǎng))

- 編輯配置文件:
/etc/squid/squid.conf

- 啟動squid服務:
systemctl start squid.service

(2)客戶端:在不能上網(wǎng)的主上測試,在瀏覽器中輸入:www.baidu.com不能訪問

- 加入代理:Preference——>Network settings——>Manual proxy configuration


- 填寫squid服務的主機和squid服務的端口號,設置完成后,該主機雖然沒有聯(lián)網(wǎng)但是可以通過代理訪問www.baidu.com及其他網(wǎng)站

- 在客戶端測試,能正常訪問www.baidu.com

但是在客戶端主機在仍然不能ping通www.baidu.com

10 反向代理
node1:沒有apache服務的虛擬機172.25.254.244
node2:能正常使用apache服務的虛擬機172.25.254.193,配置apache的發(fā)布文件

- 下載代理: dnf install squid -y

- 編輯配置文件:vim /etc/squid/squid.conf

- 重啟squid服務:systemctl restart squid
原本沒有apache服務的主機172.25.254.244可以通過172.25.254.193的80端口(http)緩存數(shù)據(jù)

11 apache 支持的語言
php

重啟apache服務: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

編輯虛擬主機的配置文件:vim /etc/httpd/conf.d/vhost.conf

重啟服務:systemctl restart httpd.service
測試:http://172.25.254.144/cgi/index.cgi

到此這篇關于Linux下Apache服務的部署和配置的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
關注官方微信