httpd配置
1. 工作模式
httpd的工作模式模式有3种
1.1 Prefork 模式
-
特点:
-
每个请求由一个单独的子进程处理。
-
每个子进程只处理一个请求。
-
不使用多线程,因此每个子进程相对独立。
-
-
优点:
- 由于进程是独立的,一个子进程崩溃不会影响其他子进程,因此更加稳定和可靠。
- 对于那些不支持线程的第三方模块或库,这种模式更兼容。
-
缺点:
- 内存使用量较大,因为每个子进程都要分配独立的内存空间。
- 并发处理能力较低,不适合高并发场景。
1.2 Worker 模式
-
特点:
-
使用多线程,每个子进程可以处理多个线程。
-
每个线程处理一个请求。
-
-
优点:
- 内存使用效率高,因为线程共享进程的内存空间。
- 并发处理能力较强,适合高并发场景。
-
缺点:
-
如果线程崩溃,可能会影响整个进程,从而影响多个请求的处理。
-
需要注意线程安全问题,某些不支持线程的第三方模块或库可能不兼容。
-
1.3. Event 模式
-
特点:
-
类似于Worker模式,但更进一步优化了连接处理。
-
采用事件驱动机制,主线程负责接受请求,工作线程负责处理请求。
-
-
优点:
-
更高效的资源利用率,适合处理大量的长连接请求,如WebSocket。
-
可以更好地应对高并发场景,特别是在Keep-Alive连接多的情况下性能更佳。
-
-
缺点:
-
和Worker模式类似,线程安全问题依然需要注意。
-
对一些特殊模块的兼容性可能不如Prefork模式。
-
总结
- Prefork模式适用于对稳定性要求高且不需要处理大量并发连接的场景。
- Worker模式适用于需要处理高并发连接,但对内存使用效率有要求的场景。
- Event模式适用于高并发和长连接的场景,提供了更好的性能和资源利用率。
1.4 调整工作模式
httpd服务默认工作在event模式下,可以使用httpd -V来查看
[root@euler conf.modules.d]# httpd -V Server version: Apache/2.4.37 (centos) Server built: Nov 12 2021 04:57:27 Server's Module Magic Number: 20120211:83 Server loaded: APR 1.6.3, APR-UTIL 1.6.1 Compiled using: APR 1.6.3, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: event threaded: yes (fixed thread count) forked: yes (variable process count)
- Server MPM: event 这里就显示了他当前的工作模式
修改工作模式为perfork
# 修改这个文件 [root@euler ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf LoadModule mpm_prefork_module modules/mod_mpm_prefork.so #LoadModule mpm_worker_module modules/mod_mpm_worker.so #LoadModule mpm_event_module modules/mod_mpm_event.so
这个文件里面会有LoadModule开头的行,默认第三个是开启的,对应的是event模式,你想开启哪个就将哪个模式的注释取消,现在我们将prefork的注释取消,将event注释掉
只能够放开一个注释,如果开启多个会报错
[root@euler ~]# systemctl restart httpd && httpd -V Server version: Apache/2.4.37 (centos) Server built: Nov 12 2021 04:57:27 Server's Module Magic Number: 20120211:83 Server loaded: APR 1.6.3, APR-UTIL 1.6.1 Compiled using: APR 1.6.3, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: prefork threaded: no forked: yes (variable process count) Server compiled with....
现在httpd的工作模式就变成了prefork
2. httpd配置文件解析
httpd的主配置文件在/etc/httpd/conf/httpd.conf,这个文件里的内容非常多,但同时也有非常多的行是被注释掉的,现在我们将没有被注释的行给取出来
[root@ceph conf]# grep -Ev "#|^$" httpd.conf ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost AllowOverride none Require all denied DocumentRoot "/var/www/html" "/var/www"> AllowOverride None Require all granted "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted DirectoryIndex index.html ".ht*"> Require all denied ErrorLog "logs/error_log" LogLevel warn LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio CustomLog "logs/access_log" combined ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" "/var/www/cgi-bin"> AllowOverride None Options None Require all granted TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml AddDefaultCharset UTF-8 MIMEMagicFile conf/magic EnableSendfile on IncludeOptional conf.d/*.conf
这里面有用的配置一共就这么些,我们来逐行分析
2.1 ServerRoot
这个配置项指的是httpd服务的根目录,并不是用户访问时的根目录,这个较好理解,不过多阐述
2.2 Listen
这个指的是httpd监听哪个端口,默认监听在80上,我们将其修改
[root@ceph conf]# grep ^Listen httpd.conf Listen 9876 [root@ceph conf]# systemctl restart httpd [root@ceph conf]# ss -ntpl |grep 9876 LISTEN 0 511 *:9876 *:* users:(("httpd",pid=6584,fd=4),("httpd",pid=6583,fd=4),("httpd",pid=6582,fd=4),("httpd",pid=6580,fd=4))
现在他就监听在9876上了,可以尝试访问一下这个端口
[root@ceph conf]# echo hello > /var/www/html/index.html [root@ceph conf]# curl localhost:9876 hello
可以访问到,如果你的回显不是hello的话,排查一下selinux的状态,如果selinux处于enforcing的模式,使用命令semanage port -a -t http_port_t 9876 -p tcp
执行完这个命令之后就应该可以正常访问了
2.3 Include
这个配置项就是说要去加载conf.modules.d/*.conf,那这个conf.modules.d这个目录在哪?他写的并不是绝对路径,httpd服务怎么知道去哪找这个目录呢?这个时候就需要第一个配置项了ServerRoot,他会从ServerRoot指定的目录去找这个conf.modules.d目录,然后加载这个目录下的所有以.conf结尾的配置
2.4 User & Group
这个是指定httpd使用哪个用户去启动worker进程,主进程只能是root启动,因为默认情况下httpd监听80端口,而普通用户只能监听1024以上的端口,所以就只能使用root来启动主进程
修改user和group
# 修改前查一下 [root@ceph conf]# ps -aux |grep httpd root 6580 0.0 0.3 17464 11012 ? Ss 14:34 0:00 /usr/sbin/httpd -DFOREGROUND apache 6581 0.0 0.1 17428 6672 ? S 14:34 0:00 /usr/sbin/httpd -DFOREGROUND apache 6582 0.0 0.4 2418692 16080 ? Sl 14:34 0:00 /usr/sbin/httpd -DFOREGROUND apache 6583 0.0 0.3 2156484 11984 ? Sl 14:34 0:00 /usr/sbin/httpd -DFOREGROUND apache 6584 0.0 0.4 2222020 14096 ? Sl 14:34 0:00 /usr/sbin/httpd -DFOREGROUND
可以看到,除了第一个是root之外,其他进程都是以apache的用户身份去启动的
# 修改user & group为test用户 [root@ceph conf]# useradd test [root@ceph conf]# grep -A 1 ^User httpd.conf User test Group test [root@ceph conf]# systemctl reload httpd [root@ceph conf]# ps -aux |grep httpd root 6580 0.0 0.3 17464 11028 ? Ss 14:34 0:00 /usr/sbin/httpd -DFOREGROUND test 7652 0.0 0.1 17768 6692 ? S 14:50 0:00 /usr/sbin/httpd -DFOREGROUND test 7653 0.0 0.4 2156496 14288 ? Sl 14:50 0:00 /usr/sbin/httpd -DFOREGROUND test 7654 0.0 0.4 2353168 16324 ? Sl 14:50 0:00 /usr/sbin/httpd -DFOREGROUND test 7655 0.0 0.4 2156496 14280 ? Sl 14:50 0:00 /usr/sbin/httpd -DFOREGROUND
这里我们可以看见,用户从apache变成了test
2.5 ServerAdmin
这个配置项用来指定管理员的邮箱,正常情况下是看不见的,如果服务区遇到了500的状态码,这个邮箱就会被显示在浏览器上
我们先将邮箱修改掉
[root@ceph conf]# grep ^ServerAdmin httpd.conf ServerAdmin openEuler@example.com
还需要在配置文件里面修改一行内容
153 AllowOverride All
应该在153行附近,将默认的AllowOverride None 改为 AllowOverride All
然后来到/var/www/html
我们瞎写一段配置
# 我们在/var/www/html创建一个隐藏文件 [root@ceph html]# vim .htaccess "/var/www/html"> adfasdfaadfa
现在我们重启服务
[root@ceph conf]# systemctl restart httpd
来到浏览器访问

这里就会显示管理员的邮箱
2.6
这种配置都是给一个目录指定一个访问策略,需要看里面具体写了什么内容
AllowOverride none
Require all denied
他是这样写的,给定的目录是/,Require 就是你可以写的策略,他这里是拒绝所有,也就是不让你访问网站的根目录
AllowOverride none 这个用来控制 .htaccess 这样的文件,配置为none则忽略这些文件里面写的策略,配置为All则是不忽略
这一整段的意思是:忽略网站根目录下的.htaccess这样的文件,并且不允许访问根目录
2.7 File
可以对目录授权,相对应的,当然也可以对文件进行授权,这样做的意思就是,我有个文件需要放在网站的目录下,但是我并不想让这个文件被网页所访问到,这样的场景我们就可以使用file进行对文件授权
Require all denied
它默认的这一段配置写的是拒绝访问以.ht开头的所有文件
2.8 DocumentRoot
这个用来指定网页的根目录,也就是网页文件放在哪里,默认是/var/www/html
如果你要将这个目录给改到其他地方去,改了这一个地方之后你依然是访问不到的,你去访问会显示403(权限拒绝),产生这个错误的原因是你没有对新更改的目录进行授权
# 我们将DocumentRoot改到/www,则最少需要写这些内容 [root@ceph conf]# vim httpd.conf DocumentRoot "/www" "/www"> Require all granted [root@ceph conf]# mkdir /www [root@ceph conf]# cd /www [root@ceph www]# echo "DocumentRoot is /www" > index.html [root@ceph www]# systemctl restart httpd [root@ceph www]# curl localhost DocumentRoot is /www
现在网站的根目录就被改到了/www下了,如果还是访问不到的话去关闭selinux
这些就是httpd常用的基础配置了
3. 高级配置
3.1 httpd的长连接
httpd的长连接默认是开启的,需要配置开启/关闭的话
[root@ceph httpd]# vim /usr/share/doc/httpd/httpd-default.conf # KeepAlive: Whether or not to allow persistent connections (more than # one request per connection). Set to "Off" to deactivate. # KeepAlive On
这里面这个参数改为off就是关闭,默认就是on
3.2 配置资源访问策略
在上面我们看到了目录和文件的访问策略,但是没有具体的去写配置,在这里我们会写一些配置
3.2.1 策略的配置
配置的选项:
- Options: 配置目录的选项
- Indexes:如果在目录中找不到默认的首页文件(index.html),则索引当前的目录

-
FollowSymLinks:允许httpd访问目录中软链接的源文件
-
ALL:启用所有选项
-
None:禁用所有的配置选项
-
AllowOverride:是否允许
.htaccess这个文件中的策略生效,默认值为None- All:全部都生效
- None:全部都不生效
- AutoConfig:默认配置生效,其他指令都不生效
3.2.2 访问控制
1. 基于客户端的IP进行访问控制
需求,允许所有人访问,唯独不允许192.168.200.1这个IP访问
[root@ceph conf]# vim httpd.conf DocumentRoot "/www" "/www"> Require all granted Require not ip 192.168.200.1
这里的策略就是对于网站根目录/www,允许所有人访问,但是192.168.200.1这个IP不能访问
如果是指定白名单的话配置就是这样的
DocumentRoot "/www" "/www"> Require ip 192.168.200.1
直接指定ip就可以了,因为httpd默认策略就是拒绝,然后我们只需要告诉他一个允许访问的ip也就是白名单了
测试
# 在本地测试 [root@ceph conf]# curl localhost DocumentRoot is /www # 在192.168.200.1访问 C:\Users\86156>curl 192.168.200.210 "-//IETF//DTD HTML 2.0//EN"> <head> 403 Forbidden Forbidden
You don't have permission to access this resource.