HTTP概述:php
Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源代碼的網頁服務器軟件,能夠在大多數電腦操做系統中運行,因爲其跨平臺和安全性。被普遍使用,是最流行的Web服務器軟件之一。它快速、可靠而且可經過簡單的API擴充,將Perl/Python等解釋器編譯到服務器中。html
http協議:web
應用層協議:超文本傳輸
http/0.9
http/1.0:cache, MIME
MIME: multipurpose internet mail extensions
http/1.1:緩存功能,條件式請求;
speedy: SPDY
http/2.0:
apache
一次完整的Http請求處理過程:
windows
(1) 創建或處理鏈接請求;
(2) 接收請求;
(3) 解析請求,處理請求;
(4) 加載用戶請求的資源;
(5) 構建響應報文;
(6) 發送響應報文;
(7) 記錄訪問於日誌中;centos
httpd的特性:瀏覽器
高度模塊化設計:core modules + standard modules + 3rd party modules
DSO: Dynamic Shared Object
MPM: multipath process modules
prefork:process
每進程響應一個請求;
worker: thread
每線程響應一個請求;
event: thread
每進程響應多個請求;
豐富功能:
CGI:動態網站;
虛擬主機:IP,PORT,ServerName
反向代理:http, fcgi, wsgi, ajp, ...
負載均衡:
緩存
httpd的安裝基礎應用
安全
一、yum安裝httpdbash
[root@localhost ~]# yum install httpd Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Package httpd-2.4.6-40.el7.centos.x86_64 already installed and latest version Nothing to do
二、程序環境
主程序文件:
/usr/sbin/httpd
模塊文件:
/usr/lib64/httpd/modules/*.so
主配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
/etc/httpd/conf.modules.d/*.conf
站點文檔路徑:
/var/www/html
日誌文件路徑:
/var/log/httpd/
access_log:訪問日誌
error_log:錯誤日誌
Unit File:
/usr/lib/systemd/system/httpd.service
自帶腳本:
/usr/sbin/apachectl
三、httpd常見配置的修改
1)監聽的地址和修改
編輯主配置文件/etc/httpd/conf/httpd.conf
2)保持鏈接
tcp鏈接創建後,資源獲取完成以後不會斷開鏈接,而是繼續等待請求其它資源,當時間超出規定時間或者傳輸的數量超過限制,則會斷開
#定義一個頁面文件 [root@localhost httpd]# cat /var/www/html/index.html <h1> Hello World<h1>
使用瀏覽器訪問
添加一個配置文件,將保持連接關閉,查看效果
[root@localhost httpd]# vi conf.d/keepalive.conf [root@localhost httpd]# cat conf.d/keepalive.conf KeepAlive Off [root@localhost httpd]# systemctl restart httpd [root@localhost httpd]#
3)DSO
模塊的動態裝卸機制,若是想禁止哪一個模塊,即在配置文件中將其註釋掉便可
httpd命令:
-t -D DUMP_MODULES : show all loaded modules
-M : a synonym for -t -D DUMP_MODULES
httpd -M 發現所裝載的模塊中有個suexec模塊
在/etc/httpd/conf.modules.d/00-base.conf裏將這個模塊註釋掉
4)定義站點主頁:
DirectoryIndex filename1 filename2 ...
訪問網站時候時若是沒有指定URL路徑,系統會根據DirectoryIndex的配置找到主頁文件,若是沒有找到,則會被重定向到一個錯誤頁面。DirectoryIndex能夠有多個值,若是第一個存在,就是使用第一個,若是不存在,自左而右找,直到找到爲止。
編輯配置文件並在DirectoryIndex中添加index.php
將以前的主頁文件更名,並新建一個名爲index.php的文件
[root@localhost httpd]# mv /var/www/html/index.html /var/www/html/index.html.bak [root@localhost httpd]# vi /var/www/html/index.php [root@localhost httpd]# cat /var/www/html/index.php <h1>Index Php<h1> [root@localhost httpd]#
重啓服務並訪問ip地址
由於以前的index.html找不到,因此向右找到了index.php
5)Main Server相關配置
(1) DocumentRoot
站點文檔根路徑;
更改站點根目錄問/web/htdocs
建立目錄並添加主頁文件
[root@localhost httpd]# mkdir -pv /web/htdocs mkdir: created directory ‘/web’ mkdir: created directory ‘/web/htdocs’ [root@localhost httpd]# echo "<h1>Web Htdocs<h1>" /web/htdocs/index.html <h1>Web Htdocs<h1> /web/htdocs/index.html [root@localhost httpd]# echo "<h1>Web Htdocs<h1>" > /web/htdocs/index.html
訪問站點,但好像不是咱們指望的結果
這是由於,雖然指定了新的DocumentRoot,可是並無給新的目錄受權!查看錯誤日誌可看出
讓咱們給新的站點根目錄受權
重載服務並訪問
(2)站點文檔訪問受權及衆多服務特性的配置:
基於文件系統路徑:
<Directory "/PATH/TO/DIR">
</Directory>
<File "">
</File>
....
基於URL進行:
<Location "URL">
...
</Location>
<LocationMatch ~ "URL_PATTERN">
...
</LocationMatch>
其中的各屬性配置:
Options
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
None表示都不啓用,All表示都啓用
Indexes:索引
FollowSymLinks:容許跟蹤符號連接
ExecCGI:容許執行CGI腳本
AllowOverride(一般都使用None)
httpd的訪問控制配置,容許每目錄單獨進行;在每一個目錄下創建一個.htaccess文件;
AllowOverride表示是否容許目錄中的.htaccess文件中的配置來覆蓋當前配置段中的配置;
Options FileInfo AuthConfig Limit
All
None
基於源地址的訪問控制
容許全部地址訪問:Require all granted
拒絕全部地址訪問:Require all denied
<RequireAll>
</RequireAll>
基於IP控制:
Require ip ADDRESS
Require not ip ADDRESS
ADDRESS能夠是單個ip,也能夠寫某個網段
基於主機名控制:
Require host HOSTNAME
Require not host HOSTNAME
HOSTNAME能夠是主機名也能夠是域名
6)User/Group
進程的運行者身份;
http進程運行時是以apache用戶apache組來進行的。
7)路徑別名
Alias /URL/ /PATH/TO/SOME_DIR/
經過別名映射到真正的目錄上
建立一個目錄,並在此目錄下建立一個index.html的文件
[root@localhost ~]# mkdir /data/bbs -pv mkdir: created directory ‘/data/bbs’ [root@localhost ~]# echo "<h1>Alias BBS Page<h1>" > /data/bbs/index.html
在主配置文件中添加如下代碼
httpd的官方手冊頁,須要安裝httpd-manual包而後進行相應的配置,便能正常訪問
安裝httpd-manual包
[root@localhost ~]# yum install httpd-manual Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Package httpd-manual-2.4.6-40.el7.centos.noarch already installed and latest version Nothing to do
配置文件:conf.d/manual.conf
[root@localhost ~]# cat /etc/httpd/conf.d/manual.conf # # This configuration file allows the manual to be accessed at # http://localhost/manual/ # AliasMatch ^/manual(?:/(?:de|en|fr|ja|ko|ru))?(/.*)?$ "/usr/share/httpd/manual$1" <Directory "/usr/share/httpd/manual"> Options Indexes AllowOverride None Require all granted </Directory>
9)status page(經過status模塊擴展之後生成)
編輯配置文件/etc/httpd/conf.d/status.conf
錯誤日誌:
ErrorLog "/var/log/httpd/error_log"
警告級別:Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
訪問日誌:
LogFormat "FORMAT_STRINGS" LOG_FORMAT_NAME
CustomLog "/PATH/TO/LOG_FILE" LOG_FORMAT_NAME
11)虛擬主機
主機標識方式:
IP不一樣
PORT不一樣
ServerName
<VirtualHost IP:PORT>
ServerName
DocumentRoot
<Directory "">
...
Require all granted
</DIrective>
ErrorLog
CustomLog
</VirtualHost>
實例:
基於ip和端口方式混合的方式
建立一系列文件夾以及網站主頁文件
[root@localhost ~]# mkdir -pv /vhosts/www{1,2,3} mkdir: created directory ‘/vhosts’ mkdir: created directory ‘/vhosts/www1’ mkdir: created directory ‘/vhosts/www2’ mkdir: created directory ‘/vhosts/www3’ [root@localhost ~]# echo "<h1>Vhosts www1<h1>" > /vhosts/www1/index.html [root@localhost ~]# echo "<h1>Vhosts www2<h1>" > /vhosts/www2/index.html [root@localhost ~]# echo "<h1>Vhosts www3<h1>" > /vhosts/www3/index.html [root@localhost ~]#
添加/etc/httpd/conf.d/vhosts.conf文件
[root@localhost ~]# vi /etc/httpd/conf.d/vhosts.conf <VirtualHost 10.1.0.26:80> ServerName www1.xiaoshui.com DocumentRoot "/vhosts/www1" <Directory "/vhosts/www1"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 10.1.0.26:8080> ServerName www2.xiaoshui.com DocumentRoot "/vhosts/www2" <Directory "/vhosts/www2"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 10.1.0.27:80> ServerName www3.xiaoshui.com DocumentRoot "/vhosts/www3" <Directory "/vhosts/www3"> Options None AllowOverride None Require all granted "/etc/httpd/conf.d/vhosts.conf" 27L, 605C
添加臨時地址10.1.0.27
[root@localhost ~]# ifconfig eno16777736:0 10.1.0.27/16 [root@localhost ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.0.26 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe27:9b0e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:27:9b:0e txqueuelen 1000 (Ethernet) RX packets 4769 bytes 445376 (434.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1137 bytes 151773 (148.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.0.27 netmask 255.255.0.0 broadcast 10.1.255.255 ether 00:0c:29:27:9b:0e txqueuelen 1000 (Ethernet)
重啓服務並分別訪問之
基於主機名的方式
更改/etc/httpd/conf.d/vhosts.conf文件,將ip地址改成相同的,以下
[root@localhost ~]# vi /etc/httpd/conf.d/vhosts.conf <VirtualHost *:80> ServerName www1.xiaoshui.com DocumentRoot "/vhosts/www1" <Directory "/vhosts/www1"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName www2.xiaoshui.com DocumentRoot "/vhosts/www2" <Directory "/vhosts/www2"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName www3.xiaoshui.com DocumentRoot "/vhosts/www3" <Directory "/vhosts/www3"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
在windows的HOSTS文件中添加相對的主機名和ip地址的解析
12)基於用戶的訪問控制
◆上面提到的Require的機制
Require user USERLIST
Require group GRPLIST
◆虛擬用戶 (須要帳號密碼驗證)
認證方式:
basic
digest
<Directory "">
Options None
AllowOverride None
AuthType Basic
AuthName "STRING"
AuthUserFile ""
Require user USER1 USER2 ...
</Directory>
帳號文件生成工具htpasswd
htpasswd [options] "/PATH/TO/HT_PASSWD_FILE" username
-c:建立此文件;
-m:md5加密密碼存放;
-s:sha加密
-D: 刪除指定用戶
基於組進行認證:
<Directory "/vhosts/www1/admin">
Options None
AllowOverride None
AuthType Basic
AuthName "Admin Area, Enter your name/pass"
AuthUserFile "/etc/httpd/conf/.htpasswd"
AuthGroupFile "/etc/httpd/conf/.htgroup"
Require group GRPNAME1 GRPNAME 2
</Directory>
組帳號文件:
每行定義一個組
group_name: user1 user2 ...
實例:
在上面的/etc/httpd/conf.d/vhost.conf文件中作如下修改
#第一次建立時須要加-m選項,之後添加用戶時只需-m選項,若是再加-c選項,會將之前的用戶抹掉 [root@localhost ~]# htpasswd -c -m "/etc/httpd/conf/.htpasswd" tom New password: Re-type new password: Adding password for user tom [root@localhost ~]# htpasswd -m "/etc/httpd/conf/.htpasswd" xiaoshui New password: Re-type new password: Adding password for user xiaoshui
重啓服務並訪問之
謝謝瀏覽...