配置服务文件参数

  1. [root@localhost ~]# yum -y install httpd
  2. [root@localhost ~]# systemctl start httpd
  3. [root@localhost ~]# systemctl enable httpd
  4. [root@localhost ~]# firewall-cmd --add-service=http --permanent
  5. [root@localhost ~]# firewall-cmd --reload

Linux系统中的配置文件

配置文件的名称 存放位置
服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
网站数据目录 /var/www/html
访问日志 /var/log/httpd/access_log
错误日志 /var/log/httpd/error_log
  1. [root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep -v '#' | grep -v '^$'
  2. ServerRoot "/etc/httpd"
  3. Listen 80
  4. Include conf.modules.d/*.conf
  5. User apache
  6. Group apache
  7. ServerAdmin root@localhost
  8. <Directory />
  9. AllowOverride none
  10. Require all denied
  11. </Directory>
  12. DocumentRoot "/var/www/html"
  13. <Directory "/var/www">
  14. AllowOverride None
  15. Require all granted
  16. </Directory>
  17. <Directory "/var/www/html">
  18. Options Indexes FollowSymLinks
  19. AllowOverride None
  20. Require all granted
  21. </Directory>
  22. <IfModule dir_module>
  23. DirectoryIndex index.html
  24. </IfModule>
  25. <Files ".ht*">
  26. Require all denied
  27. </Files>
  28. ErrorLog "logs/error_log"
  29. LogLevel warn
  30. <IfModule log_config_module>
  31. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  32. LogFormat "%h %l %u %t \"%r\" %>s %b" common
  33. <IfModule logio_module>
  34. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  35. </IfModule>
  36. CustomLog "logs/access_log" combined
  37. </IfModule>
  38. <IfModule alias_module>
  39. ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  40. </IfModule>
  41. <Directory "/var/www/cgi-bin">
  42. AllowOverride None
  43. Options None
  44. Require all granted
  45. </Directory>
  46. <IfModule mime_module>
  47. TypesConfig /etc/mime.types
  48. AddType application/x-compress .Z
  49. AddType application/x-gzip .gz .tgz
  50. AddType text/html .shtml
  51. AddOutputFilter INCLUDES .shtml
  52. </IfModule>
  53. AddDefaultCharset UTF-8
  54. <IfModule mime_magic_module>
  55. MIMEMagicFile conf/magic
  56. </IfModule>
  57. EnableSendfile on
  58. IncludeOptional conf.d/*.conf

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

参数 用途
ServerRoot 服务目录
ServerAdmin 管理员邮箱
User 运行服务的用户
Group 运行服务的用户组
ServerName 网站服务器的域名
DocumentRoot 网站数据目录
Directory 网站数据目录的权限
Listen 监听的IP地址与端口号
DirectoryIndex 默认的索引页页面
ErrorLog 错误日志文件
CustomLog 访问日志文件
Timeout 网页超时时间,默认为300秒

网站数据是保存在/var/www/html目录中,而如果想把保存网站数据的目录修改为/home/wwwroot目录
第1步:建立网站数据的保存目录,并创建首页文件

  1. [root@localhost ~]# mkdir /home/wwwroot
  2. [root@localhost ~]# echo "<h1>hello</h1>" > /home/wwwroot/index.html

第2步:打开httpd服务程序的主配置文件,将用于定义网站数据保存路径的参数DocumentRoot修改为/home/wwwroot,同时还需要将用于定义目录权限的参数Directory后面的路径也修改为/home/wwwroot

  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  2. ServerRoot "/etc/httpd"
  3. Listen 80
  4. Include conf.modules.d/*.conf
  5. User apache
  6. Group apache
  7. ServerAdmin root@localhost
  8. <Directory />
  9. AllowOverride none
  10. Require all denied
  11. </Directory>
  12. DocumentRoot "/home/wwwroot" #这个地方要改
  13. <Directory "/home/wwwroot"> #这个地方要改
  14. AllowOverride None
  15. Require all granted
  16. </Directory>
  17. <Directory "/var/www/html">
  18. Options Indexes FollowSymLinks
  19. AllowOverride None
  20. Require all granted
  21. </Directory>
  22. ..................................

第3步:重新启动httpd服务程序并验证效果

  1. [root@localhost ~]# systemctl restart httpd

image.png
第4步:修改selinux安全上下文,在我们新建的目录selinux权限中添加

  1. [root@localhost ~]# ls -Zd /var/www/html/
  2. drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
  3. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/
  4. [root@localhost ~]# restorecon -Rv /home/wwwroot/
  5. restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:user_home_dir_t:s0
  6. restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

然后刷新就可以看到页面了
image.png

个人用户主页功能

第1步:在httpd服务程序中,默认没有开启个人用户主页功能
在第17行的UserDir disabled参数前面加上井号(#),表示让httpd服务程序开启个人用户主页功能;同时再把第24行的UserDir public_html参数前面的井号(#)去掉(UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录)

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

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

  1. [root@localhost ~]# su - aaron
  2. [aaron@localhost ~]$ mkdir public_html
  3. [aaron@localhost ~]$ echo "aaron's website" > public_html/index.html
  4. [aaron@localhost ~]$ chmod -Rf 755 /home/aaron/

第3步:重新启动httpd服务程序,修改selinux安全上下文,网址格式为网址/~用户名(其中的波浪号是必需的,而且网址、波浪号、用户名之间没有空格)

  1. [root@localhost ~]# getsebool -a | grep http
  2. httpd_anon_write --> off
  3. httpd_builtin_scripting --> on
  4. httpd_can_check_spam --> off
  5. httpd_can_connect_ftp --> off
  6. httpd_can_connect_ldap --> off
  7. httpd_can_connect_mythtv --> off
  8. httpd_can_connect_zabbix --> off
  9. httpd_can_network_connect --> off
  10. httpd_can_network_connect_cobbler --> off
  11. httpd_can_network_connect_db --> off
  12. httpd_can_network_memcache --> off
  13. httpd_can_network_relay --> off
  14. httpd_can_sendmail --> off
  15. httpd_dbus_avahi --> off
  16. httpd_dbus_sssd --> off
  17. httpd_dontaudit_search_dirs --> off
  18. httpd_enable_cgi --> on
  19. httpd_enable_ftp_server --> off
  20. httpd_enable_homedirs --> off
  21. httpd_execmem --> off
  22. httpd_graceful_shutdown --> on
  23. httpd_manage_ipa --> off
  24. httpd_mod_auth_ntlm_winbind --> off
  25. httpd_mod_auth_pam --> off
  26. httpd_read_user_content --> off
  27. httpd_run_ipa --> off
  28. httpd_run_preupgrade --> off
  29. httpd_run_stickshift --> off
  30. httpd_serve_cobbler_files --> off
  31. httpd_setrlimit --> off
  32. httpd_ssi_exec --> off
  33. httpd_sys_script_anon_write --> off
  34. httpd_tmp_exec --> off
  35. httpd_tty_comm --> off
  36. httpd_unified --> off
  37. httpd_use_cifs --> off
  38. httpd_use_fusefs --> off
  39. httpd_use_gpg --> off
  40. httpd_use_nfs --> off
  41. httpd_use_openstack --> off
  42. httpd_use_sasl --> off
  43. httpd_verify_dns --> off
  44. named_tcp_bind_http_port --> off
  45. prosody_bind_http_port --> off
  46. [root@localhost ~]# setsebool -P httpd_enable_homedirs=on

image.png

给网页加密码

第1步:先使用htpasswd命令生成密码数据库。-c参数表示第一次生成;后面再分别添加密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)。

  1. [root@localhost ~]# htpasswd -c /etc/httpd/passwd aaron
  2. New password:
  3. Re-type new password:
  4. Adding password for user aaron

第2步:编辑个人用户主页功能的配置文件
把第31~35行的参数信息修改成下列内容,其中井号(#)开头的内容为刘遄老师添加的注释信息,可将其忽略。随后保存并退出配置文件,重启httpd服务程序即可生效

  1. [root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
  2. 31 <Directory "/home/*/public_html">
  3. 32 AllowOverride all
  4. 33 authuserfile "/etc/httpd/passwd"
  5. 34 authname "My privately website"
  6. 35 authtype basic
  7. 36 # AllowOverride FileInfo AuthConfig Limit Indexes
  8. 37 require user aaron
  9. 38 </Directory>

image.png

虚拟主机功能

第1步:分别在/home/wwwroot中创建用于保存不同网站数据的三个目录,并向其中分别写入网站的首页文件。每个首页文件中应有明确区分不同网站内容的信息

  1. [root@localhost ~]# mkdir -p /home/wwwroot/www
  2. [root@localhost ~]# mkdir -p /home/wwwroot/bbs
  3. [root@localhost ~]# mkdir -p /home/wwwroot/tech
  4. [root@localhost ~]# echo "<h1>www</h1>" > /home/wwwroot/www/index.html
  5. [root@localhost ~]# echo "<h1>bbs</h1>" > /home/wwwroot/bbs/index.html
  6. [root@localhost ~]# echo "<h1>tech</h1>" > /home/wwwroot/tech/index.html

第2步:在httpd服务的配置文件中,分别追加写入三个基于主机名的虚拟主机网站参数,然后保存并退出。记得需要重启httpd服务,这些配置才生效

  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  2. [root@localhost ~]# tail -n 30 /etc/httpd/conf/httpd.conf #在最后加上虚拟主机的配置
  3. AddDefaultCharset UTF-8
  4. <IfModule mime_magic_module>
  5. MIMEMagicFile conf/magic
  6. </IfModule>
  7. EnableSendfile on
  8. IncludeOptional conf.d/*.conf
  9. <VirtualHost 192.168.91.128>
  10. DocumentRoot "/home/wwwroot/www"
  11. ServerName "www.eagleslab.com"
  12. <Directory "/home/wwwroot/www">
  13. AllowOverride None
  14. Require all granted
  15. </Directory>
  16. </VirtualHost>
  17. <VirtualHost 192.168.91.128>
  18. DocumentRoot "/home/wwwroot/bbs"
  19. ServerName "bbs.eagleslab.com"
  20. <Directory "/home/wwwroot/bbs">
  21. AllowOverride None
  22. Require all granted
  23. </Directory>
  24. </VirtualHost>
  25. <VirtualHost 192.168.91.128>
  26. DocumentRoot "/home/wwwroot/tech"
  27. ServerName "tech.eagleslab.com"
  28. <Directory "/home/wwwroot/tech">
  29. AllowOverride None
  30. Require all granted
  31. </Directory>
  32. </VirtualHost>
  33. [root@localhost ~]# systemctl restart httpd

第3步:因为当前的网站数据目录还是在/home/wwwroot目录中,因此还是必须要正确设置网站数据目录文件的SELinux安全上下文,使其与网站服务功能相吻合。最后记得用restorecon命令让新配置的SELinux安全上下文立即生效,这样就可以立即访问到虚拟主机网站了

  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/www
  3. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
  4. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
  5. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
  6. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
  7. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*
  8. [root@localhost ~]# restorecon -Rv /home/wwwroot
  9. restorecon reset /home/wwwroot context unconfined_u:object_r:user_home_dir_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  10. restorecon reset /home/wwwroot/bbs context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  11. restorecon reset /home/wwwroot/bbs/index.html context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  12. restorecon reset /home/wwwroot/tech context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  13. restorecon reset /home/wwwroot/tech/index.html context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

image.png