跳转至

2. 安装

1. 环境

# centos7

2. yum安装

# centos7
yum install httpd -y

# ubuntu20.04
apt install apache2 -y

# 检查进程
ps -ef|grep httpd|grep -v 'grep'

3. 查看配置文件

# centos7查看安装软件配置文件方法
rpm -ql httpd |grep ".conf"

# 查看配置文件
cd /etc/httpd/conf
# 去掉注释和空行
cat httpd.conf |grep -Ev '#|^$'
[root@151 conf]# cat httpd.conf.bak |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
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

1. 常见参数解析

ServerRoot "/etc/httpd"     # 定义服务工作目录

Listen 80                   # 监听端口

Include conf.modules.d/*.conf

User apache                 # 运行服务的用户信息
Group apache                # 运行服务的用户组信息

ServerAdmin root@localhost  # 管理员邮箱地址

DocumentRoot "/var/www/html"  # 定义网站根目录
<Directory "/var/www">         #定义该目录的属性
# 可以写如下内容
  AllowOverride None        # 不允许其他配置文件覆盖现有配置
  Require all granted       # 允许所有请求
</Directory>

ErrorLog "logs/error_log"   # 错误日志路径
LogLevel warn               # 日志级别

AddDefaultCharset UTF-8

EnableSendfile on

IncludeOptional conf.d/*.conf

4. apache工作模式

1. 分类

prefork

worker

event

2. 查看工作模式

# 默认工作模式是prefork
httpd -V |grep -i 'server mpm'

3. prefork

prefork是在apache,httpd服务刚启动的时候,就预先fork一些进程,并且这些进程等待用户的请求。
这么做是为了减少频繁的创建和销毁进程而带来的额外系统开销

prefork工作模式是一个进程只有一个线程。同一时间内只能处理一个请求

优点:成熟稳定,兼容所有apache模块,并且不用担心线程冲突问题
缺点:多个进程占用较多系统资源,并且该模式不适合并发场景

4. worker

worker是使用多进程+多线程的混合模式,也是预先打开多个进程(数量少点),每个子进程会创建子进程,
同时还会有一个监听线程,每个请求来了之后会分配给一个线程去处理。

线程比起进程,消耗内存较少。适合高并发场景

优点:worker工作模式占用更少的内存
缺点:需要考虑多线程的资源抢夺问题

5. event

event和worker工作模式比较相似,解决了TCP的keepalived长连接问题。
当请求来了之后,交给一个子线程去处理,如果该TCP长连接长时间占用线程资源,对系统开销比较大,
event模式会默认开启一个监听线程,当检测到子线程处理完请求后,会释放该线程,减轻服务器压力

最后更新: 2022-02-18 00:53:30