您好,登錄后才能下訂單哦!
######Apache web服務(wù)############
本單元涵蓋的主題:
* Apache基本配置
* 虛擬主機(jī)配置
* HTTPS配置
* 集成動(dòng)態(tài)內(nèi)容
########Apache基本配置########
Apache主配置文件:/etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" 用于指定Apache的運(yùn)行目錄 Listen 80 監(jiān)聽(tīng)端口 User apache 運(yùn)行apache程序的用戶和組 Group apache ServerAdmin root@localhost 管理員郵箱 DocumentRoot "/var/www/html" 網(wǎng)頁(yè)文件的存放目錄 <Directory "/var/www/html"> <Directory> 語(yǔ)句塊自定義目錄權(quán)限 Require all granted </Directory> ErrorLog "logs/error_log" 錯(cuò)誤日志存放位置 AddDefaultCharset UTF-8 默認(rèn)支持的語(yǔ)言 IncludeOptional conf.d/*.conf 加載其它配置文件 DirectoryIndex index.html 默認(rèn)主頁(yè)名稱
########apache的安裝#######
yum install httpd -y 安裝apache軟件包
systemctl start httpd 啟動(dòng)apache服務(wù)
systemctl stop firewalld
systemctl enable httpd
systemctl disable firewalld
netstat -antlpe | grep httpd ##查看監(jiān)聽(tīng)端口
#####apache的基本配置#######
1.apache的默認(rèn)發(fā)布文件
index.html
2.apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
3.apache的默認(rèn)發(fā)布目錄
/var/www/html
默認(rèn)發(fā)布文件
4.apache的默認(rèn)端口
80
######修改apache的基本配置########
1.修改默認(rèn)發(fā)布文件
vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex westos.html index.html ## 默認(rèn)主頁(yè)名稱
systemctl restart httpd
2.修改默認(rèn)發(fā)布目錄
###當(dāng)selinux是disable狀態(tài)時(shí)
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/www/html" ##網(wǎng)頁(yè)文件的存放目錄 121 <Directory "/westos/www/html"> ##語(yǔ)句塊自定義目錄權(quán)限 122 Require all granted 123 </Directory>
systemctl restart httpd
##當(dāng)selinux是Enforcing狀態(tài)時(shí)
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##配置安全上下文
restorecon -RvvF /westos
測(cè)試:172.25.254.162
3.apache的訪問(wèn)控制
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/admin"> ##允許所有人訪問(wèn)admin目錄但拒絕62主機(jī) Order Allow,Deny Allow from All Deny from 172.25.254.62 </Directory>
<Directory "/var/www/html/admin"> ##只允許62主機(jī)訪問(wèn)admin目錄 Order Deny,Allow Allow from 172.25.254.62 Deny from All </Directory>
systemctl restart httpd
測(cè)試:172.25.254.162/admin/
#####設(shè)定用戶的訪問(wèn)########
#####用兩個(gè)賬戶創(chuàng)建Apache密碼文件
htpasswd -cm /etc/httpd/accessuser admin ##建立用戶認(rèn)證文件并建立用戶admin設(shè)置密碼123
htpasswd -m /etc/httpd/accessuser cui ##建立認(rèn)證用戶cui,密碼123
vim /etc/httpd/conf/httpd.conf ##配置基于用戶的身份驗(yàn)證
<Directory "/var/www/html/admin"> AuthUserFile/etc/httpd/accessuser ##用戶認(rèn)證文件 AuthName "Please input yourname and password !!" ##用戶認(rèn)證提示信息 AuthType basic ##認(rèn)證類型 Require valid-user ##認(rèn)證用戶,認(rèn)證文件中所有的用戶都可以通過(guò) 或 [Require user admin] ##只允許認(rèn)證文件中的admin用戶訪問(wèn),二寫(xiě)一 </Directory>
systemctl restart httpd ##重啟apache服務(wù),并使用Web瀏覽器測(cè)試訪問(wèn),在彈出的對(duì)話框中輸入上述用戶名和密碼。
測(cè)試:172.25.254.162/admin/
4.apache 語(yǔ)言支持
html語(yǔ)言支持
php語(yǔ)言支持
yum install php -y ##安裝php服務(wù)
vim /var/www/html/index.php ##寫(xiě)php測(cè)試
<?php phpinfo() ?>
systemctl restart httpd
測(cè)試:172.25.254.162
**cgi語(yǔ)言支持
mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi ##默認(rèn)發(fā)布文件主頁(yè)內(nèi)容
#!/usr/bin/perl print "Content-type: text/html\n\n"; print `date`;
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/cgi"> ##網(wǎng)頁(yè)文件目錄 Options +ExecCGI AddHandler cgi-script .cgi </Directory> DirectoryIndex index.cgiindex.html ##默認(rèn)訪問(wèn)主頁(yè)名稱
systemctl restart httpd
chmod +x index.cgi ##給文件添加執(zhí)行權(quán)限
測(cè)試:
172.25.254.136/cgi/
#####Apache的虛擬主機(jī)#####
1.定義
可以讓我們的一臺(tái)apache服務(wù)器在被訪問(wèn)不同域名的時(shí)候顯示不同的主頁(yè)
虛擬主機(jī)允許您從一個(gè)httpd服務(wù)器同時(shí)為多個(gè)網(wǎng)站提供服務(wù)。在本節(jié)中,我們將了解基于名稱的虛擬主機(jī)其中多個(gè)主機(jī)名都指向同一個(gè)IP地址,但是Web服務(wù)器根據(jù)用于到達(dá)站點(diǎn)的主機(jī)名提供具有不同內(nèi)容的不同網(wǎng)站。
2.建立測(cè)試頁(yè)
##########建立網(wǎng)頁(yè)發(fā)布目錄#######
mkdir /var/www/virtual/money.westos.com/html -p
mkdir /var/www/virtual/news.westos.com/html -p
echo "<h2>news.westos.coms's page</h2>">/var/www/virtual/news.westos.com/html/index.html
echo "<h2>money.westos.coms's page</h2>">/var/www/virtual/money.westos.com/html/index.html
3.配置##創(chuàng)建虛擬主機(jī)配置文件
vim /etc/httpd/conf.d/default.conf ##未指定域名的訪問(wèn)都訪問(wèn)default
###這是定義虛擬主機(jī)的塊
<Virtualhost _default_:80> ##虛擬主機(jī)開(kāi)啟的端口 DocumentRoot"/var/www/html" ##虛擬主機(jī)默認(rèn)發(fā)布目錄 CustomLog "logs/default.log"combined ##虛擬主機(jī)日志 </Virtualhost>
vim /etc/httpd/conf.d/news.conf ##指定域名news.westos.com的訪問(wèn)到指定的默認(rèn)發(fā)布目錄中
<Virtualhost *:80> ServerName"news.westos.com" ##指定服務(wù)器名稱 DocumentRoot"/var/www/virtual/news.westos.com/html" ##默認(rèn)發(fā)布目錄的訪問(wèn)授權(quán) CustomLog "logs/news.log"combined ##虛擬主機(jī)日志 </Virtualhost> <Directory "/var/www/virtual/news.westos.com/html"> ##語(yǔ)句塊自定義目錄權(quán)限 Require all granted </Directory>
vim /etc/httpd/conf.d/money.conf
<Virtualhost *:80> ServerName"money.westos.com" DocumentRoot"/var/www/virtual/money.westos.com/html" CustomLog "logs/money.log"combined </Virtualhost> <Directory "/var/www/virtual/money.westos.com/html"> Require all granted </Directory>
systemctl start httpd ##啟動(dòng)apache服務(wù)
4.測(cè)試
在瀏覽器所在主機(jī)中
vim /etc/hosts
172.25.254.136 www.westos.comnews.westos.com money.westos.com
####https#####
1.https定義
通過(guò)ssl加密
2.配置
yum install mod_ssl -y ##安裝證書(shū)及其私鑰
yum install crypto-utils -y ##安裝crypto-utils軟件包
genkey www.westos.com ##調(diào)用genkey,同時(shí)為生成的文件指定唯一名稱
##記錄生成的證書(shū)(www.westos.com.crt)和關(guān)聯(lián)的私鑰(www.westos.com.key)的位置:
##繼續(xù)使用對(duì)話框,并選擇合適的密鑰大小:
##在生成隨機(jī)數(shù)時(shí)比較慢,敲鍵盤(pán)和移動(dòng)鼠標(biāo)可以加速
##拒絕向認(rèn)證機(jī)構(gòu)(CA)發(fā)送證書(shū)請(qǐng)求(CSR)。
##拒絕加密私鑰
##為服務(wù)器提供合適的身份
##得到/etc/pki/tls/certs/www.westos.com.crt
/etc/pki/tls/private/www.westos.com.key
##編輯/etc/httpd/conf.d/ssl.conf, 將SSLCertificateFile和SSLCertificateKeyFile指令設(shè)置為分別指向X.509證書(shū)和密鑰文件。
vim /etc/httpd/conf.d/login.conf
<Virtualhost *:443> ##訪問(wèn)443端口 ServerName"login.westos.com" ##指定服務(wù)器名稱 DocumentRoot"/var/www/virtual/login.westos.com/html" ##網(wǎng)頁(yè)文件的存放目錄 CustomLog"logs/login.log" combined ##日志 ssLEngine on ##開(kāi)啟https功能 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ## 證書(shū) SSLCertificateKeyFile/etc/pki/tls/private/www.westos.com.key ##密鑰 </Virtualhost> <Directory "/var/www/virtual/login.westos.com/html"> Require all granted </Directory> <Virtualhost *:80> ##網(wǎng)頁(yè)重寫(xiě)把所有80端口的請(qǐng)求全部重定向由https來(lái)處理 ServerName"login.westos.com" REwriteEngine on RewriteRule^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] </Virtualhost>
#^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] 解析 #^(/.*)$ 客戶主機(jī)在地址欄中寫(xiě)入的所有字符,不好看換行符 #https:// 定向成功的訪問(wèn)協(xié)議 #%{HTTP_HOST} 客戶請(qǐng)求主機(jī) #$1 $1的值就表示^(/.*)$的值 #[redirect=301] 臨時(shí)重定向 302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
vim /var/www/virtual/login.westos.com/html/index.html
<h2>haha www.westos.com</h2>
systemctl restart httpd
測(cè)試:
在客戶主機(jī)中添加解析
vim /etc/hosts
172.25.254.136 login.westos.com
訪問(wèn)http://login.westos.com會(huì)自動(dòng)跳轉(zhuǎn)到
https://login.westos.com實(shí)現(xiàn)網(wǎng)頁(yè)數(shù)據(jù)加密傳輸
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。