一、安裝httpd
二、相關文件路徑
三、配置文件詳解
四、轉載比較詳細的HTTP配置中英文對照
一、安裝httpd
1、安裝方式:yum源、rpm包、源碼包編譯安裝,這裏爲了方便使用yum源安裝
[root@test3 html]# yum install -y httpd
可以安裝本地幫助手冊
[root@test3 conf]# yum install -y httpd-manual
訪問地址http://httpd主機ip/manual/
2、開啓服務
[root@test3 vsftpd]# chkconfig --list httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@test3 vsftpd]# chkconfig httpd on [root@test3 vsftpd]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
3、測試
(1)、可以暫時關閉iptables,在瀏覽器地址欄中輸入服務器主機ip即可成功訪問到httpd歡迎頁面,歡迎頁面配置文件/etc/httpd/conf.d/welcome.conf
(2)、可以在/var/www/html/下創建默認主頁面文件index.html,那麼以主機ip訪問時,將不顯示歡迎信息,而是index文件內容
[root@test3 html]# vim /var/www/html/index.html 編輯內容如下 <html> <title>Test Page</title> <body> <h1>Hello world.</h1> Test information. </body> </html>
HTTP的響應碼:
1xx: 信息性狀態碼
2xx: 成功狀態碼
3xx: 重定向狀態碼
301: 永久重定向, Location響應首部的值仍爲當前URL,因此爲隱藏重定向;
302: 臨時重定向,顯式重定向, Location響應首部的值爲新的URL
304:Not Modified
4xx: 客戶端錯誤狀態碼
404: Not Found
5xx: 服務器端錯誤狀態碼
500: Internal Server Error
502: Bad Gateway
504:Gateway Timeout
二、相關文件路徑
服務腳本:/etc/rc.d/init.d/httpd
運行目錄:/etc/httpd(安裝目錄)
主配置文件:/etc/httpd/conf/httpd.conf
擴展配置:/etc/httpd/conf.d/*.conf
監聽socket: http,80/tcp https,443/tcp
網頁文件目錄(DocumentRoot):/var/www/html/
CGI格式的腳本存放位置:/var/www/cgi-bin/
查看httpd核心組建
[root@test3 conf]# httpd -l
獲取httpd命令簡單幫助
[root@test3 conf]# httpd -h
查看所有裝載的模塊
[root@test3 conf]# httpd -t -D DUMP_MODULES
三、配置文件詳解
配置文件由3部分組成,主服務器和虛擬主機不能同時啓用
[root@test3 html]# grep "Section" /etc/httpd/conf/httpd.conf ### Section 1: Global Environment #全局環境,對主服務器和虛擬主機都生效,有些配置是服務器自身工作屬性 ### Section 2: 'Main' server configuration #主服務器配置,主站配置 ### Section 3: Virtual Hosts #虛擬主機配置
修改配置文件後,使用配置文件語法測試,大多數配置service httpd reload即可生效,但是改了端口設置則需要restart
[root@test3 conf]# httpd -t Syntax OK #表示沒有問題 或者 [root@test3 conf]# service httpd configtest Syntax OK
1、配置監聽的地址和端口
Listen 80
Listen 10.1.1.112:80
2、配置所選用的MPM的屬性,MPM:多道處理模塊,定義apache響應多個用戶同時請求的工作模型
(1)、prefork:一個進程響應一個請求(默認使用)
(2)、worker:一個進程生成多個線程,沒有線程響應一個請求
啓用該模塊時,需要編輯/etc/sysconfig/httpd,去掉#號
#HTTPD=/usr/sbin/httpd.worker
# prefork MPM
<IfModule prefork.c>
StartServers 8 #啓動web服務時,啓動8個空閒進程
MinSpareServers 5 #最小空閒進程數
MaxSpareServers 20 #最大空閒進程數
ServerLimit 256 #允許最大併發連接請求數
MaxClients 256 #允許同時連接的客戶端數
MaxRequestsPerChild 4000 #單個子進程在其生命週期內處理的總請求數限制,當某個子進程處理過的總請求數到達這個限制後這個進程就會被回收,如果設爲0,那麼這個進程永遠不會過期(這樣如果有內存泄露的話就會一直泄露下去……)
</IfModule>
# worker MPM
<IfModule worker.c>
StartServers 4 #啓動web服務時,啓動4個空閒線程
MaxClients 300 #允許同時連接的客戶端數
MinSpareThreads 25 #最少空閒線程數
MaxSpareThreads 75 #最大空閒線程數
ThreadsPerChild 25 #每個進程允許生成的線程數量
MaxRequestsPerChild 0 #
</IfModule>
3、配置服務器開啓持久連接keep-alived,空閒服務器可以開啓,繁忙的服務器關閉該功能比較好
KeepAlive Off #關閉 KeepAlive On #打開
KeepAliveTimeout 15 #超時時長,單位秒,一個連接超過15秒就斷開
MaxKeepAliveRequests 100 #一次持久連接中,最多請求100個資源就斷開
4、配置加載的模塊信息,不需要加載就用#號註釋掉
# Example:
LoadModule 模塊名稱 相對路徑,相對於這個路徑/etc/httpd
# LoadModule foo_module modules/mod_foo.so
5、定義站點名稱
例如:
ServerName localhost:80
ServerName 127.0.0.1:80
ServerName www.example.com:80
6、配置站點根目錄
DocumentRoot "/var/www/html"
<Directory "FS_PATH">
</Directory>
<Location "URL">
</Location>
7、配置頁面文件訪問屬性
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None #允許優先,用來控制指令
Order allow,deny
Allow from all
</Directory>
Indexes 是否允許索引頁面,生產環境中不是下載站點,建議關閉
FollowSymLinks 跟隨軟鏈接文件
SymLinksifOwnerMatch 跟隨軟鏈接文件,但屬主需匹配,意味着apache用戶是文件的屬主
ExecCGI 是否允許運行CGI腳本
All 全部開啓
None 全部關閉
8、訪問控制
(1)、基於客戶端訪問控制
Order allow,deny
Allow from all #默認deny,但是下面用allow定義了允許所有,所以整個控制意爲放行
----------------------------
Order allow,deny
Allow from 10.1.1. 0/8 #默認deny,但是下面用allow定義允許訪問的地址段,所以整個控制對定義的地址段放行
---------------------------
Order allow,deny
Deny from 10.1.1. 0/8 #默認deny,但是下面用deny定義拒絕訪問的地址段,所以整個控制就是拒絕所有訪問
---------------------------
Order:定義allow和deny哪個爲默認法則,寫在後面的爲默認法則,寫在前面的指令沒有顯示定義的即受後面的控制
(2)、基於用戶訪問控制
DocumentRoot "/var/www/html"
<Directory "/PATH/TO/DocumentRoot_SUBDIR"> 指定訪問路徑,通常爲目錄爲站點根目錄的子目錄
Options None 選項
AllowOverride AuthConfig 是否允許覆蓋、啓用用戶訪問控制
AuthName "Realm" 認證名稱,“”內是說明信息
AuthType Basic 認證類型
AuthUserFile /path/to/passwords 用戶帳號文件
Require jerry tom 指明可以登錄的用戶,所有用戶使用valid-user
</Directory>
這裏我們在/etc/httpd/conf/下創建一個隱藏的帳號文件:
htpasswd -c –m /etc/httpd/conf/.htpass jerry
添加另一個用戶
htpasswd –m /etc/httpd/conf/.htpass tom
創建用戶帳號文件,使用命令htpasswd,可用htpasswd -h獲取幫助信息
[root@test3 httpd]# htpasswd -h Usage: htpasswd [-cmdpsD] passwordfile username htpasswd -b[cmdpsD] passwordfile username password htpasswd -n[mdps] username htpasswd -nb[mdps] username password -c Create a new file. #第一次創建時,使用該選項,再次像文件寫入時不要使用,會被覆蓋 -n Don't update file; display results on stdout. -m Force MD5 encryption of the password. #用戶密碼是使用md5加密的 -d Force CRYPT encryption of the password (default). -p Do not encrypt the password (plaintext). #不加密,明文存放 -s Force SHA encryption of the password. #用戶密碼是使用SHA加密的 -b Use the password from the command line rather than prompting for it. -D Delete the specified user. On Windows, NetWare and TPF systems the '-m' flag is used by default. On all other systems, the '-p' flag will probably not work.
(3)、基於組的訪問控制
<Directory "/PATH/TO/DocumentRoot_SUBDIR"> 指定訪問路徑,通常爲目錄爲站點根目錄的子目錄
Options None 選項
AllowOverride AuthConfig
AuthName "Realm" 認證名稱,“”內是說明信息
AuthType Basic 認證類型
AuthGroupFile /path/to/groupfile 用戶帳號文件
Require GRP_NAME 指明可以登錄的用戶,所有用戶使用valid-user
</Directory>
其中groupfile是一個文本文件,文件內容是用戶名
9、userdir,讓用戶有個自己的站點
訪問方式:http://HOST/~username/
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
UserDir disabled
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
#UserDir public_html 啓用該項,在用戶家目錄下新建目錄public_html,並且apache用戶要有訪問權限,setfacl –m u:apache:x /home/username/public_html
</IfModule>
10、定義默認主頁面
DirectoryIndex index.html index.jsp index.php index.html.var
有多個文件名時,會從左至右找到目錄中符合條件的主頁面文件
11、文件訪問控制
<Files ~ "^\.ht"> 該符號~表示模式匹配
Order allow,deny
Deny from all
Satisfy All
</Files>
12、配置日誌功能,apache的日誌是自己管理的
日誌有兩類:錯誤日誌、訪問日誌
[root@test3 conf]# ls /etc/httpd/logs/ access_log error_log [root@test3 httpd]# cd /etc/httpd/ [root@test3 httpd]# ls -l total 8 drwxr-xr-x. 2 root root 4096 May 17 00:28 conf drwxr-xr-x. 2 root root 4096 May 16 23:52 conf.d lrwxrwxrwx. 1 root root 19 May 16 17:18 logs -> ../../var/log/httpd #日誌目錄實際位置在/var目錄下
錯誤日誌:ErrorLog logs/error_log 無需指定日誌格式
訪問日誌:CustomLog logs/access_log common 需指定日誌格式
日誌格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
%h:客戶端主機ip
%l:客戶端登錄的主機地址,通常爲匿名訪問,日誌記錄爲-,表示空
%u:認證提供的用戶名,通常爲匿名訪問,日誌記錄爲-,表示空
%t:請求到達時間
%r:請求報文的起始行,內容有請求連接的方法get、請求的資源、使用的協議版本
%>s:%s顯示內部完成重定向以後的狀態碼,%>s爲用戶請求到達的原始響應的狀態碼
%b:響應報文的大小,單位bytes,不包含HTTP首部
%{Referer}i:定義由哪個超鏈接跳轉
%{User-Agent}i:所使用的瀏覽器
13、設定默認字符集
AddDefaultCharset UTF-8
14、路徑別名
Alias /error/ "/var/www/error/"
兩個文件夾都在站點默認根目錄下,使用別名後,可以定義訪問路徑爲http://www.test.com/error/時,顯示的是/var/www/error/下的內容
15、腳本路徑別名:定義CGI的執行目錄
<Directory "/var/www/html">
Options Indexes FollowSymLinks ExecCGI #啓用CGI模塊,添加ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
注意:爲了安全我們可以不在上面啓用,而是使用別名
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 頁面訪問路徑爲http://www.test.com/cgi-bin/test.sh時,實際訪問的是/var/www/cgi-bin/中的CGI腳本
測試文件test.sh
#!/bin/bash # cat << EOF Content-Type: text/html <pre> The hostname is: `/bin/hostname`. The time is: `date`. EOF # end of cgi script file
16、虛擬主機
使用虛擬主機時,不能使用主服務器,關閉主服務器:註釋主服務器的DocumentRoot
(1)、類型:
基於端口的虛擬主機:監聽不同的端口,使用相同的ip
基於IP的虛擬主機:監聽相同端口,使用不同ip地址
基於主機名的虛擬主機:監聽相同端口,使用相同ip地址,使用不同的主機名,開啓NameVirtualHost *:80
(2)、每個虛擬主機的定義
#<VirtualHost *:80> *指所有地址的80端口上,也可以是ip
# ServerAdmin [email protected]
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
(3)、前文中適用於主服務器配置的配置信息也可以用於配置虛擬主機
17、httpd status
httpd狀態信息輸出模塊
LoadModule status_module modules/mod_status.so
ExtendedStatus On 是否開啓該功能
#<Location /-status> 定義訪問status信息時,是通過哪個url,例如http://10.1.1.112/status
# SetHandler server-status 處理器名稱server-status
# Order deny,allow
# Deny from all
# Allow from .example.com
#</Location>
下面配置信息中,未啓用Order來控制主機訪問,所以不需要使用AllowOverride
<Location /status>
SetHandler server-status
AuthName "Status"
AuthType Basic
AuthUserFile /etc/httpd/conf/.statuspass
Require valid-user
</Location>
18、https的實現
SSL/TLS會話的建立僅能基於IP地址進行,只有1個IP時,只能給一個虛擬主機提供SSL;訪問端口443/tcp
前提:已安裝openssl(系統默認已安裝),安裝httpd模塊mod_ssl
[root@test3 httpd]# yum install -y mod_ssl
步驟
(1)、生成私鑰
(2)、生成證書申請
(3)、使用自建CA簽署證書
以上操作參考http://64314491.blog.51cto.com/2784219/1651785
(4)、配置文件/etc/httpd/conf.d/ssl.conf
常用配置項
<VirtualHost _default_:443> _default_表示默認虛擬主機,可以直接指定主機ip
#DocumentRoot "/var/www/html" 虛擬主機工作目錄
#ServerName www.example.com:443 虛擬主機主機名和訪問端口
SSLEngine on 是否開啓SSL安全認證功能
SSLProtocol all -SSLv2 支持除SSLv2外所有的SSL協議
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW SSL支持的加密套件,!表示不支持
SSLCertificateFile /etc/pki/tls/certs/localhost.crt 證書文件路徑
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key 私鑰文件路徑
以上配置好後,重啓服務,windows上安裝簽署的證書然後用瀏覽器打開站點https://www.test.com或者使用命令測試
測試命令,指定連接的主機及端口、CA證書
#openssl s_client -connect www.test.com:443 -CAfile /etc/pki/CA/cacert.pem
#GET / HTTP/1.1 使用GET連接方式,指定url“/”,指定使用的訪問協議“HTTP/1.1”
#Host:www.test.com 指定主機名後,敲兩次回車,顯示頁面信息及OK提示,表示測試成功
19、其他
TypesConfig /etc/mime.types 默認獲取文件時,文件資源的類型
<IfModule mod_mime_magic.c> 模塊允許服務器充文件內容來確定文件類型
# MIMEMagicFile /usr/share/magic.mime
MIMEMagicFile conf/magic 配置文件位置/etc/httpd/conf/magic
</IfModule>
#AddType 新增支持的文件類型,以某個後綴識別爲什麼格式的文件
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
四、轉載比較詳細的HTTP配置中英文對照