把123.com那个虚拟主机编辑成如下内容 :
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com
<Directory /data/wwwroot/111.com>
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
使用自带命令来生成密码文件:
/usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd amo
重新加载配置
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
绑定hosts,浏览器测试
状态码为401:
curl -x127.0.0.1:80 www.123.com
状态码为200:
还可以针对单个文件进行认证:
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<FilesMatch 123.php> (123=admin)
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
</VirtualHost>
二、域名跳转
需求,把123.com域名跳转到www.123.com,配置如下:
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<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>
保存退出后要检测和重新加载一下:
若无该模块,需要编辑配置文件httpd.conf,删除rewrite_module (shared) 前面的# :
vim /usr/local/apache2.4/conf/httpd.conf
再查看一下:
/usr/local/apache2.4/bin/apachectl -M |grep rewrite
状态码为301
三、Apache访问日志
日志保存路径(上一次我们访问的111.com网站的日志记录):
ls /usr/local/apache2.4/logs/111.com-access_log
日志格式的配置文件:
搜索LogFormat
vim /usr/local/apache2.4/conf/httpd.conf
把虚拟主机配置文件改成如下:
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
CustomLog "logs/111.com-access_log" common 改成 CustomLog "logs/111.com-access_log" combined
重新加载配置
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
curl -x127.0.0.1:80 -I 111.com
重新访问一下网站,然后再查看一下日志:
查看日志:
cat /usr/local/apache2.4/logs/111.com-access_log
apache虚拟主机开启php的短标签:
http://ask.apelearn.com/question/5370