web服务器
服务器程序:
linux:httpd apache ,nginx ,lighttpd
window:IIS
httpd介绍
- 特性:
1.高度模块化:core+Modules
2.DSO:动态加载/卸载模块
3.MPM:多路处理模块,指定httpd工作模式
4.虚拟主机:ip,port,FQDN
5.CGI:通用网关接口
6.反向代理
7.负载均衡
8.路径别名
9.双向认证
10.支持第三方模块
MPM工作模式:
1.prefork:多进程IO模型,一个主进程,管理多个子进程,每个子进程处理一个请求。
2.worker:复用的多进程IO模型,多进程,多线程,一个主进程,管理多个子进程,每个子进程管理多个线程,每个线程处理一个请求。
3.event:事件驱动模型,复用IO模型,一个主进程管理多个子进程,一个子进程处理多个请求。
服务部署
- httpd下载
centos:yum install httpd -y
Ubuntu:apt-get install apache2
- 服务开启:systemctl start httpd
- 查看默认80端口的监听状态:
[root@localhost ~]# ss -tanl | grep 80
LISTEN 0 128 :::80 :::*
文件说明
[root@localhost ~]# rpm -ql httpd #查看相关文件/etc/httpd #主配置文件/etc/httpd/conf.d #服务配置目录(模块化)/etc/httpd/conf.modules.d #模块配置目录/etc/sysconfig/httpd #守护进程配置文件/usr/lib64/httpd/modules # 可用模块/usr/sbin/httpd #相关命令目录/var/log/httpd #日志目录/var/www #站点目录
默认界面
/etc/httpd/conf.d/welcome.conf 文件定义了访问找不到主页面时显示的默认界面
[root@localhost ~]# cat /etc/httpd/conf.d/welcome.conf |grep -Ev "^#|^$"
<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>
<Directory /usr/share/httpd/noindex>
AllowOverride None
Require all granted
</Directory>
Alias /.noindex.html /usr/share/httpd/noindex/index.html
Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
主页面设置:
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^Document"
DocumentRoot "/var/www/html" #主页面的路径
[root@localhost ~]# echo "hello ,lhuan" > /var/www/html/index.html 在文件中添加一句话
[root@localhost ~]# curl localhost # curl命令是一个利用URL规则在命令行下工作的文件传输工具
hello ,lhuan
#主配置说明
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep -Ev "^$|^#|#"
ServerRoot "/etc/httpd" #服务器的根
Listen 80 #监听的端口
Include conf.modules.d/*.conf #包含模块
User apache #用户
Group apache #属组
ServerAdmin root@localhost #服务管理员
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html" #网站根目录
<Directory "/var/www">
AllowOverride None
Require all granted #允许所有人访问
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log" #错误日志
LogLevel warn #日志等级
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8 #默认编码utf-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on #开启
IncludeOptional conf.d/*.conf #虚拟服务器配置文件
ps:<></>此类称之为容器,针对某个容器做配置
- 已经加载到主页面中的模块
[root@localhost ~]# ll /etc/httpd/conf.modules.d/
total 28
-rw-r--r-- 1 root root 3739 Jul 29 11:15 00-base.conf
-rw-r--r-- 1 root root 139 Jul 29 11:15 00-dav.conf
-rw-r--r-- 1 root root 41 Jul 29 11:15 00-lua.conf
-rw-r--r-- 1 root root 742 Jul 29 11:15 00-mpm.conf
-rw-r--r-- 1 root root 957 Jul 29 11:15 00-proxy.conf
-rw-r--r-- 1 root root 88 Jul 29 11:15 00-systemd.conf
-rw-r--r-- 1 root root 451 Jul 29 11:15 01-cgi.conf
基础配置实验
多端口支持
前提:关闭防火墙,关闭selinux
[root@localhost ~]# echo "Listen 8080" > /etc/httpd/conf.d
-bash: /etc/httpd/conf.d: Is a directory
[root@localhost ~]# echo "Listen 8080" > /etc/httpd/conf.d/Listen.conf
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# ss -tanl | grep 80 #查看监听的端口
LISTEN 0 128 :::8080 :::*
LISTEN 0 128 :::80 :::*
[root@localhost ~]# curl -I 192.168.230.131 #curl -I :只显示请求头信息
HTTP/1.1 200 OK
Date: Mon, 19 Aug 2019 12:29:48 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 19 Aug 2019 12:12:10 GMT
ETag: "d-590773fd5f12a"
Accept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8
[root@localhost ~]# curl -I 192.168.230.131:8080
HTTP/1.1 200 OK
Date: Mon, 19 Aug 2019 12:29:56 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 19 Aug 2019 12:12:10 GMT
ETag: "d-590773fd5f12a"
Accept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8
设置持久连接
- 用处:主要避免多次tcp三次握手带来的资源
- 关键字:
- http协议:keep alive
第一次建立http连接之后,开始计时,
httpd:
- KeepAlive on | off:默认是on,默认5s
- KeepAliveTimeout 30 :超时时间30s
- MaxKeepAliveRequests 100:最大保持连接请求100次
设置为1:
设置为0:意味着
[root@localhost ~]# echo "KeepAlive on" >> /etc/httpd/conf.d/listen.conf
[root@localhost ~]# echo " KeepAliveTimeout 10" >>/etc/httpd/conf.d/listen.conf
[root@localhost ~]# echo " MaxKeepAliveRequests >>/etc/httpd/conf.d/listen.conf
[root@localhost ~]# yum install telnet -y
[root@localhost ~]# telnet 192.168.230.131 8080
Trying 192.168.230.131...
Connected to 192.168.230.131.
Escape character is '^]'.
GET /index.html
hello ,lhuan
MPM多路径处理模块
默认的工作模式:prefork
如果需要使用其他工作模式,需要加载相应的模块,
[root@localhost ~]# cat /etc/httpd/conf.modules.d/00-mpm.conf | grep -Ev "^$|^#"
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
prefork默认参数:
StartServers 5:服务启动时的进程数
MaxSpareServers 10 :最大的默认空闲进程数
MinSpareServers 5 :最小的默认空闲进程数
MaxRequestWorkers 256 :每个进程同时处理的并发连接数
# 查看
[root@localhost ~]# apachectl -l #
Compiled in modules:
core.c
mod_so.c
http_core.c
[root@localhost ~]# apachectl -t -D DUMP_MODULES #查看加载模块
[root@localhost ~]# ps aux | grep httpd
root 1915 0.0 0.1 224060 5016 ? Ss 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1916 0.0 0.0 224060 2964 ? S 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1917 0.0 0.0 224060 2964 ? S 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1918 0.0 0.0 224060 2964 ? S 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1919 0.0 0.0 224060 2964 ? S 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1920 0.0 0.0 224060 2964 ? S 09:22 0:00 /usr/sbin/httpd -DFOREGROUND
root 2363 0.0 0.0 112708 988 pts/0 S+ 10:03 0:00 grep --color=auto httpd
# ab工具测压:
[root@localhost ~]# ab -c 100 -n 1000 http://192.168.230.132/index.html
worker工作模型
http://httpd.apache.org/docs/2.4/
event工作模型
访问控制机制:
- 定义服务器的文档页面路径:
DocumentRoot “/data/www/html”
修改/etc/httpd/conf/httpd.conf
/var/www/html ——>/data/www/html
/data/ww/html/index/html 默认主页文件
重启服务:systemctl restart httpd
curl -I localhost 报错403
解决:
Require all granted
- require 常见配置参数:
Require all granted:允许所有
Require all denied:拒绝所有
Require method http-method [http-method]:只允许指定的请求方法 Require GET POST
Require ip 10 172.20 192.168.2 :指定允许访问的ip地址段
Require user user1
Require group grou1
require 相关参数最优是放在
- Options配置参数:
Options Indexes FollowSymLinks
Indexes:如果不存在index.html是否显示索引
FollowSymLinks:允许软链接访问
AllowOverride None
用户访问控制
[root@localhost ~]# htpasswd -c -m /etc/httpd/conf.d/.htpassword lh #添加用户
AuthType Basic
AuthName “ Resource index”
AuthBasicProvider file
AuthUserfile /etc/httpd/conf.d/.htpassword #用户认证文件:用户名和密码(md5加密的)
Require user lh
AllowOverride None
日志的设定
Error “logs/error_log” #定义错误日志文件
LogLevel warn:#定义日志文件等级
- 日志等级:
- info
- warn
- error
- debug
%h :显示远程主机名
%l:显示远程日志名称
%u:显示用户
%t:显示登录时间
%r:请求行
%s:状态码
%b:响应的字节数
%f:指明文件名称
%{Referer}:”-“有利于分析用户是通过哪个网站转发过来的,可以监控网站防盗链的发送
%k:
%{Use-Agent}:记录浏览器类型””
可以防爬:爬虫可以伪造浏览器信息类型
%D:记录服务器的响应时间,单位微秒
