一、相關概念:
1、 HTTP/0.9:(Hyper Text Transfer Protocol)超文本傳輸協定,僅純文字(超連結),ASCII;核心:HTML(Hyper TextMark Language)超文本标記語言。
2、 HTTP/1.0:引入MIME(Multipurpose Internet Mail Extension)多用途網際網路郵件擴充,使網際網路走進我們的生活,GET|POST|HEAD|PUT|TRACE|OPTIONS|CONNECTION|DELETE;SMTP(Simple MailTransaction Protocol),純文字。
注:MIME:将非文本資料在傳輸前重新編碼為文本格式,接收方能夠用相反的方式将其重新還原為原來的格式,還能夠調用相應的程式來打開此檔案。
3、 HTTP/1.1:加強了緩存管理機制;引入了長連接配接機制(并發連接配接量大時,後面的用戶端将要排隊等待有斷開的連接配接然後再與伺服器建立連接配接)。
4、URI(Uniformresource identify)統一資源辨別符(全球唯一辨別),路徑、格式上的統一;
URL(Uniform resourceLocater)統一資源定位符,通常稱WEB對象(WEB資源)網際網路上的統一表示格式,URI包含URL。
URI格式:protocol://host:port/path/to/file
URL格式:/path/to/file
socket套接字:IP+PORT
5、動态網頁:伺服器端存儲的文檔非HTML格式,而是程式設計語言開發的腳本,腳本接受參數之後在伺服器運作一次,運作完成之後會生成HTML格式的文檔,把生成的文檔發給用戶端;動态網頁包含動态内容(index.php)和靜态内容(index.html,引用N個WEB對象URL),動态内容部分才需執行。
6、HTTP封包:
請求封包文法:<method> <request_URL> <version>
<headers>
空行
<entity-body>
響應封包文法:<version> <status> <reason-phrase>
<entity-body>
注:封包的第一行通常稱作“起始行”(start line),後面的标簽格式的内容稱作首部域(header field),每個首部域由名稱name和值value組成,中間用逗号分隔,另,響應封包通常還有一個稱作body的資訊主體,即響應給用戶端的内容。
7、狀态代碼:
1XX:純資訊;
2XX:成功類資訊;
3XX:重定向類的資訊(301、302、304);
4XX:client-side錯誤資訊(404,請求了不存在的檔案,notfound);
5XX:server-side錯誤資訊(程序沒啟動成功之類的)。
8、WEB服務的主要操作:
I:建立連接配接:接受或拒絕用戶端連接配接請求;
II:接收請求:通過網絡讀取HTTP請求封包;
III:處理請求:解析請求封包并作出相應的動作;
IV:通路資源:通路請求資源中相關資源;
V:建構響應:使用正确的首部生成HTTP響應封包;
VI:發送響應:向用戶端發送生成的響應封包;
VII:記錄日志:将已經完成的HTTP事務記錄進日志檔案。
9、ASF(apache software foundation)、FSF(free software foundation)、GNU、GPL
10、httpd特性:事先建立程序;按需維護适當的程序;子產品設計,核心比較小,各種功能都子產品添加,包括PHP;支援運作時配置,支援單獨編譯子產品;支援多種方式的虛拟主機配置;支援HTTPS(mod_ssl);支援使用者認證;支援基于主機名的ACL;支援每目錄的通路控制;支援URL重寫等等。
注:httpd受SELINUX控制。
10、httpd-devel (開發包)
11、/usr/sbin/httpd MPM(multi path module)多道處理子產品預設是prefork模型,支援worker模型(通過修改/etc/sysconfig/httpd檔案更改使用哪種模型)
prefork:一個請求用一個程序響應,穩定可靠,但并發量大時來回切換導緻程序抖動,效率低,此模式會生成一個主導程序master process(屬主屬組均為root)用來管理維護worker process的生成與銷毀等,其餘程序為worker process工作程序,用來響應使用者請求(屬主屬組均為apache);
worker:一個請求用一個線程響應,會啟動多個程序,每個程序生成多個線程;
event:一個程序響應多個使用者請求,内部用多線程響應請求(最強大)。
注:#httpd -l ;httpd.worker -l ; httpd.event -l
12、搭建環境:linux+apache+mysql+php
linux+nginx+mysql+php
windows+IIS+SQLserver+asp
rpm包安裝所需軟體:httpd,mysql,mysql-server,php,php-devel,php-mysql.
配置檔案:/usr/local/apache2/etc/httpd.conf
/usr/local/apache2/etc/extra/*.conf
/etc/httpd/conf/httpd.conf (RPM包安裝位置
#grep “Section” httpd.conf(若rpm安裝,此配置檔案分三段配置)
GlobalEnvironment
‘Main’Server configuration
VirtualHosts
端口:tcp(80)、ssl(443).
預設網頁儲存位置:/usr/local/apache2/htdocs/
/var/www/html (RPM包
日志儲存位置:/usr/local/apache2/logs/
/var/log/httpd/ (RPM包
二、1、常用配置項(格式:directive指令 value值)指令不區分大小寫,值根據需要有可能區分:
Section1 Global Environment
ServerTokens OS (可用選項有major|minor|Min|Prod|OS|Full)網頁出錯時在用戶端浏覽器最下行顯示具體資訊,軟體版本及作業系統版本)
ServerRoot (伺服器根目錄、工作目錄)
PidFile run/httpd.pid
Timeout 60
KeepAlive On (打開長連接配接)
MaxKeepAliveRequest 100 (限制共100個長連接配接,0為無限制)
KeepAliveTimeout 15
<IfModule prefork.c>
StartServers 8 (剛一啟動打開的程序數)
MinSpareServers 5 (最少空閑程序)
MaxSpareServers 20 (最大空閑程序)
ServerLimit 256
MaxClients 256 (最多client連進來,調整此項之前事先調ServerLimit,注:調整時先關閉服務調好後再開啟)
MaxRequestsPerChild 4000 (一個程序最多響應多少次請求,0不限制)
</IfModule>
Listen (可有多個
LocalModule moudulename module path(加載的相關子產品)
Include conf.d/*.conf (子目錄下的配置檔案也生效)
User
Group
Section2 ‘Main’ Server
ServerAdmin root@localhost(每個站點配個伺服器管理者)
ServerName www.example.com:80(伺服器名,沒有域名解析時使用臨時解析,預設不開啟;若配置虛拟主機時這項必須配置,在開啟服務時會反向解析IP與域名對應關系,檢測不到會報錯)
DocumentRoot “/var/www/html” (文檔根目錄,URL與本地檔案中的路徑不同)
<Directory “/var/www/html”>
Options Indexes FollowSymLinks (生産環境下Indexes必須關掉,除下載下傳站外,Indexes(當通路頁面不存在時會顯示并索引網頁目錄下的所有目錄及檔案),FollowSymLinks(軟連結指向的檔案生效)
AllowOverride None (若此項值為AuthConfig,則表明啟用使用者認證)
Allow from all
</Directory>
<IfModule mod_userdir.c> (使用者家目錄下的網頁,需切換至username并在家目錄下建立public_html目錄 ,http://ip:port/~username)
UserDir disabled
#UserDir public_html
DirectoryIndex index.html index.php (從左至右依次比對,若無比對則檢視是否索引,若還無傳回forbidden)
AccessFileName .htaccess(每目錄通路的權限設定,但運作效率極低,生産環境下禁用)
<Files ~ "^\.ht"> (模式比對,此項與安全相關)
Order allow,deny
Deny from all
Satisfy All
</Files>
TypesConfig /etc/mime.types (MIME,以下幾項與傳輸多媒體檔案相關)
DefaultType text/plain
<IfModule mod_mime_magic.c>
# MIMEMagicFile /usr/share/magic.mime
MIMEMagicFile conf/magic
ErrorLog logs/error_log (錯誤日志)
LogLevel warn
LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined(混合模式,%h代表宏,具體有%h是remote host,%l是remotelogin,%r是first line of request,%s是status)
LogFormat "%h %l %u %t\"%r\" %>s %b" common(通用模式)
LogFormat "%{Referer}i -> %U"referer(隻記錄client通路來源)
LogFormat "%{User-agent}i" agent(隻記錄浏覽器類型)
CustomLog logs/access_log combined (正确通路日志)
Alias /icons/ "/var/www/icons/" (目錄别名)
三、範例:
1、使用者身份驗證:
#vim /etc/httpd/conf/httpd.conf
Options None
AllowOverride AuthConfig
AuthType Basic
AuthName “Restricted site.”
AuthUserFile “/etc/httpd/conf/htpasswd” (也可設定允許組通路,AuthGroupFile “path”,Require group groupname)
Require user hadoop (僅允許某使用者通路,若讓檔案中所有使用者都通路則為,Require valid-user)
Order allow,deny
Allow from all
#htpasswd -c -m /etc/httpd/conf/htpasswd hadoop (僅第一次添加使用者使用-c,-m使用MD5加密算法)
#htpasswd -m /etc/httpd/conf/htpasswd tomcat (第二次之後添加使用者不用-c選項)
#httpd -t (測試配置檔案文法)
2、定義主機通路控制機制:
#vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html”>
……
Order deny,allow (先拒絕後允許,2.4版本不使用allow/deny方式
Deny from 192.168.1.0/24 (位址的表示方式:IP、network/netmask、domainname、hostname、particularIP:172.16(表示172.16.0.0/16))
3、虛拟主機(實體伺服器1個,WEB程式也1個,卻可服務多個不同站點):
生産中常用的是基于域名的虛拟主機
#vim /etc/httpd/conf.d/virtual.conf
NameVirtualHost 192.168.1.222:80
<VirtualHost 192.168.1.222:80>
DocumentRoot “/www/a.org”
CustomLog “/var/log/htttpd/a.org/access_log” combined
<Directory “/www/a.org”>
Options None
AllowOverride AuthConfig
AuthType Basic
AuthName “RESTRICTED SITE.”
AuthUserFile “/www/a.org/.htaccess”
Require valid-user
</Directory>
</VirtualHost>
DocumentRoot “/www/b.net”
CustomLog “/var/log/httpd/b.net/access_log” combined
#service httpd restart(登出掉主配置檔案的中心主機DocumentRoot一行,再重新開機驗證)
篩選出httpd.conf除注釋及空白行的幹淨檔案:原991行-->223行
sed '/^#/d' /etc/httpd/conf/httpd.conf |sed -r '/[[:space:]]+#/d' | grep -v "^$" > /tmp/httpd.conf.clear
四、基于openssl的https服務配置:
角色有:CA-side(第三方機構伺服器,redhat6.4)、server-side(網站伺服器)、client(windows浏覽器通路即可)
注:ssl會話僅能用于基于IP的虛拟主機,若虛拟主機基于域名那僅有一個域名能建立ssl會話
CA-side:
#cd /etc/pki/CA
#(umask 077;openssl genrsa -out private/cakey.pem 2048)(CA端私鑰)
#vim ../tls/openssl.cnf(更改預設值,确認dir = /etc/pki/CA)
#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655(生成自簽署證書)
#touch index.txt
#touch serial
#echo 01 > serial
注:生成的私鑰cakey.pem,自簽署證書cacert.pem,index.txt記錄所有請求認證的記錄,serial版本号記錄到第幾條,這些檔案名已在配置檔案openssl.cnf中定義,為友善直接使用。
server-side:
web要支援openssl得要有相應子產品
#httpd -M | grep ssl
#yum -y install mod_ssl
#mkdir /etc/httpd/ssl
#cd /etc/httpd/ssl
#(umask 077;openssl genrsa -out httpd.key 1024)(server端私鑰)
#openssl req -new -key httpd.key -out httpd.csr(server端生成的證書簽署請求certificatesignature request,需傳至CA端簽署,注意此處寫相應資訊時主機名寫hello.magedu.com最終要通路的域名,其餘資訊與CA一緻)
#cd /tmp
#openssl ca -in httpd.csr -out httpd.crt -days 3655(CA簽署證書請求,生成的證書為安全,httpd.crt傳至server-side後要删除)
#cat /etc/pki/CA/index.txt
#cat /etc/pki/CA/serial
#cd /etc/httpd/ssl
#rpm -ql mod_ssl
#vim /etc/httpd/conf.d/ssl.conf(是子配置檔案,需要注意及更改的資訊如下)
Listen 443
<VirtualHost 192.168.1.222:443>
ServerName hello.magedu.com:443
DocumentRoot "/www/magedu.com"
TransferLog logs/ssl_access_log(注意是transferlog記錄日志而不是customlog)
SSLEngine on(啟用ssl功能,off禁用很關鍵)
SSLProtocol all -SSLv2(注意減v2,隻有SSLV3和TLSV1)
SSLCertificateFile /etc/httpd/ssl/httpd.crt(CA簽署的證書)
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key(私鑰)
#mkdir -pv /www/magedu.com
#vim /www/magedu.com/index.html
hello.magedu.com
#httpd -t(檢查配置檔案文法)
#vim /etc/hosts
192.168.1.222 hello.magedu.com
#service httpd restart
#netstat -tulnp | grep :443
client-side:
1、将CA-side端cacert.pem公鑰傳至client-side,并重命名為cacert.crt,輕按兩下安裝證書
2、給windows添加解析記錄C:\Windows\System32\drivers\etc\hosts最末添加IP與域名對應關系:192.168.1.222 hello.magedu.com
3、通過浏覽器通路:https://hello.magedu.com
例:
生産中将vsftpd的根目錄作為httpd的根目錄(vsftpd&httpd均yum方式安裝):
101.73(vsftpd):
虛拟使用者/密碼:ane56/ane56!
資料目錄:/ane/ftproot/
隐藏httpd版本:
sed -i 's/ServerTokens OS/ServerTokensProductOnly/' /etc/httpd/conf/httpd.conf
sed -i 's/ServerSignatureOn/ServerSignature Off/' /etc/httpd/conf/httpd.conf
#vim /etc/httpd/conf/httpd.conf
ServerSignature Off
ServerTokens PorductOnly #(僅在ServerSignature為On時此項才生效,有ProductOnly、Major、Minor、Minimal、OS、Full)
DocumentRoot "/ane/ftproot"
<Directory "/ane/ftproot">
Options Indexes
AllowOverride All
IndexOptions FancyIndexing NameWidth=25 Charset=UTF-8 FoldersFirstVersionSort
IndexOrderDefault Ascending Name
Allow from all
#mv /etc/httpd/conf.d/welcome.conf/etc/httpd/conf.d/welcome.conf.bak
[root@PDA4-ane ~]# id virtual
uid=501(virtual) gid=501(virtual) groups=501(virtual)
[root@PDA4-ane ~]# usermod -a -G 501 apache
[root@PDA4-ane ~]# tail -2 /etc/group
virtual:x:501:apache
apache:x:48:
#service httpd restart
Options Indexes #Indexes使目錄内的檔案或子目錄以清單方式顯示
IndexOptions FancyIndexing #This turns on fancy indexing ofdirectories.
IndexOptions ScanHTMLTitles #如果目錄中含有HTML檔案,則Apache會自動讀取HTML檔案的<title>......</title>部分,用 HTML檔案的标題作為Description(描述)顯示在清單目錄中的Description部分。。(注意:加載該指令會加大CPU的負荷!)
IndexOptions NameWidth=25 #指定目錄清單可以顯示最長為25位元組的檔案/目錄名,如果使用*來做值,會自适應到最長檔案名
IndexOptions DescriptionWidth=256 #指定目錄清單可以顯示最長為256位元組的檔案/目錄描述内容
IndexOptions HTMLTable #允許HTML格式
IndexOptions VersionSort #如果目錄中含有同一檔案的不同版本,則Apache會對該檔案按照版本号自動排序
IndexOptions FoldersFirst #最先列出檔案夾會使顯示效果
IndexOptions IgnoreCase #忽略大小寫
IndexOptions Charset=GBK #預設顯示字元集
IndexOrderDefault Ascending Name #預設以檔案或目錄名升序排列
#AddDescription "Lua ... "lua-5.1.3.tar.gz #給檔案lua-5.1.3.tar.gz添加注釋
以上是學習《馬哥課程》做的筆記。
源碼包安裝的apache虛拟主機及個人首頁的配置:
基于IP的虛拟主機:一台伺服器,多個IP,搭建多個網站
基于端口的虛拟主機:一台伺服器,一個IP,搭建多個網站,每個網站使用不同的端口通路
基于域名的虛拟主機:一台伺服器,一個IP,搭建多個網站,每個網站使用不同域名通路
#vi /usr/local/apache2/etc/httpd.conf
打開 Include etc/ /extra/httpd-vhosts.conf (以下三個實驗,均需打開此項
》例:基于IP的虛拟主機
#ifconfig eth0:1 192.168.216.227 netmask 255.255.255.0 (臨時生效
#cp ifcfg-eth0 ifcfg-eth0:1 (永久生效需改配置檔案
#vi /usr/local/apache2/etc/extra/httpd-vhost.conf
<VirtualHost 192.168.216.226>
DocumentRoot "/usr/local/apache2/htdocs/baidu"
ServerName www.baidu.com
<Directory "/usr/local/apache2/htdocs/baidu">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.216.227>
DocumentRoot "/usr/local/apache2/htdocs/xdl"
ServerName www.xdl.com
<Directory "/usr/local/apache2/htdocs/xdl"
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
#mkdir –v /usr/local/apache2/htdocs/{baidu,xdl}
#cd baidu
#echo "www.baidu.com" >> index.html
#cd xdl
#echo "www.xdl.com" >> index.html
#vi /usr/local/apache2/etc/httpd.conf
#DocumentRoot "/usr/local/apache2//htdocs" (關掉預設網頁設定
#/usr/local/apache2/bin/apachectl stop--->start
#elinks 192.168.216.226
#elinks 192.168.216.227
》例:基于端口的虛拟主機
#vi /usr/local/apache2/etc/extra/httpd-vhosts.conf (在上例的基礎上修改如下内容
Listen 8080
<VirtualHost 192.168.216.226:80>
<VirtualHost 192.168.216.226:8080>
#elinks 192.168.216.226:80
#elinks 192.168.216.226:8080
》例:基于域名的虛拟主機
布好域名伺服器
#vi /usr/local/apache2/etc/extra/httpd-vhosts.conf
<VirtualHost *:80>
…… (同上例中内容
#elinks www.baidu.com(www.xdl.com)
源碼包安裝個人首頁配置:
#vi /usr/lcoal/apache2/etc/httpd.conf
Include etc/extra/httpd-userdir.conf (打開子配置檔案
LoadModule userdir module modules/mod_userdir.so (打開子產品
#vi /usr/local/apache2/etc/extra/httpd-userdir.conf (此配置檔案已設定好,不需改動
#useradd zhangsan
#useradd lisi
#mkdir /home/zhangsan/public_html
#mkdir /home/lisi/public_html
#echo "zhangsan's HomePage" >> /home/zhangsan/public_html/index.html
#echo "lisi's HomePage" >> /home/lisi/public_html/index.html
#chmod o+x /home/zhangsan
#chmod o+x /home/lisi
#elinks 192.168.216.226/~zhangsan
#elinks 192.168.216.226/~lisi
本文轉自 chaijowin 51CTO部落格,原文連結:http://blog.51cto.com/jowin/1659486,如需轉載請自行聯系原作者