默认虚拟主机
1、配置文件
# vim /usr/local/apache2.4/conf/httpd.conf
// 搜索关键词httpd-vhost,找到这行把行首的井号删除 保存 // 保存主配置文件,然后编辑虚拟机主机配置文件
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
//这个配置文件是虚拟机的主配置文件,接下来的各个操作都是在这个配置文件中完成的。
<VirtualHost *:80>
ServerAdmin guanliyuan.com //管理员邮箱 //对下面操作作用不大
DocumentRoot "/data/wwwroot/bai.com" //网站根目录
ServerName bai.com //网站的域名
ServerAlias www.bai.com //网站第二域名
ErrorLog "logs/bai.com-error.log" //错误日志
CustomLog "logs/bai.com-access_log" common //访问日志
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com" //网站根目录
ServerName www.123.com //网站的域名
</VirtualHost>
//测试
# mkdir -p /data/wwwroot/bai.com /data/wwwroot/www.123.com //定义两个站点——bai.com和123.com
# echo "bai.com" > /data/wwwroot/bai.com/index.html //将网站的域名传到index.html,默网站默认的主页就是index.html
# echo "123.com" > /data/wwwroot/www.123.com/index.html //网站默认主页为index.html
# /usr/local/apache2.4/bin/apachectl -t //检查配置
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
# curl -x127.0.0.1:80 www.bai.com //测试
aming.com
# curl -x127.0.0.1:80 www.123.com //测试
123.com
# curl -x127.0.0.1:80 www.abc.com //测试
aming.com //不管什么域名指向服务器,只要配置文件中没有标记,就会访问这个默认虚拟主机
2、用户认证
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
//123.com虚拟主机编译
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com" //网站根目录
ServerName www.123.com //网站的域名
</VirtualHost>
改为:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<Directory /data/wwwroot/www.123.com>
AllowOverride AuthConfig
AuthName "www.123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
</VirtualHost>
# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd bai //创建密码文件
New passwd:
Re-type new passwd:
Adding password for user aming:
//htpasswd命令为常见用户的工具, -c为创建,-m指定密码加密方式为MD5
// /data/.htpasswd为密码文件,aming为要创建的用户,第一次需要加-c
//否则/data/.htpasswd文件会被重置,之前的用户会被清空
# /usr/local/apache2.4/bin/apachectl -t //验证,检查配置
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
// 用管理员模式打开”记事本“或者”写字板“在最下一行增加:
# 192.168.142.143(自己的ip4地址) www.123.com
//用浏览器去访问www。123.com了弹出一个用于认证的提示框
用户名就是创建密码文件时创建的bai用户与密码
3、配置域名跳转
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把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功能
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行
</IfModule>
</VirtualHost>
# vim /usr/local/apache2.4/conf/httpd.conf //在主配置文件上修改
// 将 LoadModule rewrite_module module/mod_rewrite.so 放开这一行注释
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite //查看模块是否放开
rewrite_module(shared)
# curl -x127.0.0.1:80 -I 123.com //测试状态码返回301 成功
HTTP/1.1 301 Moved Permanently
Date: Fri, 21 May 2021 17:57:17 GMT //时间日期为当前日期
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Location: http://www.123.com/ //跳转后的网址为http://www.123.com/
Content-Type: text/html; charset=iso-8859-1
# 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
// %u为用户名,%t为时间,%r为请求的动作 %>s为请求的状态码 %b为传输数据大小 %{Referer}i为referer信息 %{User-Agent}i为浏览器标识
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主配置文件
<VirtualHost *:80
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c>
RewriteEngine on
rewriteCond %{HTTP_HOST} !^www.123.com$
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
</IfModule>
CustomLog "logs/123.com-access_log" combined //增加内容
</VirtualHost>
# /usr/local/apache2.4/bin/apachectl -t //检查配置
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
# curl -x127.0.0.1:80 -I 123.com //测试,如果为301的话就说明正确
# tail /usr/local/apache2.4/logs/123.com-access_log 显示文件中的尾部内容。
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主配置文件
// 修改www.123.com的配置文件 不记录静态文件日志
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c>
RewriteEngine on
rewriteCond %{HTTP_HOST} !^www.123.com$
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
</IfModule>
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>
// 先定义一个image-request环境变量,把gif、jpg、png、bmp、swf、js、css等格式的文件全部归类到image-request
// env=!image-request用到”!“,意思就是把image-request以外的类型文件记录到日志当中
// rotetelogs为httpd自带切割日志的工具,访问日志按我们定义的文件格式进行切割
// 86400单位为”秒“,相当于”一天“
# /usr/local/apache2.4/bin/apachectl -t //检测配置文件是否出错
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置文件
# ls /usr/local/apache2.4/logs/ // 查看目录
//静态文件不记录日志 测试
# touch /data/wwwroot/www.123.com/aming.jpg
# touch /data/wwwroot/www.123.com/aming.txt
# curl -x127.0.0.1:80 www.123.com/wsw.txt
# curl -x127.0.0.1:80 www.123.com/wsw.jpg
# cat /usr/local/apache2.4/logs/123.com-access_20210522.log
127.0.0.1 - - [22/May/2021:05:39:58 +0800] "HEAD HTTP://123.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
127.0.0.1 - - [22/May/2021:05:59:42 +0800] "GET HTTP://www.123.com/aming.txt HTTP/1.1" 200 - "-" "curl/7.29.0" // 只有txt的记录
//访问了txt文件和jpg文件,日志只记录了txt的记录。
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主文件
// 在www.123.com虚拟机上编译 配置静态元素过期时间
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c>
RewriteEngine on
rewriteCond %{HTTP_HOST} !^www.123.com$
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
</IfModule>
SetEnvIf Request_URI ".*\.gif$" img //去除根据请求信息,不用从服务器上下载,直接访问用户电脑里面的缓存
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min" //配置各种文件的过期时间
</IfModule>
</VirtualHost>
# /usr/local/apache2.4/bin/apachectl -t //检测配置服务
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
# vim /usr/local/apache2.4/conf/httpd.conf //配置主配置文件
LoadModule expires_module modules/mod_expires.so # 放开这一行注释
# /usr/local/apache2.4/bin/apachectl -M |grep -i expires //打开该模块
expires_module (shared) // 成功加载expires模块
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
// 访问jpg文件,发现max-age=86400 表示缓存一天
# curl -x127.0.0.1:80 -I www.123.com/wsw.jpg
HTTP/1.1 200 OK
Date: Sat, 22 May 2021 13:30:31 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Last-Modified: Fri, 21 May 2021 21:58:54 GMT
ETag: "0-5c2de2a749f39"
Accept-Ranges: bytes
Cache-Control: max-age=86400
Expires: Sun, 23 May 2021 13:30:31 GMT
Content-Type: image/jpeg
# curl -x127.0.0.1:80 -I www.123.com/wsw.txt
HTTP/1.1 200 OK
Date: Sat, 22 May 2021 13:31:53 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Last-Modified: Fri, 21 May 2021 21:58:49 GMT
ETag: "0-5c2de2a23f2e1"
Accept-Ranges: bytes
Cache-Control: max-age=0 // 访问txt 发现 max-age=0 表示没有缓存该类型的文件。
Expires: Sat, 22 May 2021 13:31:53 GMT
Content-Type: text/plain
7、配置防盗链
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主文件
# 防盗链
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
<Directory /data/wwwroot/www.123.com>
SetEnvIfNoCase Referer "http://www.123.com" local_ref
SetEnvIfNoCase Referer "http://123.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref //^$为空referer
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> //用filesmatch定义需要保护的文件类型,访问txt、doc、MP3、zip、rar、jpg、gif格式的文件时会被限制
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
</Directory>
</VirtualHost>
// 使用非允许的referer 报错403
# curl -x127.0.0.1:80 -I -e "http://www.1234.com/1.txt" http://www.123.com/aming.jpg //用-e来定义referer,这个referer一定要以http://开头,否则不管用 ?
HTTP/1.1 403 Forbidden
Date: Sat, 22 May 2021 14:04:32 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Content-Type: text/html; charset=iso-8859-1
// 访问html文件可以被访问,未被保护
# curl -x127.0.0.1:80 -I -e "http://www.1234.com/1.txt" http://www.123.com/index.html
HTTP/1.1 200 OK
Date: Sat, 22 May 2021 14:05:00 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Last-Modified: Fri, 21 May 2021 10:34:59 GMT
ETag: "8-5c2d49c8eb296"
Accept-Ranges: bytes
Content-Length: 8
Content-Type: text/html
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //虚拟机主配置文件
// 访问控制
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
<Directory /data/wwwroot/www.123.com/admin/> //来限制访问的目录
Order deny,allow
Deny from all //所有IP都被限制
Allow from 127.0.0.1 //用此代码只允许127.0.0.1这个IP
</Directory>
</VirtualHost>
# /usr/local/apache2.4/bin/apachectl -t //检测配置
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
// 配置限制文件
<Directory /data/wwwroot/www.123.com>
<FilesMatch "admin.php(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1 // 只允许127.0.0.1IP访问
</FilesMatch>
</Directory>
//创建目录,模拟网站后台
# mkdir /data/wwwroot/www.123.com/admin //创建admin目录,为了模拟网站后台
# echo "admin" > /data/wwwroot/www.123.com/admin/index.html //在后台下创建目录,并写入内容
# > /usr/local/apache2.4/logs/123.com-access_20210522.log //清空日志
# curl -x192.168.200.50:80 -I www.123.com/admin/index.html //测试限制目录
HTTP/1.1 403 Forbidden //验证只允许127.0.0.1这个IP访问
Date: Sat, 22 May 2021 14:34:30 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Content-Type: text/html; charset=iso-8859-1
# curl -x127.0.0.1:80 -I www.123.com/admin/index.html
HTTP/1.1 200 OK
Date: Sat, 22 May 2021 14:34:44 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Last-Modified: Sat, 22 May 2021 14:30:13 GMT
ETag: "6-5c2ec03b1ca18"
Accept-Ranges: bytes
Content-Length: 6
Content-Type: text/html
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //编写虚拟机主配置文件
// 禁止解析PHP
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
<Directory /data/wwwroot/www.123.com/upload>
php_admin_flag engine off
</Directory>
</VirtualHost>
// 验证
# /usr/local/apache2.4/bin/apachectl -t //检测配置服务
# /usr/local/apache2.4/bin/apachectl graceful // 重新加载配置服务
# mkdir /data/wwwroot/www.123.com/upload // 创建目录、模拟网站上传目录
# cp /usr/local/apache2.4/htdocs/1.php /data/wwwroot/www.123.com/upload/ //复制文件
// 检测
# curl -x127.0.0.1:80 www.123.com/upload/1.php
<?php
echo 111
?> //测试1.php能否正常启动
# vim /usr/local/apache2.4/conf/extra/httpd-vhost.conf //编辑虚拟机主配置文件
# 限制user_agent
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] // %{HTTP_USER_AGENT}为user_agent的内置变量 OR 表示“或者”, NC表示“不区分大小写”
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] // user——agent匹配curl或者baidu.com
RewriteRule .* - [F] //F相当于Forbidden
</IfModule>
</VirtualHost>
# /usr/local/apache2.4/bin/apachectl -t //检测配置服务
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
# curl -I -x127.0.0.1:80 www.123.com/upload/1.php
HTTP/1.1 403 Forbidden
Date: Sat, 22 May 2021 15:28:07 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
Content-Type: text/html; charset=iso-8859-1
//测试
# curl -A "123123" -I -x127.0.0.1:80 www.123.com/upload/1.php //curl的-A选项指定user_agent //user_agent自定义的“123123”,没有匹配任何条件,状态为200
HTTP/1.1 200 OK
Date: Sat, 22 May 2021 15:28:29 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.39
X-Powered-By: PHP/5.6.39
Content-Type: text/html; charset=UTF-8