Loading... # 一.配置服务文件参数 ## 1.Linux系统中的配置文件 | 配置文件的名称 | 存放位置 | | -------------- | -------------------------- | | 服务目录 | /etc/httpd | | 主配置文件 | /etc/httpd/conf/httpd.conf | | 网站数据目录 | /var/www/html | | 访问日志 | /var/log/httpd/access_log | | 错误日志 | /var/log/httpd/error_log | ## 2.配置httpd服务常用的参数及用途 | 参数 | 用途 | | -------------- | ------------------------- | | ServerRoot | 服务目录 | | ServerAdmin | 管理员邮箱 | | User | 运行服务的用户 | | Group | 运行服务的用户组 | | ServerName | 网站服务器的域名 | | DocumentRoot | 网站数据目录 | | Listen | 监听的IP地址与端口号 | | DirectoryIndex | 默认的索引页页面 | | ErrorLog | 错误日志文件 | | CustomLog | 访问日志文件 | | Timeout | 网页超时时间,默认为300秒 | ## 3.httpd服务的主配置文件构成  # 二.个人用户主页功能 ## 1.基础配置 - **第1步**:在httpd服务程序中,默认没有开启个人用户主页功能。 - 我们需要编辑配置文件,然后在第17行的`UserDir disabled` 参数前面加上井号(#),表示让httpd服务程序开启个人用户主页功能; - 再把第24行的`UserDir public_html` 参数前面的井号(#)去掉(UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录)。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf 1 # 2 # UserDir: The name of the directory that is appended onto a user's home 3 # directory if a ~user request is received. 4 # 5 # The path to the end user account 'public_html' directory must be 6 # accessible to the webserver userid. This usually means that ~userid 7 # must have permissions of 711, ~userid/public_html must have permissions 8 # of 755, and documents contained therein must be world-readable. 9 # Otherwise, the client will only receive a "403 Forbidden" message. 10 # 11 <IfModule mod_userdir.c> 12 # 13 # UserDir is disabled by default since it can confirm the presence 14 # of a username on the system (depending on home directory 15 # permissions). 16 # 17 # UserDir disabled 18 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 # 28 # Control access to UserDir directories. The following is an example 29 # for a site where these directories are restricted to read-only. 30 # 31 <Directory "/home/*/public_html"> 32 AllowOverride FileInfo AuthConfig Limit Indexes 33 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec 34 Require method GET POST OPTIONS 35 </Directory> ``` - **第2步**:在用户家目录中建立用于保存网站数据的目录及首页面文件。 - 另外,还需要把家目录的权限修改为755,保证其他人也有权限读取里面的内容。 ``` [root@linuxprobe home]# su - linuxprobe Last login: Fri May 22 13:17:37 CST 2017 on :0 [linuxprobe@linuxprobe ~]$ mkdir public_html [linuxprobe@linuxprobe ~]$ echo "This is linuxprobe's website" > public_html/index.html [linuxprobe@linuxprobe ~]$ chmod -Rf 755 /home/linuxprobe ``` - **第3步**:重新启动httpd服务程序,在浏览器的地址栏中输入网址,其格式为“网址/~用户名”(其中的波浪号是必需的,而且网址、波浪号、用户名之间没有空格) - 从理论上来讲就可以看到用户的个人网站了。但需要打开Selinux个人用户主页功能 - **第4步**:使用getsebool命令查询并过滤出所有与HTTP协议相关的安全策略。 - off为禁止状态,on为允许状态。 ```shell [root@linuxprobe ~]# getsebool -a | grep http httpd_anon_write --> off httpd_builtin_scripting --> on httpd_can_check_spam --> off httpd_can_connect_ftp --> off httpd_can_connect_ldap --> off httpd_can_connect_mythtv --> off httpd_can_connect_zabbix --> off httpd_can_network_connect --> off httpd_can_network_connect_cobbler --> off httpd_can_network_connect_db --> off httpd_can_network_memcache --> off httpd_can_network_relay --> off httpd_can_sendmail --> off httpd_dbus_avahi --> off httpd_dbus_sssd --> off httpd_dontaudit_search_dirs --> off httpd_enable_cgi --> on httpd_enable_ftp_server --> off httpd_enable_homedirs --> off httpd_execmem --> off httpd_graceful_shutdown --> on httpd_manage_ipa --> off httpd_mod_auth_ntlm_winbind --> off httpd_mod_auth_pam --> off httpd_read_user_content --> off httpd_run_stickshift --> off httpd_serve_cobbler_files --> off httpd_setrlimit --> off httpd_ssi_exec --> off httpd_sys_script_anon_write --> off httpd_tmp_exec --> off httpd_tty_comm --> off httpd_unified --> off httpd_use_cifs --> off httpd_use_fusefs --> off httpd_use_gpg --> off httpd_use_nfs --> off httpd_use_openstack --> off httpd_use_sasl --> off httpd_verify_dns --> off named_tcp_bind_http_port --> off prosody_bind_http_port --> off [root@linuxprobe ~]# setsebool -P httpd_enable_homedirs=on ``` ## 2.添加用户身份验证 > 验证时使用的账户和密码是用htpasswd命令生成的专门用于网站登录的口令密码,而不是系统中的用户密码 - **第1步**:先使用htpasswd命令生成密码数据库。-c参数表示第一次生成;后面再分别添加密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)。 ```shell [root@linuxprobe ~]# htpasswd -c /etc/httpd/passwd linuxprobe New password:此处输入用于网页验证的密码 Re-type new password:再输入一遍进行确认 Adding password for user linuxprobe ``` - **第2步**:编辑个人用户主页功能的配置文件。把第31~35行的参数信息修改成下列内容,其中井号(#)开头的内容为刘遄老师添加的注释信息,可将其忽略。随后保存并退出配置文件,重启httpd服务程序即可生效。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf 27 # 28 # Control access to UserDir directories. The following is an example 29 # for a site where these directories are restricted to read-only. 30 # 31 <Directory "/home/*/public_html"> 32 AllowOverride all #刚刚生成出来的密码验证文件保存路径 33 authuserfile "/etc/httpd/passwd" #当用户尝试访问个人用户网站时的提示信息 34 authname "My privately website" 35 authtype basic #用户进行账户密码登录时需要验证的用户名称 36 require user linuxprobe 37 </Directory> [root@linuxprobe ~]# systemctl restart httpd ``` # 三.虚拟主机功能 ## 1.基于IP地址 - **第1步**:分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并向其中分别写入网站的首页文件。 - **第2步**:在httpd服务的配置文件中大约113行处开始,分别追加写入三个基于IP地址的虚拟主机网站参数,然后保存并退出。记得需要重启httpd服务,这些配置才生效。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… 113 <VirtualHost 192.168.10.10> 114 DocumentRoot /home/wwwroot/10 115 ServerName www.linuxprobe.com 116 <Directory /home/wwwroot/10 > 117 AllowOverride None 118 Require all granted 119 </Directory> 120 </VirtualHost> 121 <VirtualHost 192.168.10.20> 122 DocumentRoot /home/wwwroot/20 123 ServerName bbs.linuxprobe.com 124 <Directory /home/wwwroot/20 > 125 AllowOverride None 126 Require all granted 127 </Directory> 128 </VirtualHost> 129 <VirtualHost 192.168.10.30> 130 DocumentRoot /home/wwwroot/30 131 ServerName tech.linuxprobe.com 132 <Directory /home/wwwroot/30 > 133 AllowOverride None 134 Require all granted 135 </Directory> 136 </VirtualHost> ………………省略部分输出信息……………… [root@linuxprobe ~]# systemctl restart httpd ``` - **第3步**:此时访问网站,则会看到httpd服务程序的默认首页面。 - 需要手动把新的网站数据目录的SELinux安全上下文设置正确 ```shell [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/10 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/10/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/20 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/20/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/30 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/30/* [root@linuxprobe ~]# restorecon -Rv /home/wwwroot ``` ## 2.基于主机域名 - **第1步**:手工定义IP地址与域名之间对应关系的配置文件,保存并退出后会立即生效。 ```shell [root@linuxprobe ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.10 www.linuxprobe.com bbs.linuxprobe.com tech.linuxprobe.com ``` - **第2步**:分别在/home/wwwroot中创建用于保存不同网站数据的三个目录,并向其中分别写入网站的首页文件。 ```shell [root@linuxprobe ~]# mkdir -p /home/wwwroot/www [root@linuxprobe ~]# mkdir -p /home/wwwroot/bbs [root@linuxprobe ~]# mkdir -p /home/wwwroot/tech [root@linuxprobe ~]# echo "WWW.linuxprobe.com" > /home/wwwroot/www/index.html [root@linuxprobe ~]# echo "BBS.linuxprobe.com" > /home/wwwroot/bbs/index.html [root@linuxprobe ~]# echo "TECH.linuxprobe.com" > /home/wwwroot/tech/index.html ``` - **第3步**:在httpd服务的配置文件中大约113行处开始,分别追加写入三个基于主机名的虚拟主机网站参数,然后保存并退出。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… 113 <VirtualHost 192.168.10.10> 114 DocumentRoot "/home/wwwroot/www" 115 ServerName "www.linuxprobe.com" 116 <Directory "/home/wwwroot/www"> 117 AllowOverride None 118 Require all granted 119 </directory> 120 </VirtualHost> 121 <VirtualHost 192.168.10.10> 122 DocumentRoot "/home/wwwroot/bbs" 123 ServerName "bbs.linuxprobe.com" 124 <Directory "/home/wwwroot/bbs"> 125 AllowOverride None 126 Require all granted 127 </Directory> 128 </VirtualHost> 129 <VirtualHost 192.168.10.10> 130 DocumentRoot "/home/wwwroot/tech" 131 ServerName "tech.linuxprobe.com" 132 <Directory "/home/wwwroot/tech"> 133 AllowOverride None 134 Require all granted 135 </directory> 136 </VirtualHost> ………………省略部分输出信息……………… ``` - **第4步**:设置网站数据目录文件的SELinux安全上下文 ```shell [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/* [root@linuxprobe ~]# restorecon -Rv /home/wwwroot ``` ## 3.基于端口号 - **第1步**:分别在/home/wwwroot中创建用于保存不同网站数据的两个目录,并向其中分别写入网站的首页文件。 ```shell [root@linuxprobe ~]# mkdir -p /home/wwwroot/6111 [root@linuxprobe ~]# mkdir -p /home/wwwroot/6222 [root@linuxprobe ~]# echo "port:6111" > /home/wwwroot/6111/index.html [root@linuxprobe ~]# echo "port:6222" > /home/wwwroot/6222/index.html ``` - **第2步**:在httpd服务配置文件的第43行和第44行分别添加用于监听6111和6222端口的参数。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… 33 # 34 # Listen: Allows you to bind Apache to specific IP addresses and/or 35 # ports, instead of the default. See also the <VirtualHost> 36 # directive. 37 # 38 # Change this to Listen on specific IP addresses as shown below to 39 # prevent Apache from glomming onto all bound IP addresses. 40 # 41 #Listen 12.34.56.78:80 42 Listen 80 43 Listen 6111 44 Listen 6222 ………………省略部分输出信息……………… ``` - **第3步**:在httpd服务的配置文件中大约113行处开始,分别追加写入两个基于端口号的虚拟主机网站参数 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… 113 <VirtualHost 192.168.10.10:6111> 114 DocumentRoot "/home/wwwroot/6111" 115 ServerName www.linuxprobe.com 116 <Directory "/home/wwwroot/6111"> 117 AllowOverride None 118 Require all granted 119 </Directory> 120 </VirtualHost> 121 <VirtualHost 192.168.10.10:6222> 122 DocumentRoot "/home/wwwroot/6222" 123 ServerName bbs.linuxprobe.com 124 <Directory "/home/wwwroot/6222"> 125 AllowOverride None 126 Require all granted 127 </Directory> 128 </VirtualHost> ………………省略部分输出信息……………… ``` - **第4步**:设置网站数据目录文件的SELinux安全上下文,使其与网站服务功能相吻合。 ```shell [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/* [root@linuxprobe ~]# restorecon -Rv /home/wwwroot/ ``` - **第5步**:SELinux服务检测到6111和6222端口原本不属于Apache服务应该需要的资源,但现在却以httpd服务程序的名义监听使用了,所以SELinux会拒绝使用Apache服务使用这两个端口。 ```shell [root@linuxprobe ~]# semanage port -l | grep http http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6111 [root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6222 [root@linuxprobe ~]# semanage port -l| grep http http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@linuxprobe ~]# systemctl restart httpd ``` # 四.访问控制 > Apache可以基于源主机名、源IP地址或源主机上的浏览器特征等信息对网站上的资源进行访问控制。它通过Allow指令允许某个主机访问服务器上的网站资源 > > 通过Deny指令实现禁止访问。 > > 在允许或禁止访问网站资源时,还会用到Order指令,这个指令用来定义Allow或Deny指令起作用的顺序,其匹配原则是按照顺序进行匹配,若匹配成功则执行后面的默认指令。 > > 比如“Order Allow, Deny”表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之则拒绝访问请求。 - **第1步**:先在服务器上的网站数据目录中新建一个子目录,并在这个子目录中创建一个包含Successful单词的首页文件。 ```shell [root@linuxprobe ~]# mkdir /var/www/html/server [root@linuxprobe ~]# echo "Successful" > /var/www/html/server/index.html ``` - **第2步**:打开httpd服务的配置文件,在第129行后面添加下述规则来限制源主机的访问。 - 这段规则的含义是允许使用Firefox浏览器的主机访问服务器上的首页文件,除此之外的所有请求都将被拒绝。 ```shell [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… 129 <Directory "/var/www/html/server"> 130 SetEnvIf User-Agent "Firefox" ff=1 131 Order allow,deny 132 Allow from env=ff 133 </Directory> ………………省略部分输出信息……………… [root@linuxprobe ~]# systemctl restart httpd ```  Last modification:December 11, 2021 © Allow specification reprint Like 0 If you think my article is useful to you, please feel free to appreciate