11.18 Apache用戶認證php
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.confapache
<VirtualHost *:80> vim
DocumentRoot "/data/wwwroot/111.com"; windows
ServerName111.com瀏覽器
ServerAlias www.example.com服務器
<Directory /data/wwwroot/111.com> //指定認證的目錄 curl
AllowOverride AuthConfig //這個至關於打開認證的開關 ide
AuthName "111.com user auth" //自定義認證的名字,做用不大 工具
AuthType Basic //認證的類型,通常爲Basic 測試
AuthUserFile /data/.htpasswd //指定密碼文件所在位置
require valid-user //指定須要認證的用戶爲所有可用用戶
</Directory>
</VirtualHost>
/usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd -c nan
(注:htpasswd命令是Apache的Web服務器內置工具,用於建立和更新儲存用戶名、域和用戶基本認證的密碼文件。)
cat /data/.htpasswd
從新加載配置-t , graceful
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
windows綁定hosts,瀏覽器測試
curl -x127.0.0.1:80 111.com //狀態碼爲401
curl -x192.168.174.128:80 111.com
curl -x127.0.0.1:80 -unan:123123 111.com //狀態碼爲200
還能夠針對單個文件進行認證
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
<FilesMatch 123.php>
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
</VirtualHost>
把<Directory /data/wwwroot/111.com>註釋掉 加上<FilesMatch 123.php>
從新加載配置-t , graceful
vim /data/wwwroot/111.com/123.php
<?php
echo "123.php";
?>
curl -x192.168.174.128:80 111.com
curl -x192.168.174.128:80 111.com/123.php
11.19/11.20 域名跳轉
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
註釋掉<Directory>模塊
添加如下內容
<VirtualHost *:80>
DocumentRoot 「/data/wwwroot/111.com」
ServerName 111.com
ServerAlias www.example.com2111.com.cn
<IfModule mod_rewrite.c> //須要mod_rewrite模塊支持
RewriteEngine on //打開rewrite功能
RewriteCond %{HTTP_HOST} !^111.com$
#定義rewrite的條件,主機名(域名)不是www.123.com知足條件
RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
#定義rewrite規則,當知足上面的條件時,這條規則纔會執行
</IfModule>
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -M|grep -i rewrite
//若無該模塊,須要編輯配置文件httpd.conf,刪除rewrite_module (shared) 前面的#
vi /usr/local/apache2.4/conf/httpd.conf 搜rewrite 刪除前面的#
改完配置文件,先檢測-t ,再加載graceful
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
curl -x127.0.0.1:80 -I 111.com //狀態碼爲301 (301永久重定向,302臨時)
curl -x127.0.0.1:80 111.com
curl -x127.0.0.1:80 http://111.com/123.php -I
curl -x127.0.0.1:80 www.example.com
C:\Windows\System32\drivers\etc hosts 記得配置
vim /usr/local/apache2.4/conf/httpd.conf
curl -x192.168.174.128:80 111.com/123.php -I
11.21 Apache訪問日誌
訪問日誌記錄用戶的每個請求
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
把虛擬主機配置文件改爲以下:把common改爲combined
combined 能夠顯示更多的信息
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" combined
</VirtualHost>
從新加載配置文件 -t,graceful
curl -x127.0.0.1:80 -I 111.com/123.php
tail /usr/local/apache2.4/logs/111.com-access_log
擴展
apache虛擬主機開啓php的短標籤 http://ask.apelearn.com/question/5370