Apache介绍:
1 Apache是目前世界上使用最广泛的一种web server,它以跨平台,高效和稳定而闻名。
2 Apache缺点是变得越来越重,被普遍认为是重量级的web server。(Apache主要采用的是基于进程的Prefork模式(还有基于线程的Worker模式),也就是,对于每个请求会用一个进程去进行服务,进程非常占资源,当并发量大的时候,就需要等额的进程,导致的是高内存占用和CPU占用,这就是所谓的“重量级”。 )
3 Apache是基于模块化设计的,总体上看起来代码的可读性高于php代码,它的核心代码并不多,大多数的功能都被分割到各种模块中,各个模块在系统启动时按需载入。
4 Apache是用C语言写的。
特点:功能强大,高度模块化,采用MPM多路处理模块,配置简单,速度快,应用广泛,性能稳定可靠,可做代理服务器或负载均衡来使用,双向认证,支持第三方模块
MPM工作模式
prefork:多进程I/O模型,一个主进程,管理多个子进程,一个子进程处理一个请求。
worker:复用的多进程I/O模型,多进程多线程,一个主进程,管理多个子进程,一个子进程
管理多个线程,每个 线程处理一个请求。
event:事件驱动模型,一个主进程,管理多个子进程,一个进程处理多个请求。
安装并设置第一个站点
[root@localhost ~]#yum install -y httpd
[root@localhost ~]#echo '<h1>It works!</h1>' > /var/www/html/index.html
[root@localhost ~]#systemctl start httpd
1. 检查防火墙和selinux是否关闭
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#systemctl status firewalld
[root@localhost ~]#setenforce 0
[root@localhost ~]#getenforce
2. 检查端口是否存在
[root@localhost ~]#ss -tanl | grep 80
3. 查看进程是否存在
[root@localhost ~]#ps -ef | grep http
4. 在服务器本地进行测试
[root@localhost ~]#wget <http://IP地址>
[root@localhost ~]#curl <IP地址>
文件说明
/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 # 服务器管理员
<Directory />
AllowOverride none
Require all denied
</Directory> # <Directory>和</Directory>用于封装一组指令,使之仅对某个
目录及其子目录生效。
DocumentRoot "/var/www/html"
ErrorLog "logs/error_log" # 错误日志
LogLevel warn # 日志等级
EnableSendfile on # 开启
IncludeOptional conf.d/*.conf # 虚拟服务器配置文件
说明:<></>此类称之为容器,针对某个容器做配置
apache功能模块
持久连接
持久连接,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成
KeepAlive:是否开启持久连接,on或者off,默认开启
KeepAliveTimeout:持久连接超时时长,默认5秒
KeepAliveRequests:持久连接最大连接数
多路处理模块
MPM工作模式
Prefork(进程):多进程I/O模型,一个主进程,管理多个子进程,一个子进程处理一个请求。
Worker(线程):复用的多进程I/O模型,多进程多线程,一个主进程,管理多个子进程,一个子进程管理多个线程,每个 线程处理一个请求。
event:事件驱动模型,一个主进程,管理多个子进程,一个进程处理多个请求.它和worker模式很像,最大的区别在于,它解决了keep-alive场景下,长期被占用的线程的资源浪费问题(某些线程因为被keepalive,挂载哪里等待,中间基于没有请求过来,一直等到超时)
切换apache的mpm工作模式:/etc/httpd/conf.modules.d/00-mpm.conf
修改prefork参数:vim /etc/httpd/conf.d/mpm.conf
StartServers 5 # 服务启动时的进程数
MaxSpareServers 10 # 最大空闲服务进程数
MinSpareServers 5 # 最小空闲进程数
MaxRequestWorkers 256 # 单个进程最多接受的进程数
修改work参数:
StartServers #服务器启动时建立的子进程数量,在workers模式下默认是3. ServerLimit #系统配置的最大进程数量
MinSpareThreads #空闲子进程的最小数量,默认75
MaxSpareThreads #空闲子进程的最大数量,默认250
ThreadsPerChild #每个子进程产生的线程数量,默认是64 MaxRequestWorkers /MaxClients #限定服务器同一时间内客户端最大接入的请求数量.
MaxConnectionsPerChild #每个子进程在其生命周期内允许最大的请求数量,如果请求总数已经达到这个数值,子进程将会结束, 如果设置为0,子进程将永远不会结束。
访问控制机制
IP地址访问控制
更改站点根目录案例
1.重新定义根目录
# 定义服务器的文档的页面路径:
[root@server1 conf]# vim httpd.conf
DocumentRoot "/data/www/html"
# 准备页面
[root@server1 ~]# echo "this path /data/wwww/html" >
/data/www/html/index.html
# 重启服务
[root@server1 ~]# systemctl restart httpd
2.访问控制机制中开放相应目录权限
[root@server1 conf]# vim httpd.conf
<Directory "/data/www/html">
Require all granted
</Directory>
3.重启服务器
systemctl restart httpd
黑名单方式 白名单方式
<RequireAll> <RequireAny>
Require all granted Require all denied
Require not ip 172.16.1.1 ip 172.16.1.1
#拒绝特定IPrequire #允许特定IP
</RequireAll> </RequireAny>
只允许特定网段访问
<requireany>
require all denied
Require ip 192.168.39.0/24
</requireany>
URI匹配规则(资源访问)
#基于目录
<Directory “/path">
...
</Directory>
#基于文件
<File “/path/file”>
...
</File>
#基于文件通配符
<File “/path/*file*”>
...
</File>
#基于正则表达式
<FileMatch “regex”>
...
</FileMatch>
URL匹配规则()
http://192.168.80.193/dir1 客户端的URL的值是:/dir1
http://192.168.80.193/dir1/ 客户端的URL的值是:/dir1/
http://192.168.80.193/dir1/index.html 客户端的URL的值是:/dir1/index.html
<Location "/dir1">
上述三种情况都会被匹配到,设置的Require all denied都会生效
<Location "/dir1/">
上述后两种情况都会被匹配到,设置的Require all denied都会生效
用户访问控制
1.创建用户认证文件,为用户认证做准备
[root@server1 ~]# htpasswd -c -m /etc/httpd/conf.d/.htpassword lisi
New password:
Re-type new password:
Adding password for user lisi
[root@server1 ~]# htpasswd -b -m /etc/httpd/conf.d/.htpassword zhangsan zhangsan
Adding password for user zhangsan
2.修改配置文件,启用用户认证
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/data/www/html">
AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/httpd/conf.d/.htpassword
#AuthGroupFile /etc/httpd/conf.d/.htgroup
Require user lisi
#Require group group1
</Directory>
[root@server1 ~]# systemctl restart httpd.service
3.测试访问,发现lisi可以成功访问,zhangsan不能访问
Options指令
后跟1个或多个以空白字符分隔的选项列表, 在选项前的+,- 表示增加或删除指定选项
常见选项(默认是全部禁用):
Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户
FollowSymLinks:允许访问符号链接文件所指向的源文件
None:全部禁用
All: 全部允许
AllowOverride指令(保护隐藏访问控制权限)
在主配置文件中禁止 Indexes 和 FollowSymLinks ,但是在 .htaccess 中打开
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/data/html">
Options -Indexes -FollowSymLinks
AllowOverride options=FollowSymLinks,Indexes
[root@localhost ~]# systemctl reload httpd
创建 .htaccess 文件,然后发现主配置文件中的设置被修改了
[root@localhost ~]# echo "Options FollowSymLinks Indexes" >
/data/html/dir/.htaccess
[root@localhost ~]# systemctl reload httpd
因为有主配置文件中设置了 .htaccess 对应的文件拒绝全部访问,所以相对是安全的
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Files ".ht*">
Require all denied
</Files>
虚拟主机
修改配置文件:/etc/httpd/conf.d/site.conf
基于IP地址虚拟主机(不同主机访问的文件夹不同)
<Directory "/data/">
Require all granted
</Directory>
<VirtualHost 192.168.0.145:80>
DocumentRoot "/data/site2/"
</VirtualHost>
基于端口虚拟主机(不同端口访问的文件夹不同)
Listen 8080
Listen 9090
<Directory "/data/">
Require all granted
</Directory>
<VirtualHost *:8080>
DocumentRoot "/data/site3/"
</VirtualHost>
<VirtualHost *:9090>
DocumentRoot "/data/site4/"
</VirtualHost>
基于FQDN(域名)虚拟主机(同一IP同一端口不同域名访问的文件夹不同)
<Directory "/data/">
Require all granted
</Directory>
<VirtualHost 192.168.80.100:10101>
Servername www.site5.com
DocumentRoot "/data/site5/"
</VirtualHost>
<VirtualHost 192.168.80.100:10101>
Servername www.site6.com
DocumentRoot "/data/site6/"
</VirtualHost>
SSL配置认证证书
1.安装mod_ssl和openssl
[root@node1 ~]# yum install mod_ssl openssl -y
2.生成2048位的加密私钥server.key
[root@node1 ~]# openssl genrsa -out server.key 2048
3.生成证书签名请求server.csr
[root@node1 ~]# openssl req -new -key server.key -out server.csr
4.生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年server.crt
[root@node1 ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out
5.复制文件到相应位置
[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/
6.修改配置文件
vim /etc/httpd/conf.d/ssl.conf
Servername 服务端:443
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key