默认虚拟主机

1、配置文件

image.png

  1. # vim /usr/local/apache2.4/conf/httpd.conf
  2. // 搜索关键词httpd-vhost,找到这行把行首的井号删除 保存 // 保存主配置文件,然后编辑虚拟机主机配置文件
  3. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  4. //这个配置文件是虚拟机的主配置文件,接下来的各个操作都是在这个配置文件中完成的。
  5. <VirtualHost *:80>
  6. ServerAdmin guanliyuan.com //管理员邮箱 //对下面操作作用不大
  7. DocumentRoot "/data/wwwroot/bai.com" //网站根目录
  8. ServerName bai.com //网站的域名
  9. ServerAlias www.bai.com //网站第二域名
  10. ErrorLog "logs/bai.com-error.log" //错误日志
  11. CustomLog "logs/bai.com-access_log" common //访问日志
  12. </VirtualHost>
  13. <VirtualHost *:80>
  14. DocumentRoot "/data/wwwroot/www.123.com" //网站根目录
  15. ServerName www.123.com //网站的域名
  16. </VirtualHost>
  17. //测试
  18. # mkdir -p /data/wwwroot/bai.com /data/wwwroot/www.123.com //定义两个站点——bai.com和123.com
  19. # echo "bai.com" > /data/wwwroot/bai.com/index.html //将网站的域名传到index.html,默网站默认的主页就是index.html
  20. # echo "123.com" > /data/wwwroot/www.123.com/index.html //网站默认主页为index.html
  21. # /usr/local/apache2.4/bin/apachectl -t //检查配置
  22. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
  23. # curl -x127.0.0.1:80 www.bai.com //测试
  24. aming.com
  25. # curl -x127.0.0.1:80 www.123.com //测试
  26. 123.com
  27. # curl -x127.0.0.1:80 www.abc.com //测试
  28. aming.com //不管什么域名指向服务器,只要配置文件中没有标记,就会访问这个默认虚拟主机

2、用户认证

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  2. //123.com虚拟主机编译
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com" //网站根目录
  5. ServerName www.123.com //网站的域名
  6. </VirtualHost>
  7. 改为:
  8. <VirtualHost *:80>
  9. DocumentRoot "/data/wwwroot/www.123.com"
  10. ServerName www.123.com
  11. <Directory /data/wwwroot/www.123.com>
  12. AllowOverride AuthConfig
  13. AuthName "www.123.com user auth"
  14. AuthType Basic
  15. AuthUserFile /data/.htpasswd
  16. require valid-user
  17. </Directory>
  18. </VirtualHost>
  19. # /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd bai //创建密码文件
  20. New passwd:
  21. Re-type new passwd:
  22. Adding password for user aming:
  23. //htpasswd命令为常见用户的工具, -c为创建,-m指定密码加密方式为MD5
  24. // /data/.htpasswd为密码文件,aming为要创建的用户,第一次需要加-c
  25. //否则/data/.htpasswd文件会被重置,之前的用户会被清空
  26. # /usr/local/apache2.4/bin/apachectl -t //验证,检查配置
  27. Syntax OK
  28. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
  29. // 用管理员模式打开”记事本“或者”写字板“在最下一行增加:
  30. # 192.168.142.143(自己的ip4地址) www.123.com
  31. //用浏览器去访问www。123.com了弹出一个用于认证的提示框
  32. 用户名就是创建密码文件时创建的bai用户与密码

image.png

3、配置域名跳转

image.png
image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com域名跳转到 www.123.com
  2. 在原来文件上再进行修改
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. <IfModule mod_rewrite.c> //需要mod_rewrite模块支持
  8. RewriteEngine on //打开rewrite功能
  9. rewriteCond %{HTTP_HOST} !^www.123.com$ //定义rewrite功能
  10. RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行
  11. </IfModule>
  12. </VirtualHost>
  13. # vim /usr/local/apache2.4/conf/httpd.conf //在主配置文件上修改
  14. // 将 LoadModule rewrite_module module/mod_rewrite.so 放开这一行注释
  15. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
  16. # /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite //查看模块是否放开
  17. rewrite_module(shared)
  18. # curl -x127.0.0.1:80 -I 123.com //测试状态码返回301 成功
  19. HTTP/1.1 301 Moved Permanently
  20. Date: Fri, 21 May 2021 17:57:17 GMT //时间日期为当前日期
  21. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  22. Location: http://www.123.com/ //跳转后的网址为http://www.123.com/
  23. Content-Type: text/html; charset=iso-8859-1

4、配置访问日志


image.png

  1. # vim /usr/local/apache2.4/conf/httpd.conf //配置主文件,搜索LogFormat
  2. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  3. LogFormat "%h %l %u %t \"%r\" %>s %b" common
  4. // %u为用户名,%t为时间,%r为请求的动作 %>s为请求的状态码 %b为传输数据大小 %{Referer}i为referer信息 %{User-Agent}i为浏览器标识
  5. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主配置文件
  6. <VirtualHost *:80
  7. DocumentRoot "/data/wwwroot/www.123.com"
  8. ServerName www.123.com
  9. ServerAlias 123.com
  10. <IfModule mod_rewrite.c>
  11. RewriteEngine on
  12. rewriteCond %{HTTP_HOST} !^www.123.com$
  13. RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
  14. </IfModule>
  15. CustomLog "logs/123.com-access_log" combined //增加内容
  16. </VirtualHost>
  17. # /usr/local/apache2.4/bin/apachectl -t //检查配置
  18. Syntax OK
  19. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置
  20. # curl -x127.0.0.1:80 -I 123.com //测试,如果为301的话就说明正确
  21. # tail /usr/local/apache2.4/logs/123.com-access_log 显示文件中的尾部内容。

5、访问日志不记录静态文件

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主配置文件
  2. // 修改www.123.com的配置文件 不记录静态文件日志
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. <IfModule mod_rewrite.c>
  8. RewriteEngine on
  9. rewriteCond %{HTTP_HOST} !^www.123.com$
  10. RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
  11. </IfModule>
  12. SetEnvIf Request_URI ".*\.gif$" img
  13. SetEnvIf Request_URI ".*\.jpg$" img
  14. SetEnvIf Request_URI ".*\.png$" img
  15. SetEnvIf Request_URI ".*\.bmp$" img
  16. SetEnvIf Request_URI ".*\.swf$" img
  17. SetEnvIf Request_URI ".*\.js$" img
  18. SetEnvIf Request_URI ".*\.css$" img
  19. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
  20. </VirtualHost>
  21. // 先定义一个image-request环境变量,把gif、jpg、png、bmp、swf、js、css等格式的文件全部归类到image-request
  22. // env=!image-request用到”!“,意思就是把image-request以外的类型文件记录到日志当中
  23. // rotetelogs为httpd自带切割日志的工具,访问日志按我们定义的文件格式进行切割
  24. // 86400单位为”秒“,相当于”一天“
  25. # /usr/local/apache2.4/bin/apachectl -t //检测配置文件是否出错
  26. Syntax OK
  27. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置文件
  28. # ls /usr/local/apache2.4/logs/ // 查看目录
  29. //静态文件不记录日志 测试
  30. # touch /data/wwwroot/www.123.com/aming.jpg
  31. # touch /data/wwwroot/www.123.com/aming.txt
  32. # curl -x127.0.0.1:80 www.123.com/wsw.txt
  33. # curl -x127.0.0.1:80 www.123.com/wsw.jpg
  34. # cat /usr/local/apache2.4/logs/123.com-access_20210522.log
  35. 127.0.0.1 - - [22/May/2021:05:39:58 +0800] "HEAD HTTP://123.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
  36. 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的记录
  37. //访问了txt文件和jpg文件,日志只记录了txt的记录。

6、配置静态元素过期时间

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主文件
  2. // 在www.123.com虚拟机上编译 配置静态元素过期时间
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. <IfModule mod_rewrite.c>
  8. RewriteEngine on
  9. rewriteCond %{HTTP_HOST} !^www.123.com$
  10. RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
  11. </IfModule>
  12. SetEnvIf Request_URI ".*\.gif$" img //去除根据请求信息,不用从服务器上下载,直接访问用户电脑里面的缓存
  13. SetEnvIf Request_URI ".*\.jpg$" img
  14. SetEnvIf Request_URI ".*\.png$" img
  15. SetEnvIf Request_URI ".*\.bmp$" img
  16. SetEnvIf Request_URI ".*\.swf$" img
  17. SetEnvIf Request_URI ".*\.js$" img
  18. SetEnvIf Request_URI ".*\.css$" img
  19. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
  20. <IfModule mod_expires.c>
  21. ExpiresActive on
  22. ExpiresByType image/gif "access plus 1 days"
  23. ExpiresByType image/jpeg "access plus 24 hours"
  24. ExpiresByType image/png "access plus 24 hours"
  25. ExpiresByType text/css "now plus 2 hour"
  26. ExpiresByType application/x-javascript "now plus 2 hours"
  27. ExpiresByType application/javascript "now plus 2 hours"
  28. ExpiresByType application/x-shockwave-flash "now plus 2 hours"
  29. ExpiresDefault "now plus 0 min" //配置各种文件的过期时间
  30. </IfModule>
  31. </VirtualHost>
  32. # /usr/local/apache2.4/bin/apachectl -t //检测配置服务
  33. Syntax OK
  34. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
  35. # vim /usr/local/apache2.4/conf/httpd.conf //配置主配置文件
  36. LoadModule expires_module modules/mod_expires.so # 放开这一行注释
  37. # /usr/local/apache2.4/bin/apachectl -M |grep -i expires //打开该模块
  38. expires_module (shared) // 成功加载expires模块
  39. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
  40. // 访问jpg文件,发现max-age=86400 表示缓存一天
  41. # curl -x127.0.0.1:80 -I www.123.com/wsw.jpg
  42. HTTP/1.1 200 OK
  43. Date: Sat, 22 May 2021 13:30:31 GMT
  44. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  45. Last-Modified: Fri, 21 May 2021 21:58:54 GMT
  46. ETag: "0-5c2de2a749f39"
  47. Accept-Ranges: bytes
  48. Cache-Control: max-age=86400
  49. Expires: Sun, 23 May 2021 13:30:31 GMT
  50. Content-Type: image/jpeg
  51. # curl -x127.0.0.1:80 -I www.123.com/wsw.txt
  52. HTTP/1.1 200 OK
  53. Date: Sat, 22 May 2021 13:31:53 GMT
  54. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  55. Last-Modified: Fri, 21 May 2021 21:58:49 GMT
  56. ETag: "0-5c2de2a23f2e1"
  57. Accept-Ranges: bytes
  58. Cache-Control: max-age=0 // 访问txt 发现 max-age=0 表示没有缓存该类型的文件。
  59. Expires: Sat, 22 May 2021 13:31:53 GMT
  60. Content-Type: text/plain

7、配置防盗链

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //配置虚拟机主文件
  2. # 防盗链
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
  8. <Directory /data/wwwroot/www.123.com>
  9. SetEnvIfNoCase Referer "http://www.123.com" local_ref
  10. SetEnvIfNoCase Referer "http://123.com" local_ref
  11. SetEnvIfNoCase Referer "^$" local_ref //^$为空referer
  12. <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> //用filesmatch定义需要保护的文件类型,访问txt、doc、MP3、zip、rar、jpg、gif格式的文件时会被限制
  13. Order Allow,Deny
  14. Allow from env=local_ref
  15. </filesmatch>
  16. </Directory>
  17. </VirtualHost>
  18. // 使用非允许的referer 报错403
  19. # curl -x127.0.0.1:80 -I -e "http://www.1234.com/1.txt" http://www.123.com/aming.jpg //用-e来定义referer,这个referer一定要以http://开头,否则不管用 ?
  20. HTTP/1.1 403 Forbidden
  21. Date: Sat, 22 May 2021 14:04:32 GMT
  22. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  23. Content-Type: text/html; charset=iso-8859-1
  24. // 访问html文件可以被访问,未被保护
  25. # curl -x127.0.0.1:80 -I -e "http://www.1234.com/1.txt" http://www.123.com/index.html
  26. HTTP/1.1 200 OK
  27. Date: Sat, 22 May 2021 14:05:00 GMT
  28. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  29. Last-Modified: Fri, 21 May 2021 10:34:59 GMT
  30. ETag: "8-5c2d49c8eb296"
  31. Accept-Ranges: bytes
  32. Content-Length: 8
  33. Content-Type: text/html

8、访问控制

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //虚拟机主配置文件
  2. // 访问控制
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
  8. <Directory /data/wwwroot/www.123.com/admin/> //来限制访问的目录
  9. Order deny,allow
  10. Deny from all //所有IP都被限制
  11. Allow from 127.0.0.1 //用此代码只允许127.0.0.1这个IP
  12. </Directory>
  13. </VirtualHost>
  14. # /usr/local/apache2.4/bin/apachectl -t //检测配置
  15. Syntax OK
  16. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
  17. // 配置限制文件
  18. <Directory /data/wwwroot/www.123.com>
  19. <FilesMatch "admin.php(.*)">
  20. Order deny,allow
  21. Deny from all
  22. Allow from 127.0.0.1 // 只允许127.0.0.1IP访问
  23. </FilesMatch>
  24. </Directory>
  25. //创建目录,模拟网站后台
  26. # mkdir /data/wwwroot/www.123.com/admin //创建admin目录,为了模拟网站后台
  27. # echo "admin" > /data/wwwroot/www.123.com/admin/index.html //在后台下创建目录,并写入内容
  28. # > /usr/local/apache2.4/logs/123.com-access_20210522.log //清空日志
  29. # curl -x192.168.200.50:80 -I www.123.com/admin/index.html //测试限制目录
  30. HTTP/1.1 403 Forbidden //验证只允许127.0.0.1这个IP访问
  31. Date: Sat, 22 May 2021 14:34:30 GMT
  32. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  33. Content-Type: text/html; charset=iso-8859-1
  34. # curl -x127.0.0.1:80 -I www.123.com/admin/index.html
  35. HTTP/1.1 200 OK
  36. Date: Sat, 22 May 2021 14:34:44 GMT
  37. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  38. Last-Modified: Sat, 22 May 2021 14:30:13 GMT
  39. ETag: "6-5c2ec03b1ca18"
  40. Accept-Ranges: bytes
  41. Content-Length: 6
  42. Content-Type: text/html

9.禁止解析PHP

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //编写虚拟机主配置文件
  2. // 禁止解析PHP
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
  8. <Directory /data/wwwroot/www.123.com/upload>
  9. php_admin_flag engine off
  10. </Directory>
  11. </VirtualHost>
  12. // 验证
  13. # /usr/local/apache2.4/bin/apachectl -t //检测配置服务
  14. # /usr/local/apache2.4/bin/apachectl graceful // 重新加载配置服务
  15. # mkdir /data/wwwroot/www.123.com/upload // 创建目录、模拟网站上传目录
  16. # cp /usr/local/apache2.4/htdocs/1.php /data/wwwroot/www.123.com/upload/ //复制文件
  17. // 检测
  18. # curl -x127.0.0.1:80 www.123.com/upload/1.php
  19. <?php
  20. echo 111
  21. ?> //测试1.php能否正常启动

10、限制user_age

image.png

  1. # vim /usr/local/apache2.4/conf/extra/httpd-vhost.conf //编辑虚拟机主配置文件
  2. # 限制user_agent
  3. <VirtualHost *:80>
  4. DocumentRoot "/data/wwwroot/www.123.com"
  5. ServerName www.123.com
  6. ServerAlias 123.com
  7. CustomLog "| /usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined
  8. <IfModule mod_rewrite.c>
  9. RewriteEngine on
  10. RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] // %{HTTP_USER_AGENT}为user_agent的内置变量 OR 表示“或者”, NC表示“不区分大小写”
  11. RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] // user——agent匹配curl或者baidu.com
  12. RewriteRule .* - [F] //F相当于Forbidden
  13. </IfModule>
  14. </VirtualHost>
  15. # /usr/local/apache2.4/bin/apachectl -t //检测配置服务
  16. Syntax OK
  17. # /usr/local/apache2.4/bin/apachectl graceful //重新加载配置服务
  18. # curl -I -x127.0.0.1:80 www.123.com/upload/1.php
  19. HTTP/1.1 403 Forbidden
  20. Date: Sat, 22 May 2021 15:28:07 GMT
  21. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  22. Content-Type: text/html; charset=iso-8859-1
  23. //测试
  24. # curl -A "123123" -I -x127.0.0.1:80 www.123.com/upload/1.php //curl的-A选项指定user_agent //user_agent自定义的“123123”,没有匹配任何条件,状态为200
  25. HTTP/1.1 200 OK
  26. Date: Sat, 22 May 2021 15:28:29 GMT
  27. Server: Apache/2.4.33 (Unix) PHP/5.6.39
  28. X-Powered-By: PHP/5.6.39
  29. Content-Type: text/html; charset=UTF-8