Apache Server下,能夠對單個目錄進行訪問控制,如ip過濾,要求用戶名密碼進行Basic Auth等。下面使用 .htaccess來實現Auth.apache
首先在配置文件 http.conf裏,對所要控制的目錄,開啓Override功能,若是在這裏設置爲AllowOverride none, .htaccess文件將不起做用。瀏覽器
<Directory "/your/site/dir/need/to/control/"> Options FollowSymLinks Multiviews MultiviewsMatch any AllowOverride all Require all granted </Directory>
上面的AllowOverride能夠選 FileInfo, AuthConfig, Limit, all, none.iview
配置文件保存後,重啓server. 我在mac os x Yosemite下執行ide
sudo apachectl restart
Apache的配置文件到此完成,如今在對應的site目錄下建立.htaccess文件,如上面的/your/site/dir/need/to/control/.htaccess,內容以下ui
AuthName 'name' AuthUserFile /your/site/dir/need/to/control/.htpasswd AuthType Basic require valid-user
上面的name爲用戶login時的用戶名,AuthUserFile爲存放password的文件。下面講如何生成.htpasswd文件:spa
在console(mac os x Yosemite)下使用htpasswd命令生成.htpasswd文件rest
htpasswd -bc /your/site/dir/need/to/control/.htpasswd name password
如今在瀏覽器訪問 http://host/your/.../control/,會彈出對話框,輸入name, password便可。code