Apache的基础使用

安装Apache

1
2
3
[root@root ~]# yum install httpd			#安装Apache服务
[root@root ~]# systemctl start httpd #开启Apache服务
[root@root ~]# systemctl enable httpd #设置开机自启

更改站点主目录

配置新站点主目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@root ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
113
114 #
115 # DocumentRoot: The directory out of which you will serve your
116 # documents. By default, all requests are taken from this directory, but
117 # symbolic links and aliases may be used to point to other locations.
118 #
119 DocumentRoot "/var/www/html" #将其改为自建站点主目录
120
121 #
122 # Relax access to content within /var/www.
123 #
124 <Directory "/var/www/html"> #将其改为自建站点主目录
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 </Directory>
………………省略部分输出信息………………
[root@root ~]# systemctl restart httpd #重启服务,使其生效
SELinxu限制Apache服务导致的“Forbidden,You don’t have permission to access /index.html on this server.”解决

可以用setenforce [0|1]命令修改SELinux当前的运行模式(0 为禁用,1 为启用)确保问题为SELinux导致的问题

1
[root@root~]# setenforce 0			#修改安全等级为permissive

确保问题为SELinux导致后,进行以下配置
1
2
3
4
5
6
7
8
[root@root ~]# setenforce 1				#修改安全等级为enforecing
[root@root ~]# ls -Zd /var/www/html #查看原站点主目录SELinux安全上下文值
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@root ~]# ls -Zd /home/wwwroot #查看新站点主目录SELinux安全上下文值
drwxrwxrwx. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot #添加一条SELinux安全上下文
[root@lroot ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/* #添加一条SELinu安全上下文
[root@root ~]# restorecon -Rv /home/wwwroot/ #使SELinux 安全上下文立即生效

个人用户主页

开启Apache个人用户功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@root ~]# 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>
在用户家目录中建立用于保存网站数据的目录及首页面文件,并给予权限
1
2
3
4
5
6
7
[root@root home]# su - linuxprobe
Last login: Fri May 22 13:17:37 CST 2017 on :0
[linuxprobe@root ~]$ mkdir public_html
[linuxprobe@root ~]$ echo "This is linuxprobe's website" > public_html/
index.html
[linuxprobe@root ~]$ chmod -Rf 755 /home/linuxprobe
[linuxprobe@root ~]# systemctl restart httpd
SELiux域设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
###使用getsebool 命令查询并过滤出所有与HTTP 协议相关的安全策略
[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@root ~]# setsebool -P httpd_enable_homedirs=on #开启个人用户功能
身份验证功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@root ~]# htpasswd -c /etc/httpd/passwd linuxprobe		#使用htpasswd生成密码数据库
New password:此处输入用于网页验证的密码
Re-type new password:再输入一遍进行确认
Adding password for user linuxprobe
[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

虚拟主机功能

基于IP地址
使用nmcli配置多ip
1
2
[root@root ~]# nmcli connection add type ethernet con-name [网卡名] ifname [网卡配置文件名] autoconnect yes			#为网卡增加配置文件(DHCPi东获取)
[root@root ~]# nmcli connection show #查看网卡配置文件是否创建成功
配置三个基于IP地址的虚拟主机网站参数

以此表配置为例

IP 主机名 主站点目录
192.168.10.10 www1.RHEL.com /home/wwwroot/10
192.168.10.20 www2.RHEL.com /home/wwwroot/20
192.168.10.30 www3.RHEL.com /home/wwwroot/30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@root ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
113 <VirtualHost 192.168.10.10>
114 DocumentRoot /home/wwwroot/10
115 ServerName www1.RHEL.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 www2.RHEL.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 www3.RHEL.com
132 <Directory /home/wwwroot/30 >
133 AllowOverride None
134 Require all granted
135 </Directory>
136 </VirtualHost>
………………省略部分输出信息………………
[root@root ~]# systemctl restart httpd
SELinxu限制Apache服务导致的“Forbidden,You don’t have permission to access /index.html on this server.”解决
1
2
3
4
5
6
7
8
[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
基于主机域名
配置三个基于域名的虚拟主机网站参数

以此表配置为例

IP 主机名 主站点目录
192.168.10.10 www1.RHEL.com /home/wwwroot/www1
192.168.10.10 www2.RHEL.com /home/wwwroot/www2
192.168.10.10 www3.RHEL.com /home/wwwroot/www3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@root ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
113 <VirtualHost 192.168.10.10>
114 DocumentRoot "/home/wwwroot/www1"
115 ServerName "www1.RHEL.com"
116 <Directory "/home/wwwroot/www1">
117 AllowOverride None
118 Require all granted
119 </directory>
120 </VirtualHost>
121 <VirtualHost 192.168.10.10>
122 DocumentRoot "/home/wwwroot/www2"
123 ServerName "www2.RHEL.com"
124 <Directory "/home/wwwroot/www2">
125 AllowOverride None
126 Require all granted
127 </Directory>
128 </VirtualHost>
129 <VirtualHost 192.168.10.10>
130 DocumentRoot "/home/wwwroot/www3"
131 ServerName "www3.RHEL.com"
132 <Directory "/home/wwwroot/www3">
133 AllowOverride None
134 Require all granted
135 </directory>
136 </VirtualHost>
………………省略部分输出信息………………
[root@root ~]#systemctl restart httpd
SELinxu限制Apache服务导致的“Forbidden,You don’t have permission to access /index.html on this server.”解决
1
2
3
4
5
6
7
8
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www1
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www1/*
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www2
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www2/*
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www3
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www3/*
[root@root ~]# restorecon -Rv /home/wwwroot
基于端口号
增加监听端口号,写入两个基于端口号的虚拟主机网站参数

以此表配置为例

IP 端口号 域名 主站点目录
192.168.10.10 6111 www1.RHEL.com /home/wwwroot/6111
192.168.10.10 6222 www2.RHEL.com /home/wwwroot/6222
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@root ~]# 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 #监听6111
44 Listen 6222 #监听6622
………………省略部分输出信息………………
113 <VirtualHost 192.168.10.10:6111>
114 DocumentRoot "/home/wwwroot/6111"
115 ServerName www1.RHEL.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 www2.RHEL.com
124 <Directory "/home/wwwroot/6222">
125 AllowOverride None
126 Require all granted
127 </Directory>
128 </VirtualHost>
………………省略部分输出信息………………
[root@root ~]#systemctl restart httpd
SELinxu限制Apache服务导致的“Forbidden,You don’t have permission to access /index.html on this server.”解决
1
2
3
4
5
6
7
8
9
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
[root@root ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*
[root@root ~]# restorecon -Rv /home/wwwroot/
[root@root ~]# semanage port -a -t http_port_t -p tcp 6111 #增加SELinux允许端口号
[root@root ~]# semanage port -a -t http_port_t -p tcp 6222 #增加SELinux允许端口号
[root@root ~]# semanage port -l| grep http #查看允许端口号
访问控制
1
2
3
4
5
6
7
8
9
10
###允许使用Firefox浏览器的主机访问服务器上的首页文件,除此之外的所有请求都将被拒绝
[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
129 <Directory "/var/www/html/server"> #/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
1
2
3
4
5
6
7
8
9
10
11
###只允许IP 地址为192.168.10.20 的主机访问网站资源
[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
129 <Directory "/var/www/html/server"> #/var/www/html/server为站点主目录
130 Order allow,deny
131 Allow from 192.168.10.20 #只允许192.168.10.20访问
132 Order allow,deny
133 Allow from env=ie
134 </Directory>
………………省略部分输出信息………………
[root@linuxprobe ~]# systemctl restart httpd

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!