標籤:apache 服務 http
一.apache的安裝
yum install httpd -y 安裝服務
systemctl start httpd 啟動服務
systemctl stop firewalld 關閉防火牆
systemctl enable httpd 開機自動啟動
systemctl disable firewalld 開機不啟動防火牆
二.apache相關配置資訊
1.apache的預設發布目錄檔案
/var/www/html/index.html
2.apache的設定檔
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
3.apache的預設連接埠
80
三. apache的基本配置
1.修改預設發布檔案
(1)vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex westos.html index.thml
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/9E/3F/wKioL1mOXiGjeGM1AAApSoKdMpc333.png" style="float:none;" title="Screenshot from 2017-08-11 15-05-17.png" alt="wKioL1mOXiGjeGM1AAApSoKdMpc333.png" />
vim westos
hello ,westos!
預設優先讀取寫在前面的發布檔案,westos.html檔案損壞後,讀取index.html檔案
650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/9E/50/wKiom1mOXBvCj-6FAAAt-AqmjS8228.png" style="float:none;" title="Screenshot from 2017-08-11 15-00-29.png" alt="wKiom1mOXBvCj-6FAAAt-AqmjS8228.png" />
重啟apache服務systemctl restart httpd,訪問首頁面為westos.html
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/9E/3F/wKioL1mOXBuDAnmVAABVy9bR758367.png" style="float:none;" title="Screenshot from 2017-08-11 15-03-47.png" alt="wKioL1mOXBuDAnmVAABVy9bR758367.png" />
(2)刪除westos發佈頁面,index成為預設發布檔案
vim westos
hello ,world!
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/9E/51/wKiom1mOXiHwGCXYAABjb2q_6Q0476.png" style="float:none;" title="Screenshot from 2017-08-11 15-05-28.png" alt="wKiom1mOXiHwGCXYAABjb2q_6Q0476.png" />
連接埠顯示正常
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/9E/3F/wKioL1mOXiKQF37BAABsE_On0zo791.png" style="float:none;" title="Screenshot from 2017-08-11 15-11-15.png" alt="wKioL1mOXiKQF37BAABsE_On0zo791.png" />
重啟apache服務,訪問首頁面為index.html
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/9E/3F/wKioL1mOXiCBOhRKAABQabZ02CM798.png" style="float:none;" title="Screenshot from 2017-08-11 15-04-30.png" alt="wKioL1mOXiCBOhRKAABQabZ02CM798.png" />
2.修改預設發布目錄
(1)當selinux是disable狀態
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/html" 修改預設發布目錄為/westos/html
<Directory "/westos/html">
Require all granted 所有人能訪問
</Directory>
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/3F/wKioL1mOYDiDQXLGAAAY6cxeLDo114.png" style="float:none;" title="Screenshot from 2017-08-11 15-27-10.png" alt="wKioL1mOYDiDQXLGAAAY6cxeLDo114.png" />
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/9E/3F/wKioL1mOYDeBcxUnAABLrYEyYUs436.png" style="float:none;" title="Screenshot from 2017-08-11 15-22-56.png" alt="wKioL1mOYDeBcxUnAABLrYEyYUs436.png" />
systemctl restart httpd
建立新的發布目錄/westos/html
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/9E/3F/wKioL1mOYDfzp__BAABEf5EbExQ631.png" style="float:none;" title="Screenshot from 2017-08-11 15-26-24.png" alt="wKioL1mOYDfzp__BAABEf5EbExQ631.png" />
編寫westos發布檔案
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/51/wKiom1mOYDjR0Ip7AAAkJGScTwQ197.png" style="float:none;" title="Screenshot from 2017-08-11 15-27-01.png" alt="wKiom1mOYDjR0Ip7AAAkJGScTwQ197.png" />
重啟服務,訪問為建立目錄下的檔案正常
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/9E/3F/wKioL1mOYDnxdfDOAABdo--ZWUA572.png" style="float:none;" title="Screenshot from 2017-08-11 15-28-07.png" alt="wKioL1mOYDnxdfDOAABdo--ZWUA572.png" />
(2)當selinux是enforcing狀態
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/html" 修改預設發布目錄為/westos/html
<Directory "/westos/html">
Require all granted 所有人能訪問
</Directory>
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/3F/wKioL1mOYDiDQXLGAAAY6cxeLDo114.png" style="float:none;" title="Screenshot from 2017-08-11 15-27-10.png" alt="wKioL1mOYDiDQXLGAAAY6cxeLDo114.png" />
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/9E/3F/wKioL1mOYDeBcxUnAABLrYEyYUs436.png" style="float:none;" title="Screenshot from 2017-08-11 15-22-56.png" alt="wKioL1mOYDeBcxUnAABLrYEyYUs436.png" />
systemctl restart httpd
建立新的發布目錄/westos/html
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/9E/3F/wKioL1mOYDfzp__BAABEf5EbExQ631.png" style="float:none;" title="Screenshot from 2017-08-11 15-26-24.png" alt="wKioL1mOYDfzp__BAABEf5EbExQ631.png" />
編寫westos發布檔案
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/51/wKiom1mOYDjR0Ip7AAAkJGScTwQ197.png" style="float:none;" title="Screenshot from 2017-08-11 15-27-01.png" alt="wKiom1mOYDjR0Ip7AAAkJGScTwQ197.png" />
查看預設發布目錄及建立目錄的上下文,設定建立目錄westos的上下文為httpd_sys_content_t
修改完成後,更新上下文 restorecon -RvvF /westos
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/51/wKiom1mOYDmTbPvgAAFXRazZVsg442.png" style="float:none;" title="Screenshot from 2017-08-11 15-27-46.png" alt="wKiom1mOYDmTbPvgAAFXRazZVsg442.png" />
重啟服務,訪問為建立目錄下的檔案正常
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/9E/3F/wKioL1mOYDnxdfDOAABdo--ZWUA572.png" style="float:none;" title="Screenshot from 2017-08-11 15-28-07.png" alt="wKioL1mOYDnxdfDOAABdo--ZWUA572.png" />
3.apache的存取控制
(1)設定ip的訪問(其中Order 按順序執行)
vim /etc/httpd/conf/httpd.conf
<Directory "/westos/html"> 允許所有人訪問westos目錄但是拒絕250主機
Order Allow,Deny
Allow from All
Deny from 172.25.254.250
</Directory>
<Directory "/westos/html"> 只允許250主機訪問westos目錄
Order Deny,Allow
Allow from 172.25.254.250
Deny from All
</Directory>
(2)設定使用者的訪問
htpasswd -m /etc/httpd/accessuser admin 建立訪問的使用者認證檔案 (-c create -m 指定名稱)
注意:第一個建立的使用者需要加c ,以後建立的使用者不需要加c,直接指定-m
建立訪問使用者admin admin1 指定名稱為authfile
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/9E/40/wKioL1mOaADxVdAbAAByj8pN0zY509.png" style="float:none;" title="Screenshot from 2017-08-11 16-14-14.png" alt="wKioL1mOaADxVdAbAAByj8pN0zY509.png" />
配置認證配置
vim /etc/httpd/conf/httpd.conf
<Directory "/westos/html">
AuthUserFile /etc/httpd/conf/authfile 使用者認證檔案
AuthName "Please input your name and password !!" 使用者認證提示資訊
AuthType basic 認證類型 基礎認證
Require valid-user 認證使用者,認證檔案中所有使用者都可以通過
Require user admin 只允許認證檔案中admin使用者訪問
</Directory>
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/40/wKioL1mOaqHC7xa1AACQ_8egKXw123.png" title="Screenshot from 2017-08-12 10-40-04.png" alt="wKioL1mOaqHC7xa1AACQ_8egKXw123.png" />
將認證檔案放到/etc/httpd/conf,重啟服務
650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/9E/51/wKiom1mOaAHROwhbAAAtuwpvM9E287.png" style="float:none;" title="Screenshot from 2017-08-11 16-17-14.png" alt="wKiom1mOaAHROwhbAAAtuwpvM9E287.png" />
再次訪問172.25.254.128,需要輸入認證帳號和密碼
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/9E/40/wKioL1mOaALRL-NxAAD-MLpedqk507.png" style="float:none;" title="Screenshot from 2017-08-11 16-17-40.png" alt="wKioL1mOaALRL-NxAAD-MLpedqk507.png" />
輸入帳號密碼後進入到訪問頁面
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/9E/51/wKiom1mOaAOBO8Y2AAA6eK1q1Ow814.png" style="float:none;" title="Screenshot from 2017-08-11 16-17-59.png" alt="wKiom1mOaAOBO8Y2AAA6eK1q1Ow814.png" />
四.apache的虛擬機器主機
1.定義
可以讓我們的一台apache伺服器在被訪問不同網域名稱的時候顯示不同的首頁
2.建立測試頁
建立目錄測試發布檔案並將預設目錄改為/var/www/html/
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/9E/40/wKioL1mObPXRWLgSAAC7eEls154751.png" style="float:none;" title="Screenshot from 2017-08-11 16-44-03.png" alt="wKioL1mObPXRWLgSAAC7eEls154751.png" />
建立news.westos.com和sport.westos.com目錄
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9E/52/wKiom1mObPfQCsIaAACSfjsPWz0780.png" style="float:none;" title="Screenshot from 2017-08-11 16-48-42.png" alt="wKiom1mObPfQCsIaAACSfjsPWz0780.png" />
建立news下的發布檔案
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/9E/52/wKiom1mObPax4A1JAAAiyAHWh2w595.png" style="float:none;" title="Screenshot from 2017-08-11 16-47-30.png" alt="wKiom1mObPax4A1JAAAiyAHWh2w595.png" />
建立sports下的發布檔案
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/9E/40/wKioL1mObPaCgLVeAAArUif9z6w269.png" style="float:none;" title="Screenshot from 2017-08-11 16-48-34.png" alt="wKioL1mObPaCgLVeAAArUif9z6w269.png" />
3.配置
cd 到/etc/httpd/conf.d/
(1)建立未指定網域名稱的訪問設定檔
vim /etc/httpd/conf.d/default.conf 未指定網域名稱的訪問都訪問default
<Virtualhost _default_:80> 虛擬機器主機開啟的連接埠
DocumentRoot "/var/www/html" 虛擬機器主機的預設發布目錄
CustomLog "logs/default.log" combined 虛擬機器主機日誌
</Virtualhost>
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/9E/40/wKioL1mObPeQweweAAA2pwigz8g476.png" style="float:none;" title="Screenshot from 2017-08-11 16-54-04.png" alt="wKioL1mObPeQweweAAA2pwigz8g476.png" />
(2)建立news.westos.com網域名稱的訪問檔案
vim /etc/httpd/conf.d/news.conf 指定網域名稱news.westos.com的訪問到指定預設發布目錄中
<Virtualhost *:80>
ServerName news.westos.com
DocumentRoot /var/www/westos/news.westos.com
CustomLog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/westos/news.westos.com"> 預設發布目錄的訪問授權
Require all granted
</Directory>
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/9E/40/wKioL1mObPiBaiT_AACuYYbBFuY494.png" style="float:none;" title="Screenshot from 2017-08-11 17-01-49.png" alt="wKioL1mObPiBaiT_AACuYYbBFuY494.png" />
(3)建立sports.westos.com網域名稱的訪問檔案
vim /etc/httpd/conf.d/sports.conf 指定網域名稱sports.westos.com的訪問到指定預設發布目錄中
<Virtualhost *:80>
ServerName sports.westos.com
DocumentRoot /var/www/westos/sports.westos.com
CustomLog "logs/sports.log" combined
</Virtualhost>
<Directory "/var/www/westos/sports.westos.com"> 預設發布目錄的訪問授權
Require all granted
</Directory>
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/9E/40/wKioL1mObPjjQ1TQAAC0PlpGNsw654.png" style="float:none;" title="Screenshot from 2017-08-11 17-02-30.png" alt="wKioL1mObPjjQ1TQAAC0PlpGNsw654.png" />
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9E/52/wKiom1mObPnRP308AABcnH5U9Lg583.png" style="float:none;" title="Screenshot from 2017-08-11 17-05-47.png" alt="wKiom1mObPnRP308AABcnH5U9Lg583.png" />
4.測試
在172.25.254.28上進行測試
在瀏覽器所在主機中配置本地解析檔案 vim /etc/hosts
172.25.254.128 www.westos.com news.westos.com sports.westos.com
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9E/41/wKioL1mOc42hKTM9AABFhC-N_sY495.png" style="float:none;" title="Screenshot from 2017-08-11 17-13-58.png" alt="wKioL1mOc42hKTM9AABFhC-N_sY495.png" />
在瀏覽器開啟www.westos.com,訪問到預設檔案上
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/9E/41/wKioL1mOc4yziVNWAAAx0sGDGAM340.png" style="float:none;" title="Screenshot from 2017-08-11 17-11-35.png" alt="wKioL1mOc4yziVNWAAAx0sGDGAM340.png" />
在瀏覽器開啟news.westos.com,訪問到news檔案上
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9E/52/wKiom1mOc42gh9b_AAAz8dneDTM522.png" style="float:none;" title="Screenshot from 2017-08-11 17-11-46.png" alt="wKiom1mOc42gh9b_AAAz8dneDTM522.png" />
在瀏覽器開啟sports.westos.com,訪問到sports檔案上
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/9E/52/wKiom1mOc47BR7HbAAA4H33tvqM070.png" style="float:none;" title="Screenshot from 2017-08-11 17-14-19.png" alt="wKiom1mOc47BR7HbAAA4H33tvqM070.png" />
Linux的Apache 服務