下載
-
打開網頁http://httpd.apache.org/,點擊
在download頁面點擊
然后在新頁面選擇
在新窗口選擇
選擇32位或64位apache下載,此處下載64位的apache:
httpd-2.4.34-o110h-x64-vc14.zip
-
安裝
將httpd-2.4.34-o110h-x64-vc14.zip解壓開到某個目錄,打開readme_first.html會有完整的介紹。下圖是apache支持的操作系統
下載Visual C++ 2015 x64 Redistribution並安裝
-
啟動服務
-
將apache加入到windows服務
到D:\green\Apache24\bin目錄下執行httpd -k install,會在windows服務中看見apache的服務項
紅色部分為測試apache啟動,有錯誤是因為apache目錄在httpd.conf中指定的不對,打開httpd.conf,找到38行,將Define SRVROOT "/Apache24"修改為實際目錄
Define SRVROOT "D:/green/Apache24"
-
Apache命令
Start Apache httpd –k start
Stop Apache
httpd -k stop
Restart Apache
httpd -k restart
Uninstall Apache Service
httpd -k uninstall
Test Config Syntax
httpd -t
Version Details
httpd -V
Command Line Options List
httpd -h
-
配置
-
Listen 配置
Listen主要偵聽web服務端口狀態,默認為:80,即偵聽所有的地址的80端口,注意這里也可以寫成IP地址的偵聽形式,不寫即默認的地址:0.0.0.0
-
ServerRoot 配置
-
Apache服務管理員通知郵箱地址
此選項主要用指定Apache服務管理員通知郵箱地址,選擇默認值即可,如果有真實的郵箱地址也可以設置此值
-
Apache根目錄訪問權限設置
此選項主要是針對用戶對根目錄下所有的訪問權限控制,默認Apache對根目錄訪問都是拒絕訪問
-
Apache的默認網站根目錄設置及訪問控制
-
Apache的默認首頁設置
-
虛擬目錄配置
-
修改httpd-vhost.conf
打開Apache24\conf\extra\httpd-vhost.conf文件,添加如下內容:
<VirtualHost *:8090>
DocumentRoot "d:\wwwroot"
ServerName www.xframe.com
ServerAlias xframe.com
<Directory "d:\wwwroot">
Options Indexes FollowSymLinks MultiViews
AllowOverride none
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
-
添加端口監聽
在httpd.conf中找到Listen 80,在Listen 80下面添加Listen 8090,如果虛擬目錄不需要80以外的端口就不需要這樣做。
-
打開對虛擬機的支持
在httpd.conf中找到Include conf/extra/httpd-vhosts.conf,將前面的#號刪掉
-
修改hosts
打開C:\Windows\System32\drivers\etc中的hosts文件,添加如下內容:
127.0.0.1 xframe.com
::1 xframe.com
關閉該文件,打開cmd窗口,執行如下命令刷新dns緩存:
ipconfig /flushdns
-
重啟服務並訪問
重新啟動apache,在5.1中,我們建立的虛擬機對應的物理目錄為d:\wwwroot,虛擬主機名為xframe.com,我們在該目錄下拷貝一個index.html文件,用如下任何地址都可以進行訪問了:
-
apache的AllowOverride以及Options使用詳解
AllowOverride參數就是指明Apache服務器是否去找.htacess文件作為配置文件,如果設置為none,那么服務器將忽略.htacess文件,如果設置為All,那么所有在.htaccess文件里有的指令都將被重寫。對於AllowOverride,還可以對它指定如下一些能被重寫的指令類型.
通常利用Apache的rewrite模塊對 URL 進行重寫的時候, rewrite規則會寫在 .htaccess 文件里。但要使 apache 能夠正常的讀取.htaccess 文件的內容,就必須對.htaccess 所在目錄進行配置。從安全性考慮,根目錄的AllowOverride屬性一般都配置成不允許任何Override ,即<Directory />
AllowOverride None
</Directory>在 AllowOverride 設置為 None 時, .htaccess 文件將被完全忽略。當此指令設置為 All 時,所有具有 ".htaccess" 作用域的指令都允許出現在 .htaccess 文件中。
而對於 URL rewrite 來說,至少需要把目錄設置為< Directory /myblogroot/>
AllowOverride FileInfo
< /Directory>2.在要支持url rewirte的目錄啟用 Options FollowSymLinks和AllowOverride All
Alias /php "c:/web/php/"
<Directory "c:/web/php/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
這樣通過http://localhost:8080/php/訪問時,/php/和其下面的子目錄將支持url rewrite1 AuthConfig 允許使用所有的權限指令,他們包括AuthDBMGroupFile AuthDBMUserFile AuthGroupFile AuthName AuthTypeAuthUserFile和Require
2 FileInfo 允許使用文件控制類型的指令。它們包括AddEncoding AddLanguage AddType DEfaultType ErrorDocument LanguagePriority
3 Indexes 允許使用目錄控制類型的指令。它們包括AddDescription AddIcon AddIconByEncoding AddIconByType DefaultIcon DirectoryIndex FancyIndexing HeaderName IndexIgnore IndexOptions ReadmeName
4 Limit 允許使用權限控制指令。它們包括Allow Deny和Order
5 Options 允許使用控制目錄特征的指令.他們包括Options 和XBitHack
Options
1 All 准許以下除MultiViews以外所有功能
2 MultiViews 允許多重內容被瀏覽,如果你的目錄下有一個叫做foo.txt的文件,那么你可以通過/foo來訪問到它,這對於一個多語言內容的站點比較有用
3 Indexes 若該目錄下無index文件,則准許顯示該目錄下的文件以供選擇
4 IncludesNOEXEC 准許SSI,但不可使用#exec和#include功能
5 Includes 准許SSI
6 FollowSymLinks 在該目錄中,服務器將跟蹤符號鏈接。注意,即使服務器跟蹤符號鏈接,它也不會改變用來匹配不同區域的路徑名,如果在<Local>;標記內設置,該選項會被忽略
7 SymLinksIfOwnerMatch 在該目錄中僅僅跟蹤本站點內的鏈接
8 ExecCGI 在該目錄下准許使用CGI
-
使用require指令進行訪問控制
從Apache2.2升級到Apache2.4后,發現原來用來限制部分IP和垃圾網絡爬蟲的訪問控制規則不起作用,查詢后才發現,Apache2.4中開始使用mod_authz_host這個新的模塊來進行訪問控制和其他的授權檢查。原來在Apache2.2版本下用以實現網站訪問控制的Order,Allow,Deny指令需要替換為新的Require訪問控制指令。
注意:使用require指令時,需要在指令外添加<RequireAll></RequireAll>標簽對,否則重啟Apache2.4加載規則時將出現錯誤:" negative Require directive has no effect in <RequireAny> directive "。
下面直接給出一些實例對Require指令的使用進行說明:
-
允許所有訪問請求
Apache2.4下的配置:
<Directory xxx/www/yoursite>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
-
拒絕所有訪問請求
<Directory xxx/www/yoursite>
<RequireAll>
Require all denied
</RequireAll>
</Directory>
-
只允許來自特定域名主機的訪問請求,其他請求將被拒絕
<Directory xxx/www/yoursite>
<RequireAll>
Require host google.com
</RequireAll>
</Directory>
-
只允許來自特定IP或IP段的訪問請求,其他請求將被拒絕
<Directory xxx/www/yoursite>
<RequireAll>
Require ip 192.120 192.168.100 192.168.1.1
</RequireAll>
</Directory>
-
允許所有訪問請求,但拒絕來自特定IP或IP段的訪問請求(阻止惡意IP或惡意爬蟲網段的訪問)
<Directory xxx/www/yoursite>
<RequireAll>
Require all granted
Require not ip 192.168.1.1
Require not ip 192.120 192.168.100
</RequireAll>
</Directory>
-
允許所有訪問請求,但拒絕某些User-Agent的訪問請求(通過User-Agent屏蔽垃圾網絡爬蟲)
使用mod_setenvif通過正則表達式匹配來訪請求的User-Agent,並設置內部環境變量BADBOT,最后拒絕BADBOT的訪問請求。
<Directory xxx/www/yoursite>
SetEnvIfNoCase User-Agent ".*(FeedDemon|JikeSpider|AskTbFXTV|CrawlDaddy|Feedly|Swiftbot|ZmEu|oBot).*" BADBOT
SetEnvIfNoCase User-Agent "brandwatch" BADBOT
SetEnvIfNoCase User-Agent "rogerbot" BADBOT
<RequireAll>
Require all granted
Require not env BADBOT
Require not ip 192.168.100.1
</RequireAll>
</Directory>
-
其它require訪問控制指令用法
Require all granted #允許所有
Require all denied #拒絕所有
Require env env-var [env-var] ... #允許匹配環境變量中任意一個
Require method http-method [http-method] ... #允許特定的HTTP方法(GET/POST/HEAD/OPTIONS)
Require expr expression #允許,表達式為true
Require user userid [ userid ] ... #允許特定用戶
Require group group-name [group-name] ... #允許特定用戶組
Require valid-user # #允許,有效用戶
Require ip 192.100 192.168.100 192.168.100.5 #允許特定IP或IP段,多個IP或IP段間使用空格分隔
-
apache禁止列目錄
- 兩步:
- 1、修改Apache配置文件httpd.conf,把Options Indexes FollowSymLinks改成Options FollowSymLinks
- 2、如果配置了虛擬目錄,則打開httpd-vhosts.conf,將你的虛擬目錄下的Options Indexes FollowSymLinks MultiViews 修改為Options FollowSymLinks MultiViews
-