apr这个软件是apache依赖的,要编译安装http,必需要保证apr版本和http版本配合php
由于centos8上的http版本很新2.4,就在centos7上编译安装,不能用yum的apr版本,由于过久,不支持httpd2.4.41的安装html
yum info httpd
Name : httpd
Arch : x86_64
Version : 2.4.6
yum info apr
Name : apr
Arch : x86_64
Version : 1.4.8
版本有点偏旧
复制代码
进入官网apr.apache.orglinux
下载apr和apr-util,bz2压缩比更高nginx
准备apache
wget mirrors.tuna.tsinghua.edu.cn/apache//apr…vim
wget mirrors.tuna.tsinghua.edu.cn/apache//apr…centos
wget mirrors.tuna.tsinghua.edu.cn/apache//htt…浏览器
解包:tar jxvf安全
1.编译安装APR-基本的可移植库bash
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
复制代码
准备
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
复制代码
http编译过程:/app/httpd24/build/config.nice
自带的服务控制脚本:/app/httpd24/bin/apchectl
useradd -s /sbin/nologin -r apache
# 自动建立用户和组
复制代码
vim /app/httpd24/conf/httpd
User apache
Group apache
# 用来起worker进程
# 若是以前有yum安装过httpd,apache帐号会自动建立
复制代码
vim /etc/profile.d/httpd24.sh
PATH=/app/httpd24/bin:$PATH
. /etc/profile.d/httpd24.sh 让脚本生效
# 解决调用可执行脚本的全路径调用的问题
复制代码
vim /etc/man_db.conf
MANDATORY_MANPATH /app/http24/man
复制代码
vim /etc/rc.d/rc.local
/app/httpd24/bin/apachectl start
chmod +x /etc/rc.d/rc.local
# 开机启动还有service文件和脚本
复制代码
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
复制代码
# 自定义启动脚本(参考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
复制代码
修改配置文件的时候记得过滤注释和空行
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
复制代码
http -t
vim /etc/httpd/conf/httpd.conf
# ServerName www.example.com:80
severname www.wyjn.com
http -t
复制代码
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
复制代码
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端口
复制代码
协议约定持久链接,服务器决定接收模式
Persistent Connection:链接创建,每一个资源获取完成后不断开链接,而是继续等待其余请求完成,默认关闭持久链接(一个TCP链接中能够屡次进行http请求而且不断开,除非知足两个条件)
断开条件:时间限制:以秒为单位,默认5s,httpd-2.4支持毫秒级// 事件限制:传输请求的次数
# 具体使用根据业务
好比电商网站,同一个连接浏览,不须要再链接了,慢慢离线看就能够了,用短期链接便可
若是是游戏的,要长链接
复制代码
缺点:对并发访问量大的服务器,持久链接会使有些请求得不到响应
平衡:使用较短的持久链接事件
持久链接相关指令:
注意:先看主配置的过滤文件有没有,没有说明是默认配置,去官网看默认配置是什么-指令快速索引
注意:修改,不改主配置文件
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
没有马上断开,说明启用了持久链接
若是马上断开,就说明没有启用持久链接
复制代码
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
复制代码
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模型
复制代码
# 默认开启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]
复制代码
StartServers 2 提早开2个进程
MinSpareThreads 25
MaxSpareSTheads 75
ServerLimit 16 # 最多进程数,最大值20000
MaxRequestworkers 150 # 最大的并发链接数,默认256
ThreadsPerChild 25
# 子进程最多能处理的请求数量,在处理ThreadsPerChild个请求以后,子进程将会
被父进程终止,这时候子进程占用的内存就会被释放(为0时永远不释放)
复制代码
# 默认yum安装在/var/www/html下
# 编译安装在htdoc下
DocumentRoot "/path"
# 2.4版本有个要求光改这一项会报提示错误403fobbiden
# 必定要作好原数据备份,也就是先复制再从新写值
<directory /path>
Require all granted
# 这个是受权指令,容许任何人访问
</directory>
复制代码
说明:
范例:
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
# 至关于定义了整个网站的根
复制代码
DirectoryIndex index.php index.html test.html
# 访问的时候默认找什么文件做为主页
# 就是找不着index.php 文件找文件,找不到index.html这个文件找test.html也行
# 会顺序找,多加几个均可以的,即便不写文件路径也能够找到
注意:修改文件必定要备份
cp xxx{,.bak} xx
xxx.bak xx
复制代码
2020-3.25 12:14
复制代码