[Linux]经典面试题 - 服务管理 - Apache

一、Apache 概述

1.1 Apache 是什么?

  • Apache是一款跨平台运行的开源HTTP服务器软件。
  • 和很多软件一样,有源码编译安装和二进制数据包安装两种方式,可根据不同的需求进行选择,前者具有可根据企业要求定制的优点,后者具有方便快捷部署的优点。
    • 源码部署的配置文件一般在/usr/local/apache2/conf目录下
    • yum或者dnf包安装的配置文件一般在/etc/httpd/conf/httpd.conf目录下

1.2 Apache 和 Nginx 对比

  • Apache 拥有丰富的模块组件的支持,稳定性强,BUG少,对动态内容处理强。
  • Nginx 更为轻量级,占用资源少,负载均衡,并发性优秀,对静态内容处理高效。

二、Apache 安装部署

2.1 安装 Apache

  • 通过包管理器安装apache
dnf install httpd -y
  • 关闭SELinux和Firewalld(实际生产不推荐这么做)
systemctl stop firewalld
setenforce 0	#临时关闭

2.2 配置文件

  • 先备份,以防裂开
cp /etc/httpd/conf/httpd.conf{,.bak}
rm -f /etc/httpd/conf/httpd.conf
egrep -v "^ *#|^$" httpd.conf.bak > httpd.conf
  • 核心配置文件/etc/httpd/conf/httpd.conf
<!--ServerRoot设置Apache软件的安装主目录,在这里是/etc/httpd目录-->
ServerRoot "/etc/httpd"
<!--格式:Listen [IP地址:]端口 [协议]-->
Listen 80
<!--通常在ServerRoot目录下的modules目录中,用于存放模块,文件后缀.so。如果希望Apache动态加载模块,需要在编译的时候通过--enable-so将mod_so以静态的方式编译到Apache的核心模块中。-->
<!--加载其他附加配置文件的路径。-->
Include conf.modules.d/*.conf
<!--指定Apache运行用户。-->
User apache
<!--指定Apache运行用户组。-->
Group apache
<!--用于收集客户问题反馈的邮箱。-->
ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>
<!--Web服务对客户端开放可见文档的根目录,即是访问网站的根路径。-->
DocumentRoot "/var/www/html"
<!--设置DocumentRoot指定目录的属性。-->
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    <!--找不到主页时,以目录的方式呈现。-->
    Options Indexes FollowSymLinks
    <!--none表示不使用,all表示允许,.htaccess控制。-->
    AllowOverride None
    <!--granted允许所有访问,denied拒绝所有访问。-->
    Require all granted
</Directory>
<IfModule dir_module>
    <!--定义主页文件,在这里是index.html-->
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
<!--定位服务器错误日志的位置,默认在ServerRoot目录下的log/error_log。-->
ErrorLog "logs/error_log"
<!--等级比warn高的日志才会被记录下来。-->
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 优化策略

3.1 404页面美化

vim httpd.conf
ErrorDocument 404 http://www.new404.com	#重定向到一个新的404页面。

(后续待补充)

posted @ 2021-07-02 20:36  SkyBiuBiu  阅读(97)  评论(0编辑  收藏  举报