1.使用htpasswd建立用户文件:

$htpasswd -mc /etc/httpd/conf/.htpasswd admin


2在网站根目录下  

$touch .htaccess

$vim .htaccess

AuthName "pls use passwd"

AuthType Basic

AuthUserFile /etc/httpd/conf/.htpasswd

require valid-user


3修改apache配置文件,添加authconfig

vim /etc/httpd/conf/httpd.conf


<Directory "/opt/vhost/xxx.com/www">
   Options Indexes FollowSymLinks
   AllowOverride AuthConfig
   Order allow,deny
   Allow from all
</Directory>


4 /etc/init.d/httpd restart