目前世界主流使用的web服务器软件
- apache
- 跨平台、Windows、Linux
- 不同的机器平台运行
- nginx
- lis微软
- Lighttpd
HTTP服务器,apache高度模块化的web服务器
apache支持多虚拟主机功能
- 基于端口的多虚拟主机
- 基于域名的多虚拟主机
- 基于IP的多虚拟主机
Linux安装apache
linux安装软件的方式
- 去官网获取软件源代码,自由选择版本,获取代码后,进行源代码编译安装,扩展额外的功能,自定义安装路径
- rpm包手动安装,不好用,需要自行解决软件依赖
- 配置yum源,使用yum自动化安装,解决依赖关系
1.使用yum源进行安装
yum install -y httpd
2.查看本机80号端口是否被占用
netstat -tunlp | grep 80
3.启动apache并设置开启自启
systemctl start httpd && systemctl enable httpd
4.检查解析apache是否启动成功
systemctl status httpd
curl 127.0.0.1
apache配置文件解析
1.检查apache安装了哪些文件,通过yum安装的软件,如何检查安装的信息呢
rpm -ql httpd | grep '.conf'
2.apache主配置目录:/etc/httpd/
主配置文件绝对路径:/etc/httpd/conf/httpd.conf
3.查看主配置文件的有益信息
[root@localhost ~]# grep -Ev "^[#]|^$" /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" httpd主配置目录定义
Listen 80 定义httpd运行的端口
Include conf.modules.d/*.conf 通过include语法,把其他目录中的配置文件包含进来,实现配置文件简洁化
User apache 定义httpd运行的用户名
Group apache 定义httpd运行的用户组
ServerAdmin root@localhost 定义主机名
#当用户访问网站的根目录,返回某些资源:http://192.168.100.200/:80
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html" 定义httpd的网页根目录
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLink
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
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
apache常用功能
修改apache网页根目录,需要修改两个参数
1.修改httpd.conf
DocumentRoot 代表网页根目录
Directory 定义该目录的属性
修改如下参数
DocumentRoot "/www/html"
<Directory "/www">
AllowOverride None 不允许其他配置文件覆盖现有配置
# Allow open access:
Require all granted 允许所有请求
</Directory>
2.创建新的站点目录
mkdir -p /www/html
3.创建新的首页文件
[root@localhost ~]# cat /www/html/index.html
hello
4.关闭防火墙和selinux
iptables -F
systemctl stop fireWalld
setenforce 0
5.重新启动httpd服务
systemctl restart httpd
6.浏览器输入http://192.168.100.200:80测试是否输出hello
修改apache默认运行端口
1.修改主配置文件
#Listen 12.34.56.78:80
Listen 85
2.重新启动服务
systemctl restart httpd
3.浏览器测试:http://192.168.100.200:85是否可以成功访问
apache的工作模式
- prefork
- 是在apache,httpd服务刚启动的时候,就预先fork(打开)一些进程,并且等待用户的请求,这么做是为了减少频繁的创建和销毁进程,带来的额外服务器开销
- 该prefork模式是一个进程,只有一个线程;一个时间内,只能处理一个请求
- 优点:成熟稳定,兼容所有apache模块,并且不用担心线程冲突的问题
- 缺点:多个进程占用了较多的系统资源,消耗了较多的内存;并且该模式,不适合并发场景
- 相当于:银行的窗口,等待客户来办理业务(进程),每个窗口只有一个业务员(线程)只能办理一个客户的问题
- worker
- 使用多进程+多线程的混合模式,也是预先打开了多个进程(数量少一些),每个子进程都会创建一些子线程,同时还会有一个监听线程,每个请求来了之后,会分配给一个线程去处理
- 线程单位比起进程单位,消耗较少的内存,在高并发场景下比prefork高效
- 优点:worker模式,占用更少的内存
- 缺点:需要考虑多线程的资源抢夺问题
- event
- 和worker模式比较相似,解决了TCP的keep-alived长连接的问题,当请求来了之后,交给一个子线程去处理;如果该TCP长连接,长时间占用线程资源,对系统开销较大,event模式默认开启一个监听线程,检测到当子线程处理完毕请求之后,会释放该线程,减轻服务器压力
检查默认的工作模式,默认是prefork
[root@localhost ~]# httpd -V | grep -i 'server mpm'
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Server MPM: prefork
userdir功能
userdir模块可以很方便的和其他人共享服务器的目录资料
该功能需要修改此配置文件:/etc/httpd/conf.d/userdir.conf
通过userdir模块,共享目录资料,配置步骤如下
1.编辑userdir配置文件,修改如下参数
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
#UserDir disabled 注释该行,表示打开userdir功能
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
UserDir public_html 取消该注释,指定共享哪个目录
</IfModule>
<Directory "/home/*/public_html">
AllowOverride all 允许所有其他配置文件都能够修改其配置
添加一些认证的配置,使得用户可以用账号,密码访问该目录
authuserfile "/etc/httpd/passwd"
authname "input your account"
authtype basic
require user qiu
#AllowOverride FileInfo AuthConfig Limit Indexes
#Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
#Require method GET POST OPTIONS
</Directory>
3.创建网站的资料,注意权限的问题
[root@localhost ~]# su - qhj
[qhj@localhost ~]$ mkdir public_html 创建共享文件夹
[qhj@localhost ~]$ echo hello test >> public_html/index.html 创建共享网页文件
[qhj@localhost ~]$ chmod -Rf 755 public_html/ 修改权限
4.创建apache的用户认证文件,注意要使用root用户执行
[root@localhost ~]# htpasswd -c /etc/httpd/passwd qiu
New password:
Re-type new password:
Adding password for user qiu
5.重新启动apache服务
systemctl restart httpd
6.使用浏览器访问是否配置完成
http://192.168.100.200/~qhj
注:应该把home下面的用户家目录修改成711权限(默认700),否则浏览器访问无法进行认证
[root@localhost ~]# chmod 711 /home/qhj/
[root@localhost ~]# ll /home/
total 4
drwx--x--x. 3 qhj qhj 4096 May 4 12:54 qhj
基于IP的多虚拟主机
在一台服务器上,绑定多个IP或多个网卡,每个IP地址部署一个网站,当用户请求不同的IP地址时,apache根据用户的请求信息,来响应不同的网站内容
1.给Linux服务器绑定多个IP
[root@localhost ~]# ip a | grep "inet 192"
inet 192.168.100.100/24 brd 192.168.100.255 scope global eno16777736
#给服务器添加2个IP地址
[root@localhost ~]# ip address add 192.168.100.101/24 dev eno16777736
[root@localhost ~]# ip address add 192.168.100.102/24 dev eno16777736
#检查服务器IP地址是否添加成功
[root@localhost ~]# ip a | grep "inet 192"
inet 192.168.100.100/24 brd 192.168.100.255 scope global eno16777736
inet 192.168.100.101/24 scope global secondary eno16777736
inet 192.168.100.102/24 scope global secondary eno16777736
在Linux服务器上,添加多个站点资料
192.168.100.100 /www/test01
192.168.100.101 /www/test02
192.168.100.102 /www/test03
#创建3个站点的目录
[root@localhost ~]# mkdir -p /www/{test01,test02,test03}
[root@localhost ~]# ls /www/
test01 test02 test03
#创建3个站点的网页资料
[root@localhost ~]# echo "hello is test01" >> /www/test01/index.html
[root@localhost ~]# echo "hello is test02" >> /www/test02/index.html
[root@localhost ~]# echo "hello is test03" >> /www/test03/index.html
#针对三个不同的IP地址,做一个域名的解析关系,在hosts文件中,添加如下对应解析关系(注意是在客户端下添加)
192.168.100.100 www.test01.com
192.168.100.101 www.test02.com
192.168.100.102 www.test03.com
配置apache的主配置文件,定义多个虚拟主机
#192.168.100.100的虚拟主机
<VirtualHost 192.168.100.100>
DocumentRoot /www/test01
ServerName www.test01.com
<Directory /www/test01 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#192.168.100.101的虚拟主机
<VirtualHost 192.168.100.101>
DocumentRoot /www/test02
ServerName www.test02.com
<Directory /www/test02 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#192.168.100.102的虚拟主机
<VirtualHost 192.168.100.102>
DocumentRoot /www/test03
ServerName www.test03.com
<Directory /www/test03 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
重新启动apache服务并测试是否成功
重新启动服务
systemctl restart httpd
测试是否成功
[root@localhost ~]# curl www.test01.com
hello is test01
[root@localhost ~]# curl www.test02.com
hello is test02
[root@localhost ~]# curl www.test03.com
hello is test03
基于多域名的多虚拟主机
在服务器上,配置一个IP绑定多个域名,每个域名对应一个网站;当用户请求不同的I域名时,apache根据用户的请求信息,来响应不同的网站内容
配置单IP绑定多个域名,在hosts文件中,添加对应的域名解析关系(客户端)
192.168.100.100 www.test04.com www.test05.com
创建站点目录
mkdir /www/{test04,test05}
创建站点网页文件
[root@localhost ~]# echo "hello is test04" >> /www/test04/index.html
[root@localhost ~]# echo "hello is test05" >> /www/test05/index.html
配置apache的主配置文件,定义多个虚拟主机或在其包含的目录中创建*.conf文件添加以下内容
#www.test04.com的虚拟主机
<VirtualHost 192.168.100.100>
DocumentRoot /www/test04
ServerName www.test04.com
<Directory /www/test04 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#www.test05.com的虚拟主机
<VirtualHost 192.168.100.100>
DocumentRoot /www/test05
ServerName www.test05.com
<Directory /www/test05 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
重新启动服务并测试是否成功
重启服务
systemctl restart httpd
测试
[root@localhost ~]# curl www.test04.com
hello is test04
[root@localhost ~]# curl www.test05.com
hello is test05
基于多端口的多虚拟主机
修改主配置文件
#添加监听端口
Listen 8080
Listen 8081
#8080端口的虚拟主机
<VirtualHost 192.168.100.100:8080>
DocumentRoot /www/test06
ServerName www.test06.com
<Directory /www/test06 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#8081端口的虚拟主机
<VirtualHost 192.168.100.100:8081>
DocumentRoot /www/test07
ServerName www.test07.com
<Directory /www/test07 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
创建站点目录
mkdir /www/{test06,test07}
创建站点网页文件
[root@localhost ~]# echo "port 8080 test" >> /www/test06/index.html
[root@localhost ~]# echo "port 8081 test" >> /www/test07/index.html
重新启动服务并测试
重启服务
systemctl restart httpd
测试
[root@localhost ~]# curl 192.168.100.100:8081
port 8081 test
[root@localhost ~]# curl 192.168.100.100:8080
port 8080 test
apache设置站点访问权限
1.直接修改apache配置文件,针对一个虚拟主机去修改访问的权限
<VirtualHost 192.168.100.102>
DocumentRoot /www/test03
ServerName www.test03.com
<Directory /www/test03 >
#允许所有人访问所有权限
#AllowOverride None
#Require all granted
#针对虚拟主机站点,进行访问控制,可以控制单个IP地址,也可以控制整个网段的访问
Order allow,deny 先允许后拒绝
Allow from 192.168.100.0/24 (2.4版本之前)
Require ip 192.168.178.0/24 (2.4版本之后的写法)
</Directory>
</VirtualHost>
定义访客日志格式
ErrorLog "logs/error_log" 错误日志存放位置
#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn 日志级别
<IfModule log_config_module> 定义日志格式模块
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
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>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
apache状态监测功能
1.修改配置文件
vim /etc/httpd/conf/httpd.conf
2.添加如下参数
#设置状态页功能,用户访问192.168.100.100/server-status
<Location /server-status>
SetHandler server-status
<RequireAll>
Require ip 192.168.100.0/24
</RequireAll>
</Location>
使用apache自带的压测命令,给服务器发送大量的请求
1.使用ab命令
yum install -y httpd-tools
2.使用ab命令,给服务器并发的发送大量请求
#给服务器发送100000个请求,100个并发数
ab -c 100 -n 100000 http://192.168.100.100