Apache网站
多线程优缺点
共享资源,减少开销
公用进程,遇突发则全丢失
HTTP协议
- 超文本传输协议
- 基于TCP/IP通信协议来传递数据
- 工作于C/S或B/S架构
- 状态码:
- 1xx:指示信息——表示请求已经接收,继续处理
- 2xx:成功——表示请求已经被成功接收、理解、接收
- 3xx:重定向——要完成请求必须进行更进一步的操作
- 4xx:客户端错误——请求的语法有错误或请求无法实现
- 5xx:服务端错误——服务器未能实现合法的请求!
- 200:OK请求已经正常处理完毕
- 301:请求永久重定向
- 302:请求临时重定向
- 304:请求被重定向到客户端本地缓存
- 400:客户端请求存在语法错误
- 401:客户端请求没有经过授权
- 403:客户端的请求被服务器拒绝,一般为客户端没有访问权限
- 404:客户端请求的URL在服务端不存在
- 500:服务端永久错误
- 503:服务端发生临时错误
Apache服务
- 互联网最流行的web服务软件
- MPM工作模式
- prefork(默认):多进程I/O模型,一个主进程,管理多个子进程,一个子进程处理一个请求。
- worker:复用的多进程I/O模型,多进程多线程,一个主进程,管理多个子进程,一个子进程 管理多个线程,每个 线程处理一个请求。
- event:事件驱动模型,一个主进程,管理多个子进程,一个进程处理多个请求。
- 文件说明
/etc/httpd/:主配置文件目录/etc/httpd/conf/httpd.conf:服务配置文件/etc/httpd/conf.d/:服务配置目录(模块化)/etc/httpd/conf.modules.d/:模块配置目录/etc/sysconfig/httpd:守护进程配置文件/usr/lib64/httpd/modules/:可用模块/usr/sbin/:相关命令目录/var/log/httpd/:日志目录/var/www/:站点目录
- 主配置文件
主配置说明
- [root@node3 ~]# grep “ [ #]” /etc/httpd/conf/httpd.conf
- ServerRoot “/etc/httpd” # 服务器的根
- Listen 80 # 监听的端口
- Include conf.modules.d/.conf # 包含模块
- User apache # 用户
- Group apache # 属组
- ServerAdmin root@localhost # 服务器管理员
- DocumentRoot “/var/www/html”
- ErrorLog “logs/error_log” # 错误日志
- LogLevel warn # 日志等级
- EnableSendfile on # 开启
- IncludeOptional conf.d/*.conf # 虚拟服务器配置文件
- 说明:<>此类称之为容器,针对某个容器做配置
持久连接
HTTP连接建立在TCP建立上,有效通信两个数据包加7个请求包,
功能: 在不断开TCP连接的基础上继续新的请求。
- 默认参数
- KeepAlive默认是on,默认的超时时间是5秒。
- KeepAliveTimeout # 连接超时
- MaxKeepAliveRequests # 最大保持连接请求
- 测试持久连接
```bash [root@server ~]# yum install telnet -y
[root@server ~]# echo ‘this is test!’ > /var/www/html/index.html
[root@server ~]# telnet 127.0.0.1 80
Trying 127.0.0.1… Connected to 127.0.0.1.
Escape character is ‘^]’.
GET / HTTP/1.1
Host:127.0.0.1
HTTP/1.1 200 OK
Date: Wed, 14 Jul 2021 14:17:29 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Wed, 14 Jul 2021 14:16:40 GMT
ETag: “e-5c71600ca9dad”
Accept-Ranges: bytes Content-Length: 14
Content-Type: text/html; charset=UTF-8
this is test!
Connection closed by foreign host.
<a name="8135be8d"></a>## prefork模式**优点:**每个请求相对独立,一个请求不会影响到其他的请求**缺点:**一个进程占用更多系统资源,消耗更多内存,不擅长处理高并发请求。```bash[root@server ~]# httpd -VAH00558: httpd: Could not reliably determine the server's fully qualifieddomain name, using fe80::eaf3:dc40:2bf:6da2. Set the 'ServerName' directiveglobally to suppress this messageServer version: Apache/2.4.6 (CentOS)Server built: Nov 16 2020 16:18:20Server's Module Magic Number: 20120211:24Server loaded: APR 1.4.8, APR-UTIL 1.5.2Compiled using: APR 1.4.8, APR-UTIL 1.5.2Architecture: 64-bitServer MPM: preforkthreaded: noforked: yes (variable process count)
- 切换apache的mpm工作模式
若要使用worker和event工作模型,只需要在/etc/httpd/conf.modules.d/00-mpm.conf中取消 对应注释即可- 修改prefork参数
默认参数:StartServers 5 # 服务启动时的进程数MaxSpareServers 10 # 最大空闲服务进程数MinSpareServers 5 # 最小空闲进程数MaxRequestWorkers 256 # 单个进程最多接受的进程数[root@server ~]# vim /etc/httpd/conf.d/mpm.confStartServers 10MaxSpareServers 15MinSpareServers 10MaxRequestWorkers 256MaxRequestsPerChild 4000[root@localhost ~]# systemctl restart httpd[root@server ~]# ps -ef | grep httpd
访问控制机制
基于IP地址访问控制
- 定义服务器文档页面路径
```bash # 定义服务器的文档的页面路径:
[root@server1 conf]# vim httpd.conf …… DocumentRoot “/data/www/html” ……
# 开放对应目录权限
- 黑名单```bash<RequireAll>Require all grantedRequire not ip 172.16.1.1 #拒绝特定IP</RequireAll>
白名单
<RequireAny>Require all deniedrequire ip 172.16.1.1 #允许特定IP</RequireAny>
网段
<requireany>require all deniedRequire ip 192.168.39.0/24</requireany>
主机
<Requireany>Require all deniedRequire ip 192.168.32.7 #只允许特定的主机访问</Requireany>
用户访问控制
认证方式有basic和digest两种
- 创建用户认证文件
[root@server1 ~]# htpasswd -c -m /etc/httpd/conf.d/.htpassword lisiNew password:Re-type new password:Adding password for user lisi[root@server1 ~]# htpasswd -b -m /etc/httpd/conf.d/.htpassword zhangsanzhangsanAdding password for user zhangsan
- 修改配置文件,启用用户认证
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf<Directory "/data/www/html">AuthType BasicAuthName "Restricted Resource"AuthBasicProvider fileAuthUserFile /etc/httpd/conf.d/.htpasswordRequire user lisi</Directory>[root@server1 ~]# systemctl restart httpd.service
日志设定
ErrorLog "logs/error_log"LogLevel warn<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""combinedLogFormat "%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>
- 日志参数
参数:%h Remote hostname%l Remote logname%u Remote user%t Time the request was received, in the format [18/Sep/2011:19:18:28 -0400]%r First line of request%s Status%b Size of response in bytes, excluding HTTP headersReferer 有利于分析用户是通过哪个网站转发的如通过baidu转发的,也可以监控网站盗链的发生。User-Agent 记录浏览器的类型。防止爬虫一定程度上,爬虫可以伪造浏览器类型。curl -A "evan"http://I(伪造名字叫evan的浏览器)
虚拟主机
基于IP地址虚拟主机
[root@node3 data]# cat /etc/httpd/conf.d/site.conf<Directory "/data/">Require all granted</Directory><VirtualHost 192.168.0.140:80>Servername www.site1.comDocumentRoot "/data/site1/"</VirtualHost><VirtualHost 192.168.0.145:80>Servername www.site2.comDocumentRoot "/data/site2/"</VirtualHost>[root@node1 ~]# curl 192.168.0.142<h1>This is site1</h1>[root@node1 ~]# curl 192.168.0.145<h1>This is site2</h1>
基于端口虚拟主机
[root@server1 ~]# cat /etc/httpd/conf.d/site.confListen 8080Listen 9090<Directory "/data/">Require all granted</Directory><VirtualHost *:8080>DocumentRoot "/data/site3/"</VirtualHost><VirtualHost *:9090>DocumentRoot "/data/site4/"</VirtualHost>[root@server1 ~]# curl 192.168.80.100:8080<h1>This is site3</h1>[root@server1 ~]# curl 192.168.80.100:9090<h1>This is site4</h1>
基于FQDN虚拟主机
[root@server1 ~]# cat /etc/httpd/conf.d/site.confListen 10101<Directory "/data/">Require all granted</Directory><VirtualHost 192.168.80.100:10101>Servername www.site5.comDocumentRoot "/data/site5/"</VirtualHost><VirtualHost 192.168.80.100:10101>Servername www.site6.comDocumentRoot "/data/site6/"</VirtualHost>~[root@server1 ~]# cat /etc/hosts192.168.0.142 www.site5.com192.168.0.142 www.site6.com[root@server1 ~]# curl www.site5.com:10101<h1>This is site5</h1>[root@server1 ~]# curl www.site6.com:10101<h1>This is site6</h1>
SSL配置
- 安装mod_ssl和openssl
yum install mod_ssl openssl -y
- 生成2048位的加密私钥server.key
openssl genrsa -out server.key 2048
- 生成证书签名请求server.csr
openssl req -new -key server.key -out server.csr
- 生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年server.crt
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
- 复制到对应位置
[root@node1 ~]# cp server.crt /etc/pki/tls/certs/
[root@node1 ~]# cp server.key /etc/pki/tls/private/
[root@node1 ~]# cp server.csr /etc/pki/tls/private/
- 修改配置文件
Servername 192.168.0.140:443
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key
- 防火墙放行
firewall-cmd --add-port=443/tcp --per
进程&线程
进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基 本单位,是操作系统结构的基础。进程是线程的容器。进程是正在运行的程序的实例
线程(Threads)能独立运行的基本单位,进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程
去除配置文件的注释
grep -v '#' httpd.conf.bak | grep -Ev '^$' > httpd.conf
Selinux
SELinux的全称是Security Enhanced Linux, 就是安全加强的Linux。
setenforce 0|1 ## 0表示警告模式1表示强制模式## 状态类型:disabled ## 关闭enforcing ## 强制 [不只警告,还会限制]permissive ## 警告 [只会警告,不会限制]
更改主机IP
/etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"PROXY_METHOD="none"BROWSER_ONLY="no"BOOTPROTO="static"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"IPV6INIT="yes"IPV6_AUTOCONF="yes"IPV6_DEFROUTE="yes"IPV6_FAILURE_FATAL="no"IPV6_ADDR_GEN_MODE="stable-privacy"NAME="ens33"UUID="355e31ea-f2d3-4599-837e-755d9551b9af"DEVICE="ens33"ONBOOT="yes"IPADDR0=192.168.174.10NETMASK0=255.255.255.0GATEWAY0=192.168.174.2IPADDR1=192.168.174.20NETMASK1=255.255.255.0GATEWAY1=192.168.174.2DNS1=114.114.114.1
