11月15日任務
11.18 Apache用戶認證
11.19/11.20 域名跳轉
11.21 Apache訪問日誌php
1.Apache用戶認證apache
![](http://static.javashuo.com/static/loading.gif)
- vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那個虛擬主機編輯成以下內容
- <VirtualHost *:80>
- DocumentRoot "/data/wwwroot/www.123.com"
- ServerName www.123.com
- <Directory /data/wwwroot/www.123.com> //指定認證的目錄
- AllowOverride AuthConfig //這個至關於打開認證的開關
- AuthName "123.com user auth" //自定義認證的名字,做用不大
- AuthType Basic //認證的類型,通常爲Basic,其餘類型阿銘沒用過
- AuthUserFile /data/.htpasswd //指定密碼文件所在位置
- require valid-user //指定須要認證的用戶爲所有可用用戶
- </Directory>
- </VirtualHost>
- /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming 用Apache自帶的密碼工具生成一個密碼
- 從新加載配置-t , graceful
- 綁定hosts,瀏覽器測試
- curl -x127.0.0.1:80 www.123.com //狀態碼爲401
- curl -x127.0.0.1:80 -uaming:passwd www.123.com //狀態碼爲200 指定用戶名和密碼
實驗:一vim
- 編輯vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那個虛擬主機編輯成以下內容
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
實驗:二瀏覽器
![](http://static.javashuo.com/static/loading.gif)
- 還能夠針對單個文件進行認證
- <VirtualHost *:80>
- DocumentRoot "/data/wwwroot/www.123.com"
- ServerName www.123.com
- <FilesMatch admin.php>
- AllowOverride AuthConfig
- AuthName "123.com user auth"
- AuthType Basic
- AuthUserFile /data/.htpasswd
- require valid-user
- </FilesMatch>
- </VirtualHost>
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2. 域名跳轉curl
![](http://static.javashuo.com/static/loading.gif)
- 需求,把123.com域名跳轉到www.123.com,配置以下:
- <VirtualHost *:80>
- DocumentRoot "/data/wwwroot/www.123.com"
- ServerName www.123.com
- ServerAlias 123.com
- <IfModule mod_rewrite.c> //須要mod_rewrite模塊支持
- RewriteEngine on //打開rewrite功能
- RewriteCond %{HTTP_HOST} !^www.123.com$ //定義rewrite的條件,主機名(域名)不是www.123.com知足條件
- RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定義rewrite規則,當知足上面的條件時,這條規則纔會執行 </IfModule>
- </VirtualHost>
- /usr/local/apache2/bin/apachectl -M|grep -i rewrite //若無該模塊,須要編輯配置文件httpd.conf,刪除rewrite_module (shared) 前面的#
- curl -x127.0.0.1:80 -I 123.com //狀態碼爲301
實驗:一ide
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 檢查這個模塊是否加載,沒有加載的話,就編輯這個文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
3.Apache訪問日誌工具
![](http://static.javashuo.com/static/loading.gif)
示例一:測試
- 訪問日誌記錄用戶的每個請求
- vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
- LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 會記錄referer和user-agent。
- LogFormat "%h %l %u %t \"%r\" %>s %b" common 簡單記錄日誌
- 把虛擬主機配置文件改爲以下:
- <VirtualHost *:80>
- DocumentRoot "/data/wwwroot/www.123.com"
- ServerName www.123.com
- ServerAlias 123.com
- CustomLog "logs/123.com-access_log" combined 定義爲記錄referer和user-agent格式的日誌
- </VirtualHost>
- 從新加載配置文件 -t,graceful
- curl -x127.0.0.1:80 -I 123.com
- tail /usr/local/apache2.4/logs/123.com-access_log
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)