--
apacheweb伺服器
兩種包:一種是系統內建的httpd
另一種源碼包apache
httpd a patchy server-->apache
網頁技術: 靜態頁面和動態網頁面
靜態頁面: 都是格式化的標籤(html超文字標記語言 (HTML))組成的頁面,尾碼名有.html .htm
動態網頁面:使用動態指令碼(asp,php,jsp,cgi,fastcgi),一般會串連資料庫,有互動動作
常見的網站架構:
windows平台下: windows server 2003 + IIS + mssql + asp
lamp:linux+ apache + mysql + php
web伺服器
iis
apache
ngnix (enginee X)
tomcat jboss weblogic --中介軟體
[root@li ~]# yum list |grep httpd
This system is not registered with RHN.
RHN support will be disabled.
httpd.i386 2.2.3-31.el5 installed --rhel內建的rpm版的apache包
設定檔:/etc/httpd/conf/httpd.conf
守護進程:httpd
訪問協議:http(連接埠80)https (連接埠443) http+ssl(secure socket layer)
vim /etc/httpd/conf/httpd.conf
ServerTokens OS--向用戶端提供一些伺服器和web伺服器的資訊
--用這個命令可以查看一些網站的資訊curl -I www.uplooking.com
ServerRoot "/etc/httpd"--存放設定檔的主目錄
PidFile run/httpd.pid--指定存放pid的位置
Timeout 120--連線逾時時間
KeepAlive Off--rpm版預設是off,源碼版預設是on, 一次串連,多次請求
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule prefork.c>--apache的prefork模式
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
<IfModule worker.c>--apache的worker模式
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
Listen 80--監聽連接埠
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_alias_module modules/mod_authn_alias.so
LoadModule authn_anon_module modules/mod_authn_anon.so
...................--apache的動態模組調用,DSO (dynamic shared objects)
Include conf.d/*.conf--子設定檔
User apache
Group apache--定義訪問時的使用者名稱和使用者組,在源碼版裡預設是daemon
ServerAdmin root@localhost--管理郵箱
DocumentRoot "/www"--定義網站的根目錄
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
DirectoryIndex index.html index.html.var--定義網站訪問時的首頁檔案
AccessFileName .htaccess--網頁加密檔案
ErrorLog logs/error_log
LogLevel warn
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
/etc/init.d/httpd start
http://127.0.0.1/--第一次沒改設定檔啟動httpd,訪問自己,會看到一個紅帽test page
例一:建立網站首頁
在網站根目錄下 建立一個首頁檔案
echo 'main page' > /var/www/html/index.html
http://127.0.0.1/--就可以看到main page的資訊,也就是直接存取到首頁
例二:修改網站根目錄
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www"--修改網站根目錄為/www
<Directory "/www">--把這個也對應的修改為/www
mkdir /www
/etc/init.d/httpd reload--重裝啟apache服務
echo 'new main page' > /www/index.html
http://127.0.0.1/--看到新網站根目錄下的new main page資訊
例三:修改首頁檔案類型
vim /etc/httpd/conf/httpd.conf
DirectoryIndex abcd.html index.html index.html.var--修改網站首頁類型,優先找abcd.html,再去找index.html
/etc/init.d/httpd reload
echo 'abcd main page' > /www/abcd.html
elinks 127.0.0.1--現在看到的就是abcd main page,因為abcd.html是優先的
例四:
DefaultLanguage zh-CN--預設語言集為中文
LanguagePriority zh-CN en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-TW--語言優先順序,把中文寫到最前面
---------------------------------------------------------------------------
--關於apache的標籤
directory(針對目錄) file(針對檔案) location(針對位置)
<Directory "/www">--對/www目錄的控制開始標籤
Options Indexes FollowSymLinks--允許清單形式,和符號連結
AllowOverride None--不使用.htaccess控制
Order allow,deny
Allow from all--這兩名就是允許/www下的所有檔案被訪問
</Directory>--對/www目錄的控制結束
indexes --指當找不到預設的首頁檔案時,就把此目錄下的檔案或目錄以列表的形式顯示出來
FollowSymLinks--允許符號連結,擴充了網站根目錄
AllowOverride--指定是否允許.htaccess控制
Order allow,deny--指定先允許,再拒絕
Allow from all--指定允許所有
例五:把網站的檔案修改成目錄列表的形式
Options Indexes FollowSymLinks--確認網站根目錄的options選項裡有indexes參數
vim /etc/httpd/conf.d/welcome.conf--開啟這個檔案(就是紅帽test page),注釋掉所有,或者是刪除此檔案
#<LocationMatch "^/+$">
# Options -Indexes
# ErrorDocument 403 /error/noindex.html
#</LocationMatch>--這四行定義了紅帽 test page,我在這裡全部注釋掉
/etc/init.d/httpd reload
elinks 127.0.0.1--可以看到網站根目錄下的所有檔案和目錄是以列表形式顯示
例六:
1,拒絕所有
Order deny,allow
deny from all--把對網站根目錄的許可權改為拒絕所有
/etc/init.d/httpd reload
elinks 127.0.0.1--所有檔案都不可訪問
2,拒絕所有,只允許10.1.1.35訪問
Order deny,allow
deny from all
allow from 10.1.1.35
/etc/init.d/httpd reload
elinks 10.1.1.35--只有35這台機可以訪問
3,當拒絕所有和允許所有都存在時
Order deny,allow
deny from all
allow from all--這個與iptables規則,和路由器的ACL不同,這是越後越生效,匹配前面的,還要再去匹配後面的規則,並且後面的規則生效
/etc/init.d/httpd reload
elinks 10.1.1.35--都可以訪問
Order allow,deny
allow from all
deny from all--按照後面生效的規則,這是拒絕所有
4,允許10.1.1.0網段訪問,但拒絕10.1.1.35訪問
Order allow,deny
allow from 10.1.1.0/255.255.255.0
deny from 10.1.1.35
例七:file標籤
<Directory "/www">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
<Files "ule01.txt">--針對/www目錄下的ule01.txt檔案的控制開始標誌,注意,這裡對大小寫敏感,所以不能對象UlE01.txt,uLe.01.txt等等做出控制
order deny,allow
deny from all
</Files>--結束標誌
</Directory>
/etc/init.d/httpd reload
http://10.1.1.35/ule01.txt --訪問不了,別的都可以訪問
例八:location 標籤
[root@li ~]# mkdir /www/aa
[root@li ~]# echo 'aa main page' > /www/aa/index.ht
[root@li ~]# mkdir /www/Aa
[root@li ~]# echo 'Aa main page' > /www/Aa/index.html
[root@li ~]# vim /etc/httpd/conf/httpd.conf--加上下面一段
<Location "/aa">--這個也是大小寫敏感
order deny,allow
deny from all
</Location>
/etc/init.d/httpd reload
http://10.1.1.35/aa/--這樣訪問是拒絕的
http://10.1.1.35/Aa/--可以看到Aa main page的字樣,表示沒受控制,驗證了大小寫敏感
例九:查看伺服器的status,info
vim /etc/httpd/conf/httpd.conf--開啟下面兩段,並把存取權限改為能訪問
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
allow from all --我這裏手動加上了allow from all,允許所有人訪問此伺服器的status
# Allow from .example.com
</Location>
<Location /server-info>
SetHandler server-info
Order deny,allow
Deny from all
allow from all--這個也是樣,手動加上了,允許所有人訪問
# Allow from .example.com
</Location>
/etc/init.d/httpd reload
http://10.1.1.35/server-status
http://10.1.1.35/server-info
--注意:location和directory如果控制的資源重合,或者相同,則location生效。
例十:口令檔案驗證
對/www目錄進行口令驗證
vim /www/.htaccess--因為是對/www目錄進行口令驗證,所以,在建立在/www目錄下
authname "your password?"
authtype Basic
authuserfile /etc/httpd/userpasswd--注意這個檔案路徑不要定義到/www或者/www子目錄下,防止使用者通過http協議下載
require valid-user
[root@li ~]# htpasswd -c /etc/httpd/userpasswd abc--建立密碼檔案,-C是建立的意思,abc使用者在這裡與系統使用者無關
New password:
Re-type new password:
Adding password for user abc
[root@li ~]# htpasswd /etc/httpd/userpasswd li--再加入驗證使用者就不用加-C參數
New password:
Re-type new password:
Adding password for user li
[root@li ~]# cat /etc/httpd/userpasswd --查看可以看到密碼是加密的
abc:FsWq5DHYkXX4c
li:yLQGK44T6EF4s
vim /etc/httpd/conf/httpd.conf
<Directory "/www">
Options Indexes FollowSymLinks
AllowOverride all--把none改成all,表示對/www目錄啟用.htacess驗證功能
Order allow,deny
allow from all
</Directory>
/etc/init.d/httpd reload
http://10.1.1.35/--訪問時,就會彈出一個框框,要求輸入使用者名稱和密碼才能訪問
輸入成功,第二次訪問就不需要使用者名稱和密碼,需要清除網站緩衝才可以
--一般情況下,不會去使用這種驗證功能,因為每次訪問都要去讀取.htaccess,對網站效能是有消耗,可以用上面學的標籤功能來替代
例十一:建立系統帳號的個人空間
vim /etc/httpd/conf/httpd.conf
<IfModule mod_userdir.c>--針對mod_userdir.c這個模組的配置
# UserDir disable--預設這是關閉系統使用者的個人空間功能 ,我們在這裡注釋掉
UserDir public_html--開啟這一項
</IfModule>
--再開啟下面這一整段的注釋
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
/etc/init.d/httpd reload
[root@li ~]# cd /home/a/
[root@li a]# mkdir public_html
[root@li a]# echo 'the user room for a' > /home/a/public_html/index.html
[root@li a]# cd /home/b
[root@li b]# mkdir public_html
[root@li b]# echo 'the user room for b' > /home/b/public_html/index.html
[root@li b]# ll /home/a -d--可以看到普通使用者家目錄是不允許apache使用者去讀的
drwx------ 13 a a 4096 05-22 14:30 /home/a
[root@li b]# chmod 701 /home/a
[root@li b]# chmod 701 /home/b--我們改許可權讓apache使用者可讀
http://10.1.1.35/~a/--這樣就訪問到a使用者空間裡的首頁
http://10.1.1.35/~b/--訪問到b使用者空間裡的首頁
--#################################################################################
apache的虛擬機器主機功能
指的就是一台伺服器跑多台apache
--基於IP地址的虛擬機器主機
[root@li b]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 10.1.1.36:80>--這一段就是一個虛擬機器主機的配置
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/aa--這是用http://10.1.1.36訪問時的網站根目錄
ServerName aa.cluster.com--這是訪問的網域名稱,但要有/etc/hosts和DNS的支援
ErrorLog logs/aa-error_log--錯誤記錄檔路徑
CustomLog logs/aa-access_log common--訪問日誌
</VirtualHost>
<VirtualHost 10.1.1.37:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/bb
ServerName bb.cluster.com
ErrorLog logs/bb-error_log
CustomLog logs/bb-access_log common
</VirtualHost>
/etc/init.d/httpd restart
[root@li b]# mkdir /www/aa/
[root@li b]# mkdir /www/bb/--把虛擬機器主機的兩個網站根目錄給建立出來
[root@li b]# echo 'aa'> /www/aa/index.html
[root@li b]# echo 'bb'> /www/bb/index.html--不同的網站根目錄寫上不同的首頁,以方便實驗區分
[root@li b]# ifconfig eth0:0 10.1.1.36 netmask 255.255.255.0
[root@li b]# ifconfig eth0:1 10.1.1.37 netmask 255.255.255.0--這兩個IP在伺服器上要存在,如果不存在,可以虛擬出來做實驗
http://10.1.1.36/--這樣訪問就是訪問到了/www/aa目錄下的index.html的內容
http://10.1.1.37/--顯示/www/bb目錄下的index.html的內容
http://aa.cluster.com/
http://bb.cluster.com/--要實現網域名稱訪問,必須把它們與36,37IP地址的對應寫到/etc/hosts或者是DNS記錄裡
--上面的虛擬機器主機配置段還有一種寫法
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/bb
ServerName 10.1.1.36
ErrorLog logs/bb-error_log
CustomLog logs/bb-access_log common
</VirtualHost>
--不好的地方就是佔用IP資源,但是配置方便,不需要去配置DNS,做實驗時比較快捷
-----------------------------------------------------------
--基於連接埠的虛擬機器主機
[root@li b]# vim /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080--多加一個監聽的連接埠 8080
<VirtualHost 10.1.1.35:8080>--這裡連接埠為8080
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/bb
ServerName bb.cluster.com
ErrorLog logs/bb-error_log
CustomLog logs/bb-access_log common
</VirtualHost>
/etc/init.d/httpd reload
http://10.1.1.35:8080/--這樣訪問就能訪問到/www/bb下面的網站內容
--訪問時要指定連接埠,如果不知道連接埠,就不能訪問,而且連接埠開得越多,安全性下降
------------------------------------------------------------------
--基於網域名稱的虛擬機器主機
sports.sina.com
news.sina.com
book.sina.com
car.sina.com
...............................
第一步:
配置DNS服務,加上CNAME的子網域名稱的對應記錄
IN NS li.cluster.com.
www IN A 10.1.1.35
sports IN CNAME www
news IN CNAME www--加上了這兩句CNAME的記錄
[root@li b]# /etc/init.d/named restart
[root@li b]# vim /etc/resolv.conf
nameserver 10.1.1.35--這裡改成DNS的尋找伺服器為本機
[root@li b]# nslookup www.cluster.com
[root@li b]# nslookup news.cluster.com
[root@li b]# nslookup sports.cluster.com--檢驗這三個網域名稱返回的IP結果都為10.1.1.35
第二步:
[root@li b]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80--把這一句開啟注釋,表示開啟基於網域名稱的虛擬機器主機技術
<VirtualHost *:80>
DocumentRoot /www
ServerName www.cluster.com--訪問www.cluster.com就訪問到/www目錄
ErrorLog logs/www-error_log
CustomLog logs/www-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/sports
ServerName sports.cluster.com--訪問sport.cluster.com,就訪問到/www/sports目錄下
ErrorLog logs/sports-error_log
CustomLog logs/sports-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/news
ServerName news.cluster.com--訪問news.cluster.com,就訪問到/www/news下
ErrorLog logs/news-error_log
CustomLog logs/news-access_log common
</VirtualHost>
/etc/init.d/httpd reload
第三步:
分別建立他們的網站根目錄,並建立不同的首頁,以方便實驗區分
第四步:驗證
http://www.cluster.com/
http://sports.cluster.com/
http://news.cluster.com/
--------------------------
簡單的HTML首頁寫法
<html>
<head><center><H1>this is sports man page</H1></center> </head>
<head><center><H6>basketball</H6></center> </head>
<body>
<center>
asldjsal;ga;slgasl;gjalgjal;jgaglksalk
</center>
</body>
</html>
-------------------------