1.3http的配置详解-1-编译安装,持久连接,MPM,DSO,修改根等

496 阅读12分钟

http的基本配置详解

apr这个软件是apache依赖的,要编译安装http,必须要保证apr版本和http版本配合

因为centos8上的http版本很新2.4,就在centos7上编译安装,不能用yum的apr版本,因为太久,不支持httpd2.4.41的安装

yum info httpd
Name        : httpd
Arch        : x86_64
Version     : 2.4.6
yum info apr
Name        : apr
Arch        : x86_64
Version     : 1.4.8
版本有点偏旧

1.httpd的编译安装

1.1 编译安装httpd-2.4方法一:源码分开编译

进入官网apr.apache.org

下载apr和apr-util,bz2压缩比更高

准备

  • 安装相关包:yum -y install gcc make pcre-devel openssl-devel expat-devel
  • 下载源代码并解压缩:httpd-2.4.41.tar.bz2,apr-1.7.0.tar.bz2,apr-util-1.6.1.tar.bz

wget mirrors.tuna.tsinghua.edu.cn/apache//apr…

wget mirrors.tuna.tsinghua.edu.cn/apache//apr…

wget mirrors.tuna.tsinghua.edu.cn/apache//htt…

解包:tar jxvf

1.编译安装APR-基本的可移植库

cd apr-1.7.0
./configure --prefix=/app/apr
make && make install

2.编译安装APR-util-在APR之上提供了许多有用的抽象

cd ../apr-util-1.6.1
./configure --prefix=/app/apr-util --with-apr=/app/apr/  
# 依赖之前的apr编译
make -j 2 && make install

3.编译安装httpd-2.4

cd ../httpd-2.4.41
./configure --prefix=/app/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/app/apr/ \
--with-apr-util=/app/apr-util/ \
# 主要要单独指定apr和apr-util
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork 
# 多路处理模块MPM={prefork|worker|event}
make -j 4 && make install

1.2 编译安装httpd-2.4方法二:源码合并编译

准备

  • selinux关闭,防火墙关闭,时间同步
  • 安装相关包:yum -y install gcc make pcre-devel openssl-devel expat-devel
  • 下载源代码并解压缩:httpd-2.4.41.tar.bz2,apr-1.7.0.tar.bz2,apr-util-1.6.1.tar.bz

wget mirrors.tuna.tsinghua.edu.cn/apache//apr…

wget mirrors.tuna.tsinghua.edu.cn/apache//apr…

wget mirrors.tuna.tsinghua.edu.cn/apache//htt…

解包:tar jxvf

1.将apr和apr-util源码与httpd源码合并

mv apr-1.7.0 httpd-2.4.41/srclib/apr
mv apr-util-1.6.1 httpd-2.4.41/srclib/apr-util
ls httpd-2.4.41/srclib/
apr apr-util  Makefile.in 

2.将三者一并编译并安装

cd httpd-2.4.41/
./configure \
--prefix=/app/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-moudules=most \
--enable-mpms-shared=all \
--with-mpm=prefork 
make -j 4 && make install

目录分析

bin 二进制程序所在目录-主程序和客户端工具都在这
	httpd 二进制执行程序
	apachectl 控制台脚本-不加任何参数也是启动
	# 必须要解决路径调用才可以
		/app/httpd24/apachectl -h
		如果直接调用apachectl -h没有写到path路径下
		如果bin下bash/. apachectl -h会退出客户端
	# 用ls |xargs file即可

cgi-bin
error
icons
lib
man
modules
build
conf:存放配置文件
htdocs:主页面存放位置
	index.html
include
logs
manual

2.编译安装后配置

http编译过程:/app/httpd24/build/config.nice

自带的服务控制脚本:/app/httpd24/bin/apchectl

2.1创建专门用户

useradd -s /sbin/nologin -r apache
# 自动创建用户和组

2.2指定运行httpd用户

vim /app/httpd24/conf/httpd
User apache
Group apache
# 用来起worker进程
# 如果之前有yum安装过httpd,apache账号会自动创建

2.3配置环境变量

vim /etc/profile.d/httpd24.sh
PATH=/app/httpd24/bin:$PATH

. /etc/profile.d/httpd24.sh 让脚本生效
# 解决调用可执行脚本的全路径调用的问题

2.4配置帮助-通常不配

vim /etc/man_db.conf
MANDATORY_MANPATH   /app/http24/man

2.5设置开机启动-不建议-已经淘汰

vim /etc/rc.d/rc.local
/app/httpd24/bin/apachectl start
chmod +x /etc/rc.d/rc.local

# 开机启动还有service文件和脚本

2.6.1创建service unit 文件(centos7以上版本)-可以实现systemctl的集中管理-建议

vim /usr/lib/systemd/system/httpd24.service

[unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#Environment=/etc/sysconfig/httpd
ExecStart=/app/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/app/httpd24/bin/httpd $OPTIONs -k graceful
ExecStop=/bin/kill -WINCH {MAINPID}
# 用httpd的stop也可以
killsignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target


# 上述的都不用完全自己写的,手工在yum安装的httpd里有service文件,可以拿来参考修改
# 修改服务文件后要systemctl daemon-reload

2.6.2创建启动脚本(centos6以前版本-未来会被淘汰)

# 自定义启动脚本(参考httpd-2.2的服务脚本)
cp /etc/rc.d/init.d/httpd /etc/rc.d/init.d/httpd24
vim /etc/rc.d/init.d/httpd24
apachectl=/app/httpd24/bin/apachectl
httpd=${HTTPD-/app/httpd24/bin/httpd}
pidfile=¥{PIDFILE-/app/htttpd24/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}

chkconfig -add httpd24
chkconfig -add httpd24

3.httpd常见配置-主配置文件里

修改配置文件的时候记得过滤注释和空行

grep -Ev '^ *#|^$' /app/http24/conf/httpd.conf

配置文件可以写到主配置里,也可以用Include包含一个文件进去xxx.conf,应用服务器尽量不要改主配置文件

优势:以后想恢复的话,删掉这个配置文件即可
所以专门建一个文件夹conf.d
但是Include用的是相对的路径
因为ServerRoot “/app/httpd24” 它认为整个服务的文件都是在这个目录下的
在主配置文件最下面添加 include conf.d/*.conf 指令大小写不敏感
之后就可以把自己写的配置放进去
vim /app/httpd24/conf.d/test.conf

3.1指定服务器名

http -t
vim /etc/httpd/conf/httpd.conf
# ServerName www.example.com:80
severname www.wyjn.com

http -t

3.2显示服务器版本信息

serverTokens Major|Minor|Min[imal]|prod
# 默认值full
# 为安全考虑用prod

默认情况下,用户通过浏览器访问会暴露你的软件版本,这样是很不安全的,了解版本后可能会针对版本漏洞攻击

范例:

curl -I 域名/IP 抓头
# curl是个文本浏览器,相当于把网站源码抓下来了
[root@localhost ~]# curl -I http://10.0.0.52
HTTP/1.1 200 OK
Date: Tue, 24 Mar 2020 02:42:18 GMT
Server: Apache/2.4.41 (Unix)
Last-Modified: Tue, 24 Mar 2020 01:08:33 GMT
ETag: "2f-5a18f6401598a"
Accept-Ranges: bytes
Content-Length: 47
Content-Type: text/html

[root@localhost ~]# curl -I www.taobao.com
HTTP/1.1 301 Moved Permanently
Server: Tengine(淘宝做的nginx二次研发版)
Date: Tue, 24 Mar 2020 02:53:38 GMT
Content-Type: text/html
Content-Length: 278
Connection: keep-alive
Location: https://www.taobao.com/
Via: cache5.cn2587[,0]
Timing-Allow-Origin: *
EagleId: 6fa4108715850184186018305e

[root@localhost ~]# curl -I www.jd.com
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 24 Mar 2020 02:54:55 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: https://www.jd.com/
Access-Control-Allow-Origin: *
Timing-Allow-Origin: *
X-Trace: 302-1585018495196-0-0-0-0-0
Strict-Transport-Security: max-age=360

# 具体的也看组织,有的组织对安全性要求不高
# 北京市政府的连版本号都给去掉了,更安全
[root@localhost ~]# curl -I www.beijing.gov.cn
HTTP/1.1 200 OK
Date: Tue, 24 Mar 2020 02:56:02 GMT
Content-Type: text/html; charset=gb2312
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Tue, 24 Mar 2020 02:52:29 GMT
Cache-Control: max-age=60
X-Via-JSL: 099bae7,-
Set-Cookie: __jsluid_h=c2e94f4436f03d9085ff6e3a93c9c1bb; max-age=31536000; path=/; HttpOnly
X-Cache: bypass

[root@localhost ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 24 Mar 2020 02:56:55 GMT
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
Server: bfe/1.0.8.18  百度自己改的名字,等学nginx的时候想改成啥都可以改
# 只要手上有源码,想怎么改都可以
# 运维不用改源码改配置就可以了

ServerTokens Prod[uctOnly]:Server:Apache
ServerToken Major:Server:Apache/2 版本号
ServerToken Minor:Server:Apache/2.0 小版本号
ServerToken Min[imal]:Server:Apache/2.0.41 子版本号
ServerToken OS:Server:Apache/2.0.41(unix) 系统
ServerToken Full(or not specified):Server:Apache/2.0.41(unix) PHP/4.2.2 MyMod/1.2 显示更全
# 主配置文件没有这个值,查看官方版本说明,查看默认值

建议使用:ServerTokens Prod

vim /app/httpd24/conf.d/test.conf
ServerTokens Prod

[root@localhost conf.d]# systemctl daemon-reload
[root@localhost conf.d]# curl -I http://10.0.0.52
HTTP/1.1 200 OK
Date: Tue, 24 Mar 2020 03:13:03 GMT
Server: Apache
Last-Modified: Tue, 24 Mar 2020 02:43:54 GMT
ETag: "94d-5a190b8fe1ba0"
Accept-Ranges: bytes
Content-Length: 2381
Content-Type: text/html

3.3监听的IP和Port

Listen [IP:]PORT

说明:默认80

​ 1.省略IP表示为本机所有IP

​ 2.Listen指令至少一个,可重复多次

# 增加端口,访问特定ip端口
vim /app/httpd24/conf.d/test.conf
Listen 10.0.0.7:8081
# 这个端口只支持10.0.0.52的8081端口访问
优势:可以监听在多个端口,将来可以一个IP对应一个网站,实现了前面所谓的虚拟
主机,不同的IP端口号,一个物理服务器端可以托管上百个网站
# 但是有个缺点,访问要写端口号,浏览器默认访问http协议的80端口

3.4 持久连接

协议约定持久连接,服务器决定接收模式

Persistent Connection:连接建立,每个资源获取完成后不断开连接,而是继续等待其他请求完成,默认关闭持久连接(一个TCP连接中可以多次进行http请求并且不断开,除非满足两个条件)

断开条件:时间限制:以秒为单位,默认5s,httpd-2.4支持毫秒级// 事件限制:传输请求的次数

# 具体使用根据业务
比如电商网站,同一个链接浏览,不需要再连接了,慢慢离线看就可以了,用短时间连接即可

如果是游戏的,要长连接

缺点:对并发访问量大的服务器,持久连接会使有些请求得不到响应

平衡:使用较短的持久连接事件

持久连接相关指令:

注意:先看主配置的过滤文件有没有,没有说明是默认配置,去官网看默认配置是什么-指令快速索引

注意:修改,不改主配置文件

httpd.apache.org/docs/2.4/zh…

KeepAlive On|off
KeepAliveTimeout 15 # 连接持续15s,可以以ms做单位,默认值5s
MaxKeepAliveRequests 500 # 持久连接最大连接请求数,默认值100  # 
达到100个请求就断开
测试是不是持久连接,用telnet模拟浏览器发请求
telnet IP 端口
telnet 10.0.0.52 80
GET / HTTP/1.1    /表示htdoc下的页面index.html,也可以指定/test.html
host: 1.1.1.1 (随便写)     回车

# 修改持续连接事件长点后,可以持续请求
GET /test.html HTTP/1.1
host: 2.2.2.2

# telnet WEB_SERVER_IP PORT
# GET /URL HTTP/1.1   这是个请求报头
# Host: WEB_SERVER_IP
没有立刻断开,说明启用了持久连接
如果立刻断开,就说明没有启用持久连接

3.5 DSO(Dynamic Shared Object)

Dynamic Shared Object,加载动态模块配置,无需重启即生效

动态模块所在路径:/usr/lib64/httpd/modules/

编译安装的时候动态模块所在modules(实际上已经编译了,只是没有加载)

编译安装后,配置文件会指明加载了哪些模块

[root@localhost httpd24]# grep -Ev "^ *#|^$" conf/httpd.conf

LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

主配置/etc/httpd/conf/httpd/conf文件中指定加载模块配置文件

ServerRoot "/etc/httpd"
Include conf.modules.d/*.conf

配置指定实现模块加载格式:

LoadModule <mod_name> <mod_path>

模块文件路径可以使用i相对路径:相对于ServerRoot(默认/etc/httpd)

范例:查看模块加载的配置文件

http -M 列出已经加载的模块列表(satic 
静态加载的,不管有没有写都是默认加载的/share 动态模块,需要加载,不需要不加载)

在主配值文件中注释掉不想加载的那个文件即可实现模块不加载(share)
#LoadModule auth_basic_module modules/mod_auth_basic.so
systemctl restart httpd (注意:如果用apachectl 
启动的服务,systemctl是管不了的,除非先用apachectl stop)
httpd -M 就没有basic_moudule
但是平时这个模块是要用的,用于身份验证的
或者可以用systemctl reload httpd

3.6 MPM(multi-processing Module)多路处理模块

httpd支持三种MPM工作模式:prefork(基于进程),worker(多线程),event(每个子进程有一个监听线程)

切换使用MPM:

httpd -M可以看到加载了prefork模块
三种模式只能三选一,注释掉prefork模块,启用event模块
LoadModule mpm_event_module modules/mod_mpm_event.so
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
[root@localhost httpd24]# systemctl restart httpd24.service
httpd -M  看一下模块有没有加载
mpm_event_module (shared)

pstree -p
  ├─httpd───3*[httpd───26*[{httpd}]]

注意:event模型对高并发有用,如果访问量大的话,可以用event模块,但是event模
块可能和其他模块不兼容,因为比较新,prefork模块最悠久


注意:如果是yum安装
有一个专门指定加载模块的路径
ServerRoot "/etc/httpd"  以这个为参考点
Include conf.modules.d/*.conf
centos8用的是event模型

3.6.1 prefork模式相关配置:

# 默认开启5个进程,假如网站上线了,瞬间几千个,短短时间就要开几千个进程,
系统会崩,那就提前准备
StartServers	2000  启动时开启2000个进程
MinSpareServers	2000  最小空闲
# 机器一启动开了2000个,但是没人访问,保留2000个
MaxSpareServers	3000  最大空闲
# 最多允许3000个空闲
# 这时候来了10000个人,开了10000个进程,然后高峰期过了,慢慢关了7000个

ServerLimit		2560 # 最多进程数,最大值20000/和MaxRequestworkers保持一致
# 如果要支持个更多的连接,可以改成10000,超过10000个进程,后面的就不响应了

MaxRequestworkers	2560 # 最大的并发连接数,默认256/和ServerLimit保持一致
MaxConnectionsPerChild	4000 # 子进程最多能处理的请求数量,在处理

MaxConnectionsPerChild个请求之后,子进程将会被父进程终止,这时候子进程占用
的内存就会被释放(为0时永远不释放)
# 开了2000个进程后,不会回收,会继续为下一个用户提供服务,进程持续太久会出
问题,所以响应4000个请求以后就会销毁

MaxRequestsPerChild 
#从http2.3.9开始被MaxConnectionsPerChild代替-这个不用管了

vim /app/httpd24/conf.d/test.conf

StartServers	100  
MinSpareServers	100 
MaxSpareServers	300
ServerLimit		2560 
MaxRequestworkers	2560 
MaxConnectionsPerChild	4000 

这里意思一下能看到即可
# 注意:这个只适合prefork模式,如果是其他模式会报错
# 一旦错误,会导致httpd服务关闭,要重启

[root@localhost httpd24]# ps aux|grep httpd|wc -l
102
1个主进程,100个子进程,1个grep进程

pstree 
  ├─httpd───100*[httpd]

3.6.2 worker和event模式相关配置-不用进程用线程

StartServers	2  提前开2个进程
MinSpareThreads 25
MaxSpareSTheads 75
ServerLimit		16 # 最多进程数,最大值20000
MaxRequestworkers	150 # 最大的并发连接数,默认256
ThreadsPerChild	25 
# 子进程最多能处理的请求数量,在处理ThreadsPerChild个请求之后,子进程将会
被父进程终止,这时候子进程占用的内存就会被释放(为0时永远不释放)

3.7 定义Main server的文档页面路径-默认主页面路径

# 默认yum安装在/var/www/html下
# 编译安装在htdoc下
DocumentRoot "/path"
# 2.4版本有个要求光改这一项会报提示错误403fobbiden
# 一定要做好原数据备份,也就是先复制再重新写值
<directory /path>
	Require all granted 
	# 这个是授权指令,允许任何人访问
</directory>

说明:

  • DocumentRoot指向的路径为URL路径的起始位置
  • /path必须显式授权后才可以访问

范例:

DocumentRoot "/data/html"
# 访问网站,所有数据都是以这个目录为参考点
<directory /data/html>
	Require all granted 
</directory>
[root@localhost htdocs]# mkdir -p /data/html
[root@localhost htdocs]# mv * /data/html/
systemctl restart http24.service
访问http://10.0.0.52/test
对应/data/html/test
# 相当于定义了整个网站的根

3.8 定义站点主页面

DirectoryIndex index.php index.html test.html
# 访问的时候默认找什么文件作为主页
# 就是找不着index.php 文件找文件,找不到index.html这个文件找test.html也行
# 会顺序找,多加几个都可以的,即使不写文件路径也可以找到
注意:修改文件一定要备份
cp  xxx{,.bak}  xx 
xxx.bak  xx

log

2020-3.25 12:14