当前位置:   article > 正文

使用Apache部署静态网站_apache配置静态资源目录

apache配置静态资源目录

配置服务文件参数-Linux系统中的配置文件

服务目录:/etc/httpd

主配置文件:/etc/httpd/conf/httpd.conf

网站数据目录:/var/www/html

访问日志:/var/log/httpd/access_log

错误日志:/var/log/httpd/error_log

HTTP服务主配置文件的参数结构

  1. //全局配置
  2. ServerName www.linuxprobe.com
  3. ServerRoot /etc/httpd
  4. ........
  5. //区域配置
  6. <Directory>
  7. ....
  8. </Directory>
  9. ....
  10. //区域配置
  11. <Location /server-status>
  12. ....
  13. </Location>
  14. ....

配置httpd服务程序时最常用的参数以及用途描述

  1. //ServerRoot:服务目录
  2. //ServerAdmin:管理员邮箱
  3. //User:运行服务的用户
  4. //Group:运行服务的用户组
  5. //ServerName:网站服务器的域名
  6. //DocumentRoot:网站数据目录
  7. //Listen:监听的IP地址与端口
  8. //DirectoryIndex:默认的索引页页面
  9. //ErrorLog:错误日志文件
  10. //CustomLog:访问日志文件
  11. //Timeout:网页超时时间,默认为300
  12. //修改默认页面
  13. [root@localhost ~]# echo "Welcome to 192.168.95.100 LinuxProbe.com" > /var/www/html/index.html
  14. //建立网站数据目录
  15. [root@localhost ~]# echo "Welcome to 192.168.95.100 LinuxProbe.com" > /var/www/html/index.html
  16. [root@localhost ~]# mkdir /home/wwwroot
  17. [root@localhost ~]# echo "The New Web Directory" > /home/wwwroot/index.html
  18. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  19. DocumentRoot "/home/wwwroot"
  20. #
  21. # Relax access to content within /var/www.
  22. #
  23. <Directory "/home/wwwroot">
  24. AllowOverride None
  25. # Allow open access:
  26. Require all granted
  27. </Directory>
  28. # Further relax access to the default document root:
  29. <Directory "/home/wwwroot">
  30. .....省略部分输出信息.....
  31. </Directory>
  32. //SELinux三种配置模式
  33. 1.enforcing:强制启用安全策略模式,将拦截服务的不合法请求。
  34. 2.permissive:遇到服务越权访问时,只发出警告而不拦截
  35. 3.disbaled:对于越权行为不警告也不拦截
  36. //查看SELinux运行模式,定义的是SELinux的默认运行状态,它不会再更改后立即生效
  37. [root@localhost ~]# vim /etc/selinux/config
  38. # This file controls the state of SELinux on the system.
  39. # SELINUX= can take one of these three values:
  40. # enforcing - SELinux security policy is enforced.
  41. # permissive - SELinux prints warnings instead of enforcing.
  42. # disabled - No SELinux policy is loaded.
  43. SELINUX=enforcing
  44. //使用getenforce命令获得当前SELinux运行模式
  45. [root@localhost ~]# getenforce
  46. Enforcing
  47. //使用setenforce [0/1]修改当前运行模式(0为禁用,1为启用)
  48. [root@localhost ~]# setenforce 0
  49. [root@localhost ~]# getenforce
  50. Permissive
  51. //再次刷新网页可正常显示内容
  52. //semanage命令:用于管理SELinux的策略,语法格式为"semanage [参数] [文件]"
  53. //semanage命令中的常用参数及作用
  54. //-l:查询
  55. //-a:添加
  56. //-m:修改
  57. //-d:删除

向新的网站数据目录中新添加一条SELinux安全上下文,让这个目录以及里面的所有文件能够被httpd服务程序访问到

  1. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
  2. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

//执行上述设置之后,还无法立即访问网站,还需要使用restorecon命令将设置好的SELinux安全上下文立即生效。使用-Rv参数对指定的目录进行递归操作,以及显示SELinux安全上下文的修改过程

  1. [root@localhost ~]# restorecon -Rv /home/wwwroot/
  2. Relabeled /home/wwwroot from unconfined_u:object_r:user_home_dir_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
  3. Relabeled /home/wwwroot/index.html from unconfined_u:object_r:user_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

个人用户主页功能

httpd服务程序中,默认没有开启个人用户主页功能。为此,我们需要编辑下面的配置文件,在UserDir disabled参数前面加上#,表示让httpd服务程序开启个人用户主页功能;同时把UserDir public_html参数前面的#去掉,UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录。

  1. [root@localhost wwwroot]# vim /etc/httpd/conf.d/userdir.conf
  2. #
  3. # UserDir: The name of the directory that is appended onto a user's home
  4. # directory if a ~user request is received.
  5. #
  6. # The path to the end user account 'public_html' directory must be
  7. # accessible to the webserver userid. This usually means that ~userid
  8. # must have permissions of 711, ~userid/public_html must have permissions
  9. # of 755, and documents contained therein must be world-readable.
  10. # Otherwise, the client will only receive a "403 Forbidden" message.
  11. #
  12. <IfModule mod_userdir.c>
  13. #
  14. # UserDir is disabled by default since it can confirm the presence
  15. # of a username on the system (depending on home directory
  16. # permissions).
  17. #
  18. # UserDir disabled
  19. #
  20. # To enable requests to /~user/ to serve the user's public_html
  21. # directory, remove the "UserDir disabled" line above, and uncomment
  22. # the following line instead:
  23. #
  24. UserDir public_html
  25. </IfModule>
  26. #
  27. # Control access to UserDir directories. The following is an example
  28. # for a site where these directories are restricted to read-only.
  29. #
  30. <Directory "/home/*/public_html">
  31. AllowOverride FileInfo AuthConfig Limit Indexes
  32. Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
  33. Require method GET POST OPTIONS
  34. </Directory>

//在用户家目录中建立用于保存网站数据的目录及首页面信息。另外,还需要把家目录的权限修改为755,保证其他人也有权限读取里面的内容

  1. [root@localhost wwwroot]# su - linuxprobe
  2. [linuxprobe@localhost ~]$ mkdir public_html
  3. [linuxprobe@localhost ~]$ echo "This is linuxprobe's website" > public_html/index.html
  4. [linuxprobe@localhost ~]$ chmod -R 755 /home/linuxprobe/
  5. [linuxprobe@localhost ~]$ exit
  6. logout
  7. [root@localhost wwwroot]# systemctl restart httpd

//此次报错的原因:SELinux域的概念。SELinux域确保服务程序不能执行违规的操作,只能本本分分地为用户提供服务。httpd服务中突然开启的这项个人用户主页功能到底有没有被SELinux域默认允许

  1. [root@localhost wwwroot]# getsebool -a | grep http
  2. httpd_anon_write --> off
  3. httpd_builtin_scripting --> on
  4. httpd_can_check_spam --> off
  5. httpd_dontaudit_search_dirs --> off
  6. httpd_enable_cgi --> on
  7. httpd_enable_ftp_server --> off
  8. httpd_enable_homedirs --> off
  9. httpd_execmem --> off
  10. httpd_graceful_shutdown --> off
  11. httpd_manage_ipa --> off
  12. [root@localhost wwwroot]# setsebool -P httpd_enable_homedirs=on

//生成密码文件

//让用户通过身份验证才能看到里面的内容

  1. [root@localhost wwwroot]# htpasswd -c /etc/httpd/passwd linuxprobe
  2. New password:
  3. Re-type new password:
  4. Adding password for user linuxprobe
  5. [root@localhost wwwroot]# vim /etc/httpd/conf.d/userdir.conf
  6. 31 <Directory "/home/*/public_html">
  7. 32 AllowOverride all
  8. 33 #刚刚生成出的密码验证文件保存路径
  9. 34 authuserfile "/etc/httpd/passwd"
  10. 35 #当用户访问网站时的提示信息
  11. 36 authname "My Privately website"
  12. 37 #验证方式为密码模式
  13. 38 authtype basic
  14. 39 #访问网站时需要验证的用户名称
  15. 40 require user linuxprobe
  16. 41 </Directory>

虚拟主机

  1. [root@localhost ~]# mkdir -p /home/wwwroot/10
  2. [root@localhost ~]# mkdir -p /home/wwwroot/20
  3. [root@localhost ~]# mkdir -p /home/wwwroot/30
  4. [root@localhost ~]# echo "IP:192.168.10.10" > /home/wwwroot/10/index.html
  5. [root@localhost ~]# echo "IP:192.168.10.20" > /home/wwwroot/20/index.html
  6. [root@localhost ~]# echo "IP:192.168.10.30" > /home/wwwroot/30/index.html
  7. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  8. <VirtualHost 192.168.95.102>
  9. DocumentRoot /home/wwwroot/10
  10. ServerName www.linuxprobe.com
  11. <Directory /home/wwwroot/10>
  12. AllowOverride None
  13. Require all granted
  14. </Directory>
  15. </VirtualHost>
  16. <VirtualHost 192.168.95.104>
  17. DocumentRoot /home/wwwroot/20
  18. ServerName www.linuxcool.com
  19. <Directory /home/wwwroot/10>
  20. AllowOverride None
  21. Require all granted
  22. </Directory>
  23. </VirtualHost>
  24. <VirtualHost 192.168.95.105>
  25. DocumentRoot /home/wwwroot/30
  26. ServerName www.linuxdown.com
  27. <Directory /home/wwwroot/10>
  28. AllowOverride None
  29. Require all granted
  30. </Directory>
  31. </VirtualHost>

当服务器无法为每个网站都分配一个独立IP地址的时候,可以尝试让Apache自动识别用户请求的域名

  1. [root@localhost conf]# vim /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.95.102 www.linuxprobe.com www.linuxcool.com www.linuxdown.com
  5. [root@localhost conf]# ping -c 4 www.linuxprobe.com
  6. PING www.linuxprobe.com (192.168.95.102) 56(84) bytes of data.
  7. 64 bytes from www.linuxprobe.com (192.168.95.102): icmp_seq=1 ttl=64 time=0.039 ms
  8. 64 bytes from www.linuxprobe.com (192.168.95.102): icmp_seq=2 ttl=64 time=0.058 ms
  9. 64 bytes from www.linuxprobe.com (192.168.95.102): icmp_seq=3 ttl=64 time=0.048 ms
  10. 64 bytes from www.linuxprobe.com (192.168.95.102): icmp_seq=4 ttl=64 time=0.049 ms
  11. --- www.linuxprobe.com ping statistics ---
  12. 4 packets transmitted, 4 received, 0% packet loss, time 3068ms
  13. rtt min/avg/max/mdev = 0.039/0.048/0.058/0.009 ms

//基于主机域名

  1. [root@localhost ~]# mkdir -p /home/wwwroot/linuxprobe
  2. [root@localhost ~]# mkdir -p /home/wwwroot/linuxcool
  3. [root@localhost ~]# mkdir -p /home/wwwroot/linuxdown
  4. [root@localhost ~]# echo "www.linuxprobe.com" > /home/wwwroot/linuxprobe/index.html
  5. [root@localhost ~]# echo "www.linuxcool.com" > /home/wwwroot/linuxcool/index.html
  6. [root@localhost ~]# echo "www.linuxdown.com" > /home/wwwroot/linuxdown/index.html
  7. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  8. <VirtualHost 192.168.95.102>
  9. DocumentRoot /home/wwwroot/linuxprobe
  10. ServerName www.linuxprobe.com
  11. <Directory /home/wwwroot/linuxprobe>
  12. AllowOverride None
  13. Require all granted
  14. </Directory>
  15. </VirtualHost>
  16. <VirtualHost 192.168.95.102>
  17. DocumentRoot /home/wwwroot/linuxcool
  18. ServerName www.linuxcool.com
  19. <Directory /home/wwwroot/linuxcool>
  20. AllowOverride None
  21. Require all granted
  22. </Directory>
  23. </VirtualHost>
  24. <VirtualHost 192.168.95.102>
  25. DocumentRoot /home/wwwroot/linuxdown
  26. ServerName www.linuxdown.com
  27. <Directory /home/wwwroot/linuxdown>
  28. AllowOverride None
  29. Require all granted
  30. </Directory>
  31. </VirtualHost>

//基于主机端口

  1. [root@localhost ~]# mkdir -p /home/wwwroot/6111
  2. [root@localhost ~]# mkdir -p /home/wwwroot/6222
  3. [root@localhost ~]# mkdir -p /home/wwwroot/6333
  4. [root@localhost ~]# echo "port:6111" > /home/wwwroot/6111/index.html
  5. [root@localhost ~]# echo "port:6222" > /home/wwwroot/6222/index.html
  6. [root@localhost ~]# echo "port:6333" > /home/wwwroot/6333/index.html
  7. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  8. <VirtualHost 192.168.95.102:6111>
  9. DocumentRoot /home/wwwroot/6111
  10. ServerName www.linuxprobe.com
  11. <Directory /home/wwwroot/6111>
  12. AllowOverride None
  13. Require all granted
  14. </Directory>
  15. </VirtualHost>
  16. <VirtualHost 192.168.95.102:6222>
  17. DocumentRoot /home/wwwroot/6222
  18. ServerName www.linuxcool.com
  19. <Directory /home/wwwroot/6222>
  20. AllowOverride None
  21. Require all granted
  22. </Directory>
  23. </VirtualHost>
  24. <VirtualHost 192.168.95.102:6333>
  25. DocumentRoot /home/wwwroot/6333
  26. ServerName www.linuxdown.com
  27. <Directory /home/wwwroot/6333>
  28. AllowOverride None
  29. Require all granted
  30. </Directory>
  31. </VirtualHost>
  32. #Listen 12.34.56.78:80
  33. Listen 80
  34. Listen 6111
  35. Listen 6222
  36. Listen 6333
  37. [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6111
  38. [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6222
  39. [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6333
  40. [root@localhost ~]# systemctl restart httpd.service
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/851478
推荐阅读
相关标签
  

闽ICP备14008679号