千家信息网

七、Web服务器Apache的配置和管理

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,万维网Web,是在Internet上以超文本为基础形成的信息网。用户通过浏览器可以访问Web服务器上的信息资源,目前在Linux操作系统上最常用的Web服务器软件是Apache。Apache是一种开源
千家信息网最后更新 2026年11月01日七、Web服务器Apache的配置和管理

万维网Web,是在Internet上以超文本为基础形成的信息网。用户通过浏览器可以访问Web服务器上的信息资源,目前在Linux操作系统上最常用的Web服务器软件是Apache。Apache是一种开源的HTTP服务器软件,可以在包括UNIX、Linux以及Windows在内的大多数主流计算机操作系统中运行。

7.1、Apache简介

Apache是一款开源软件,所以得到了开源社区支持,不断开发出新的功能特性,并修补了原来的缺陷 。经过多年来不断的完善,如今的Apache已经是最流行的Web服务器端软件之一。Apache拥有以下众多的

特性,保证它可以高效稳定地运行。

1.支持最新的HTTP/1.1通信协议

2.拥有简单而强有力的基于文件的配置过程

3.支持通用网关接口CGI

4.支持基于IP和基于域名的虚拟主机

5.支持多种方式的HTTP认证

6.集成Perl处理模块

7.集成代理服务器模块

8.支持实时监视服务器状态和定制服务器日志

9.支持服务器端包含指令(SSI)

10.支持安全Socket层(SSL)

11.提供用户会话过程的跟踪

12.支持FastCGI

13.通过第三方模块可以支持JavaServlets

Apache采用模块化的设计,模块安装后就可以为Apache内核增加相应的新功能。默认情况下Apache已经安装部分模块,用户也可以使用模块配置,自定义Apache服务器中需要安装哪些功能,这正是Apache灵活性的表现。常见的功能模块有:

模块名功能说明
mod_actions.so运行基于MIME类型的CGI脚本
mod_alias.so支持虚拟目录和页面重定向
mod_asis发送包含自定义HTTP头的文件
mod_auth_basic.so基本验证
mod_auth_digest.so使用MD5加密算法的用户认证
mod_authn_alias.so允许使用第三方验证
mod_authn_anon.so允许匿名用户访问认证的区域
mod_authn_dbd.so使用数据库保存用户验证信息
mod_authn_dbm.so使用DBM数据文件保存用户验证信息
mod_authn_default.so处理用户验证失败
mod_authn_file.so使用文本文件保存用户验证信息
mod_authnz_ldap.so使用LDAP目录进行用户验证
mod_authz_default.so处理组验证失败
mod_authz_groupfile.so使用plaintext文件进行验证
mod_authz_host.so基于主机的组验证
mod_authz_user.so用户验证模块
mod_autoindex.so生成目录索引
mod_cache.so通向URI的内容缓存
mod_cgi.so支持CGI脚本
mod_cgid.so使用外部CGI进程运行CGI脚本
mod_dir.so提供用于trailing slash的目录和索引文件
mod_env.so调整传输个给CGI脚本和SSI页面的环境变量
mod_example.so
解释Apache模块的API
mod_filter.so过滤信息
mod_imagemap.so
imagemap处理
mod_include.so解析HTML文件
mod_isapi.so
ISAPI扩展
mod_ldap.so使用第三方LDAP模块进行LDAP连接和服务
mod_log_config.so
记录发给服务器的访问请求
mod_login.so
记录每个请求输入、输出的字节数
mod_mime.so联合被请求文件扩展名和文件行为的内容
mod_negotiation.so提供内容协商
mod_nw_ssl
为NetWare打开SSL加密
mod_proxy.so支持HTTP1.1协议的代理和网关服务器
mod_proxy_ajp.somod_proxy的AJP支持模块
mod_proxy_balancer.somod_proxy的负载均衡模块
mod_proxy_ftp.somod_proxy的ftp支持模块
mod_proxy_http.somod_proxy的HTTP支持模块
mod_setenvif.so允许设置基于请求的环境变量
mod_so.so
在启动或重启时提高可执行编码和模块的启动
mod_ssl.so
使用SSL和TLS的加密
mod_status.so提供服务器性能运行信息
mod_userdir.so设置每个用户的网站目录
mod_usertrack.so记录用户在网站上的活动
mod_vhost_alias.so提供大量虚拟主机的动态配置

7.2、Apache服务器的安装

对于Centos6.5,在yum源中有自带的Apache,版本为2.2.15,可以直接通过yum命令进行安装。为了能够获得最新版本的Apache时,可以通过Apache官方网站www.apache.org下载该软件的源代码进行安装,其软件名称为httpd,当前最新稳定版本为2.4.25。

[root@mylinux home]# yum list httpdLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.cn99.com * epel: mirrors.tuna.tsinghua.edu.cn * epel-debuginfo: mirrors.tuna.tsinghua.edu.cn * epel-source: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.njupt.edu.cn * updates: ftp.sjtu.edu.cnAvailable Packageshttpd.x86_64                          2.2.15-59.el6.centos                         base[root@mylinux home]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.25.tar.bz2--2017-05-26 06:32:59--  http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.25.tar.bz2Resolving mirror.bit.edu.cn... 202.204.80.77, 2001:da8:204:2001:250:56ff:fea1:22Connecting to mirror.bit.edu.cn|202.204.80.77|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 6398218 (6.1M) [application/octet-stream]Saving to: httpd-2.4.25.tar.bz2100%[======================================================>] 6,398,218   1.11M/s   in 5.9s    2017-05-26 06:33:07 (1.04 MB/s) - httpd-2.4.25.tar.bz2 saved [6398218/6398218][root@mylinux home]# lsbind-9.10.4-P8         httpd-2.4.25.tar.bz2  Python-2.7.13.tar.xz  Python-3.6.1.tar.xzbind-9.10.4-P8.tar.gz  mylinux               Python-3.6.1[root@mylinux home]# ls httpd-2.4.25.tar.bz2

1、安装依赖文件

[root@mylinux httpd-2.4.25]# yum remove apr-util-devel apr apr-util-mysql apr-docs apr-devel apr-util apr-util-docs...Removed:  apr.x86_64 0:1.3.9-5.el6_2                  apr-util.x86_64 0:1.3.9-3.el6_0.1        Dependency Removed:  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1        httpd-tools.x86_64 0:2.2.15-59.el6.centos         subversion.x86_64 0:1.6.11-15.el6_7          Complete![root@mylinux home]# wget [root@mylinux home]# wget [root@mylinux home]# wget [root@mylinux home]# tar xf apr-1.5.2.tar.gz        #解决apr not found问题[root@mylinux home]# cd apr-1.5.2[root@mylinux apr-1.5.2]# ./configure --prefix=/usr/local/apr[root@mylinux apr-1.5.2]# make && make install[root@mylinux home]# tar xf apr-util-1.5.4.tar.gz   #解决APR-util not found问题[root@mylinux home]# cd apr-util-1.5.4[root@mylinux apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config [root@mylinux apr-util-1.5.4]# make && make install[root@mylinux home]# tar xf pcre-8.40.tar.bz2       #解决pcre问题[root@mylinux home]# cd pcre-8.40[root@mylinux pcre-8.40]# ./configure --prefix=/usr/local/pcre[root@mylinux pcre-8.40]# make && make install[root@mylinux home]#yum install pcre-devel -y

2、编译安装httpd

[root@mylinux home]# tar xf httpd-2.4.25.tar.bz2[root@mylinux home]# cd httpd-2.4.25[root@mylinux httpd-2.4.25]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event[root@mylinux httpd-2.4.25]make && make install#导出头文件[root@mylinux httpd-2.4.25]# ln -sv /usr/local/apache/include /usr/include/httpd`/usr/include/httpd' -> `/usr/local/apache/include'  #导出库文件[root@mylinux httpd-2.4.25]# ldconfig [root@mylinux httpd-2.4.25]# ldconfig  -p    #查看当前系统库文件326 libs found in cache `/etc/ld.so.cache'        libz.so.1 (libc6,x86-64) => /lib64/libz.so.1        libz.so (libc6,x86-64) => /usr/lib64/libz.so        libyaml-0.so.2 (libc6,x86-64) => /usr/lib64/libyaml-0.so.2        libxtables.so.4 (libc6,x86-64) => /lib64/libxtables.so.4        libxslt.so.1 (libc6,x86-64) => /usr/lib64/libxslt.so.1        libxml2.so.2 (libc6,x86-64) => /usr/lib64/libxml2.so.2...导出man手册#vim /etc/man.config [root@mylinux httpd-2.4.25]# vim /etc/man.config...MANPATH      /usr/local/apache/man/   #添加该行输出二进制程序[root@mylinux httpd-2.4.25]# vim /etc/profile.d/httpd.shexport PATH=/usr/local/apache/bin:$PATH[root@mylinux httpd-2.4.25]# source /etc/profile.d/httpd.sh [root@mylinux httpd-2.4.25]# apachectl start    #启动AH00557: httpd: apr_sockaddr_info_get() failed for mylinuxAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message[root@mylinux httpd-2.4.25]# apachectl stop      #关闭AH00557: httpd: apr_sockaddr_info_get() failed for mylinuxAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message#解决错误信息的方法[root@mylinux etc]# vim /etc/httpd24/httpd.conf   修改ServerName www.example.com:80 为 ServerName localhost:80[root@mylinux etc]# apachectl start             #启动[root@mylinux etc]# ps -ef | grep httpd         #检查进程root     39780     1  0 07:31 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   39781 39780  0 07:31 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   39782 39780  0 07:31 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   39783 39780  0 07:31 ?        00:00:00 /usr/local/apache/bin/httpd -k startroot

3、检查Apache页面

7.3、Apache服务器的基本配置和维护

Apache在安装时已经自动采用了一系列的默认设置,安装完成后Web服务器已经可以对外提供WWW服务,但为了能够更好地运作,还需要对Apache进行一些配置。Apache的主要配置文件为httpd.conf,此外,Apache还提供了相关的命令以方便管理和配置。

常用的命令有:

httpd:Apache 服务器。

apachectl:Apache HTTP 服务器控制工具。

abApache HTTP 服务器性能基准工具。

-c: 模拟的并发数-n: 总请求数

apxs:Apache 扩展工具。

configure:配置源代码。

dbmmanage:为基本认证创建和更新DBM 格式的用户认证文件。

htcacheclean:清理磁盘缓存。

htdigest:为摘要认证创建和更新用户认证文件。

htdbm:操作 DBM 密码数据库。

htpasswd:为基本认证创建和更新用户认证文件。

httxt2dbm:为 RewriteMap 创建 dbm 文件。

logresolve:将 Apache 日志文件中的 IP 地址解析到主机名称。

rotatelogs:不关闭 Apache 而切换日志文件。

suexec:执行外部程序前切换用户。

此外测试Apache服务器是否正常工作,还可以使用curl命令。curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST、PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传,http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

curl的常用选项:

-A/--user-agent 设置用户代理发送给服务器

-basic 使用HTTP基本验证

--tcp-nodelay 使用TCP_NODELAY选项

-e/--referer 来源网址

--cacert CA证书 (SSL)

--compressed 要求返回是压缩的形势

-H/--header 自定义头信息传递给服务器

-I/--head 只显示响应报文首部信息

--limit-rate 设置传输速度

-u/--user 设置服务器的用户和密码

-0/--http1.0 使用HTTP 1.0

查看Apache软件的版本信息

[root@mylinux bin]# apachectl -VServer version: Apache/2.4.25 (Unix)Server built:   May 26 2017 07:07:15Server's Module Magic Number: 20120211:67Server loaded:  APR 1.5.2, APR-UTIL 1.5.4Compiled using: APR 1.5.2, APR-UTIL 1.5.4Architecture:   64-bitServer MPM:     event           #工作模式  threaded:     yes (fixed thread count)    forked:     yes (variable process count)Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/usr/local/apache" -D SUEXEC_BIN="/usr/local/apache/bin/suexec" -D DEFAULT_PIDLOG="logs/httpd.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="/etc/httpd24/mime.types" -D SERVER_CONFIG_FILE="/etc/httpd24/httpd.conf"

查看已经被编译的模块

[root@mylinux bin]# apachectl -lCompiled in modules:  core.c  mod_so.c  http_core.c
  • httpd的MPM特性

httpd采用core + modules模块化设计方法,其中模块采用DSO(Dynamic Shared Object动态模块加载)的方式,具有MPM(Multipath Processing Module,多道处理模块)特性。其主要有三种工作方式:

prefork: 一个请求用一个进程处理,稳定性好、大并发场景下消耗资源较多;事先创建进程,按需维持适当的进程,模块块设计,核心比较小,各种功能都模块添加(包括php),支持运行配置,支持单独编译模块,支持多种方式的虚拟主机配置。

worker: 一个进程多个线程,一个线程响应一个请求

event: 一个线程响应多个请求,事件驱动,主要目的在于实现单线程响应多个请求

  StartServers: 默认启动的工作进程数;  MinSpareServers: 最少空闲进程数;  MaxSpareServers: 最大空闲进程数;  ServerLimit: 最大活动进程数;  MaxClients: 并发请求的最大数;  MaxRequestsPerChild: 每个子进程在生命周期内所能够服务的最多请求个数;

  StartServers:启动的子进程的个数  MaxClients: 并发请求的最大数;  MinSpareThreads:最小空闲线程数;  MaxSpareThreads:最大空闲线程数;  ThreadsPerChild:每个子进程可生成的线程数;  MaxRequestsPerChild:每个子进程在生命周期内所能够服务的最多请求个数

    StartServers             3    MinSpareThreads         75    MaxSpareThreads        250    ThreadsPerChild         25    MaxRequestWorkers      400    MaxConnectionsPerChild   0
  • httpd.conf配置文件介绍

httpd.conf是Apache的配置文件,Apache中的常见配置主要都是通过修改该文件来实现的,该文件更改后需要重启Apache服务使更改的配置生效。

与Apache网络和系统相关的选项:

ServerRoot "/usr/local/apache"     #设置Apache安装目录Listen 80                          #设置监听端口User daemon                        #设置Apache进程的执行者Group daemon                       #设置Apache进程执行者所属的用户组ServerAdmin you@example.com        #设置网站管理员邮箱ServerName localhost:80            #服务其主机与端口

与Apache文件和目录权限相关选项:

                             #设置根目录权限    AllowOverride none    Require all deniedDocumentRoot "/usr/local/apache/htdocs"   #设置网站根目录    #设置/usr/local/apache/htdocs目录权限    Options Indexes FollowSymLinks   AllowOverride None   Require all granted#最终匹配结果:二者都匹配或二者都无匹配项时,则以后者为准;否则,则以匹配到的为准                     #设置index.html为首页    DirectoryIndex index.html                            #.ht*为后缀文件的访问权限    Require all denied

常见可用选项:

Indexes: 缺少指定的默认页面时,允许将目录中所有文件以列表形式返回用户

FollowSymLinks: 允许跟随符号链接所指向的原始文件;

None: 所有都不启用;

All: 所有都启用;

ExecCGI: 允许使用mod_cgi模块执行CGI脚本;

Includes: 允许使用mod_include模块实现服务器端包含(SSI);

MultiViews: 允许使用mod_negotiation实现内容协商;

SymLinksIfOwnerMatch:在链接文件属主属组与原始文件的属主属组相同时,允许跟随符号链接所指向的原始文件;

与Apache日志相关的选项:

ErrorLog "logs/error_log"     #设置错误日志位置LogLevel warn                 #设置错误日志级别  #设置访问日志的格式模板    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined    LogFormat "%h %l %u %t \"%r\" %>s %b" common          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio        CustomLog "logs/access_log" common    #设置访问日志的位置和格式       #别名设置    ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"     #设置/usr/local/apache/cgi-bin目录权限    AllowOverride None    Options None    Require all granted    RequestHeader unset Proxy early                    #mime模块的相关配置    TypesConfig /etc/httpd24/mime.types    AddType application/x-compress .Z    AddType application/x-gzip .gz .tgz    Include /etc/httpd24/extra/proxy-html.conf                     #ssl模块的相关设置  SSLRandomSeed startup builtin  SSLRandomSeed connect builtin

配置文件的修改

用户可以直接通过图形界面中文件编辑器或者通过vi对配置文件进行修改,修改完成后必须重启Apache服务器才能使更改生效。如果配置文件中添加了错误的参数或者设置了错误的参数值,Apache将无法启动,为了方便用户验证httpd.conf配置文件的参数是否配置正确,Apache提供了命令自动完成配置文件的检查工作。

[root@mylinux httpd24]# apachectl configtestSyntax OK
  • 符号链接和虚拟目录

在默认情况下,用户通过http访问Web服务器所浏览到的所有资料都是存放在DocumentRoot参数所指定的目录之下,但该参数只能指定一个目录作为参数值,当该目录空间不足时,要把文件放在其他文件系统时,用户如何访问文件呢?Apache提供了来年各种解决办法。

符号链接

假设现在的文档根目录为/usr/local/apache/htdocs,希望把/usr/share/doc目录映射成/doc/的访问路径。配置过程很简单,使用ln命令即可。

[root@mylinux httpd24]# cd /usr/local/apache/htdocs/[root@mylinux htdocs]# ln -s /usr/local/share/ doc[root@mylinux htdocs]#

虚拟目录

使用虚拟目录是另一种将根目录以外的内容加入到站点中的办法,例如把/var/log目录映射成网站根目录的log下。

[root@mylinux htdocs]# vim /etc/httpd24/httpd.conf...    #    # Redirect: Allows you to tell clients about documents that used to     # exist in your server's namespace, but do not anymore. The client     # will make a new request for the document at its new location.    # Example:    # Redirect permanent /foo http://www.example.com/bar    #    # Alias: Maps web paths into filesystem paths and is used to    # access content that does not live under the DocumentRoot.    # Example:    Alias /log   "/var/log"                #需要添加的信息           Options Indexes FollowSymLinks       AllowOverride None       Require all granted        #    # If you include a trailing / on /webpath then the server will    # require it to be present in the URL.  You will also likely    # need to provide a  section to allow access to    # the filesystem path.    #    # ScriptAlias: This controls which directories contain server scripts.     # ScriptAliases are essentially the same as Aliases, except that    # documents in the target directory are treated as applications and    # run by the server when requested rather than as documents sent to the    # client.  The same rules about trailing "/" apply to ScriptAlias    # directives as to Alias.    #    ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"...[root@mylinux htdocs]# apachectl configtestSyntax OK[root@mylinux htdocs]# apachectl restart

  • 页面重定向

如果用户经常访问某个页面,他可能会把页面的URL添加到收藏夹中,在每次访问页面的时候可以直接单击收藏夹中的记录访问。但是如果网站进行了目录结构的更新后,用户再使用原来的URL访问就会出现404页面无法找到的错误,为了方便用户能继续使用原来的URL进行访问,这是就要使用页面重定向。

Apache提供了Redirect命令用于配置页面重定向,命令格式为:

Redirect [HTTP代码]  用户请求的URL [重定向后的URL]

假设有一个/doc目录,现在管理员对网站目录结构进行整理,并把/doc目录移动到/old-doc目录下,为解决这个问题需要/doc配置页面重定向。

[root@mylinux old-doc]# vim /etc/httpd24/httpd.conf    #打开配置文件...    #    # Redirect: Allows you to tell clients about documents that used to    # exist in your server's namespace, but do not anymore. The client    # will make a new request for the document at its new location.    # Example:    _Redirect 303 /doc       #添加重定向信息... [root@mylinux old-doc]# apachectl configtestSyntax OK[root@mylinux old-doc]# apachectl restart

输入http://192.168.191.128/doc,回车后自动重定向到了http://192.168.191.128/old-doc。

  • Apache日志文件

Apache服务器运行后会生成两个日志文件,分别是access_log(访问日志)和error_log(错误日志),采用默认安装时,这些文件可以在/usr/local/apache/logs目录下找到。

[root@mylinux extra]# cd /usr/local/apache/logs/[root@mylinux logs]# lsaccess_log  error_log  httpd.pid

访问日志

Apache的访问日志就是记录Web服务器的所有访问活动,每一行记录了一条访问记录,由7个部分组成,其格式为:

客户端地址 访问者的标识 访问者的验证名字 请求时间 请求类型 请求的HTTP代码 发送给客户端的字节数

客户端地址: 表明访问网站的客户端IP地址

访问者的标识: 一般为空白,'-'代替

访问者的验证名字: 用于记录访问者进行身份验证时提供的名字,一般也为空白

请求时间: 记录访问操作的发生时间

请求类型: 记录服务器收到的是一个什么类型请求

请求的HTTP代码: 该信息可判断请求是否成功

发送给客户端的字节数:发送给客户端的总字节数

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined    LogFormat "%h %l %u %t \"%r\" %>s %b" common   #配置日志格式          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio        CustomLog "logs/access_log" common     #访问日志的格式

LogFormat Format_String Format_Name #各自段表示的含义

%h: 客户端地址

%l: 远程登录名,通常为-

%u: 认证时的远程用户名,没有认证时为-

%t: 收到请求时的时间;

%r: 请求报文的起始行;

%>s: 响应状态码;

%b: 响应报文的长度,单位为字节

%{Header_Name}i: 记录指定请求报文首部的内容(value);

[root@mylinux logs]# cat access_log 192.168.191.1 - - [26/May/2017:07:32:23 +0800] "GET / HTTP/1.1" 200 45192.168.191.1 - - [26/May/2017:07:32:23 +0800] "GET /favicon.ico HTTP/1.1" 404 209192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET / HTTP/1.1" 200 45192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET /favicon.ico HTTP/1.1" 404 209192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET /favicon.ico HTTP/1.1" 404 209192.168.191.1 - - [02/Jun/2017:04:20:12 +0800] "GET /doc HTTP/1.1" 301 235192.168.191.1 - - [02/Jun/2017:04:20:12 +0800] "GET /doc/ HTTP/1.1" 200 332192.168.191.1 - - [02/Jun/2017:04:30:51 +0800] "GET /log HTTP/1.1" 404 201

错误日志

错误日志是Apache提供的另一种标准日志,该日志文件记录了Apache服务器运行过程所发生的错误信息。httpd.conf配置文件提供了两个配置参数:

ErrorLog "logs/error_log"     #错误日志文件LogLevel warn                 #错误日志级别

错误日志级别有以下几种:

严重程度等级说明
1emerg系统不可用
2alert需要立即引起注意的情况
3
crit危急情况
4
error错误信息
5
warn警告信息
6
notice需要引起注意的情况
7
info一般信息
8
debug由运行于debug模式的程序输出的信息

emerg级别的信息严重程度最高,debug级别最低。如果把错误日志设置成warn级别,则严重程度由1-5的所有错误信息都会被记录下来。

[root@mylinux logs]# cat error_log [Fri May 26 07:20:43.874118 2017] [mpm_event:notice] [pid 39580:tid 140405771355904] AH00489: Apache/2.4.25 (Unix) configured -- resuming normal operations[Fri May 26 07:20:43.874427 2017] [core:notice] [pid 39580:tid 140405771355904] AH00094: Command line: '/usr/local/apache/bin/httpd'[Fri May 26 07:22:07.002599 2017] [mpm_event:notice] [pid 39580:tid 140405771355904] AH00491: caught SIGTERM, shutting down[Fri May 26 07:29:51.083928 2017] [mpm_event:notice] [pid 39676:tid 140351188608768] AH00489: Apache/2.4.25 (Unix) configured -- resuming normal operations[Fri May 26 07:29:51.085382 2017] [core:notice] [pid 39676:tid 140351188608768] AH00094: Command line: '/usr/local/apache/bin/httpd'[Fri May 26 07:29:57.164059 2017] [mpm_event:notice] [pid 39676:tid 140351188608768] AH00491: caught SIGTERM, shutting down

从文件内容可以看出,每一行记录一个错误,由三部分组成

时间 错误等级 错误信息

7.4、Apache安全配置

Apache提供了多种安全控制手段,包括设置Web访问控制、用户登录密码认证以及.htaccess文件等。通过这些技术手段,可以进一步提升Apache服务器的安全级别,减少服务器受***或数据被窃取的风险。

  • 访问控制

设置访问控制是提高Apache服务器安全级别最有效的手段之一。Diretory段用于设置与目录相关的参数和指令,包括访问控制和认证。

    目录相关的配置参数和指令

Allow指令:用于设置那些客户端可以访问Apache

Allow from [All/全域名/部分域名/IP地址/网络地址/CIDR地址]...

All:表示所有客户端

全域名:表示域名对应的客户端,如www.domain.com

部分域名:表示域名内的所有客户端,如domain.com

IP地址:如192.168.1.1

网络地址:如172.20.17.0/256.356.355.0

CIDR地址:如172.20.17.0/24

Deny指令:用于设置拒绝哪些客户端访问Apache

Order指令:用于指定执行访问规则的先后顺序

Order Allow,Deny :先执行允许访问规则,在执行拒绝访问规则

Order Deny,Allow :先执行拒绝访问规则,在执行允许访问规则

  • 用户认证

Apache的用户认证包括基本认证和摘要认证。摘要认证比基础认证更加安全,但不是所有的浏览器都支持摘要认证。要使用用户认证,首先要创建保存用户和密码的认证密码文件。在Apache中提供了htpasswd命令用于创建和修改认证密码文件。

[root@mylinux httpd24]# htpasswd -c /etc/httpd24/.htpasswd sam   #添加认证密码文件New password: Re-type new password: Adding password for user sam[root@mylinux httpd24]# vim httpd.conf          #添加配置信息                   #需要认证的目录        Options None                             AllowOverride AuthConfig        AuthType Basic                          #认证类型        AuthName "Private Area"        AuthUserFile /etc/httpd24/.htpasswd     #指明认证密码文件的位置        Require valid-user    [root@mylinux httpd24]# apachectl configtestSyntax OK[root@mylinux httpd24]# apachectl restart[root@mylinux fin]# ls                 #fin目录下的文件applications  index.html  info  man[root@mylinux fin]# cat index.html     #index.html文件内容Auth


7.5、虚拟主机

虚拟主机服务就是指将一台物理主机服务器虚拟成多态Web服务器,可以有效节省硬件资源且方便管理。Apache支撑3种不同的虚拟主机方式:

基于不同的IP实现不同的虚拟主机:变化IP

基于不同的port实现不同的虚拟主机:变化port

基于不同的FQDN实现不同的虚拟主机:变化ServerName的值

  • 基于IP的虚拟主机

基于IP的虚拟主机服务商必须同时设置有多个IP,服务器根据用户请求目的的IP地址来判定用户请求的那个虚拟主机。在使用虚拟主机时,必须要关闭中心主机。

[root@mylinux fin]# ifconfig     #当前IPeth0      Link encap:Ethernet  HWaddr 00:0C:29:B7:AB:D0            inet addr:192.168.191.128  Bcast:192.168.191.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:feb7:abd0/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:3627 errors:0 dropped:0 overruns:0 frame:0          TX packets:2866 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:353550 (345.2 KiB)  TX bytes:516806 (504.6 KiB)lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:0 errors:0 dropped:0 overruns:0 frame:0          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)[root@mylinux fin]# ifconfig eth0:1 192.168.191.100 netmask 255.255.255.255#添加IP地址[root@mylinux fin]# ifconfig eth0:2 192.168.191.101 netmask 255.255.255.255#添加IP地址[root@mylinux fin]# ifconfig eth0      Link encap:Ethernet  HWaddr 00:0C:29:B7:AB:D0            inet addr:192.168.191.128  Bcast:192.168.191.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:feb7:abd0/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:9337 errors:0 dropped:0 overruns:0 frame:0          TX packets:7346 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:899106 (878.0 KiB)  TX bytes:1155902 (1.1 MiB)eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:B7:AB:D0            inet addr:192.168.191.100  Bcast:192.168.191.100  Mask:255.255.255.255          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:B7:AB:D0            inet addr:192.168.191.101  Bcast:192.168.191.101  Mask:255.255.255.255          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:20 errors:0 dropped:0 overruns:0 frame:0          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:2148 (2.0 KiB)  TX bytes:2148 (2.0 KiB)
[root@mylinux httpd24]# vim httpd.conf              #打开主配置文件#DocumentRoot "/usr/local/apache/htdocs"            #注释该行,关闭中心主机Include /etc/httpd24/extra/httpd-vhosts.conf        #启动虚拟主机配置文件 [root@mylinux httpd24]# vim extra/httpd-vhosts.conf #到专用虚拟主机配置文件进行配置                        ServerAdmin webmaster@dummy-host.example.com    DocumentRoot "/usr/local/apache/docs/server1"       ServerName www.server1.com    ErrorLog "logs/server1.error_log"    CustomLog "logs/server1.access_log" common    ServerAdmin webmaster@dummy-host.example.com    DocumentRoot "/usr/local/apache/docs/server2"    ServerName www.server2.com    ErrorLog "logs/server2.error_log"    CustomLog "logs/server2.access_log" common[root@mylinux httpd24]# apachectl configtestSyntax OK[root@mylinux httpd24]# cd /usr/local/apache/       #创建首页[root@mylinux apache]# mkdir docs[root@mylinux apache]# cd docs/[root@mylinux docs]# mkdir {server1,server2}[root@mylinux docs]# cd server1/[root@mylinux server1]# echo "www.server1.com" > index.html[root@mylinux server1]# cd ..[root@mylinux docs]# cd server2/[root@mylinux server2]# echo "www.server2.com" > index.html[root@mylinux server2]# apachectl restart

  • 基于主机名的虚拟主机服务

其具体配置过程为:

[root@mylinux extra]# vim /etc/hosts     #添加主机名解析127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.191.128 www.server1.com192.168.191.128 www.server2.com"/etc/hosts" 4L, 222C written[root@mylinux httpd24]# vim httpd.conf              #打开主配置文件#DocumentRoot "/usr/local/apache/htdocs"            #注释该行,关闭中心主机Include /etc/httpd24/extra/httpd-vhosts.conf        #启动虚拟主机配置文件 [root@mylinux httpd24]# vim extra/httpd-vhosts.conf #到专用虚拟主机配置文件进行配置    ServerAdmin webmaster@dummy-host.example.com    DocumentRoot "/usr/local/apache/docs/server1"    ServerName www.server1.com    ErrorLog "logs/server1.error_log"    CustomLog "logs/server1.access_log" common           Options Indexes FollowSymLinks       AllowOverride None       Require all granted        ServerAdmin webmaster@dummy-host.example.com    DocumentRoot "/usr/local/apache/docs/server2"    ServerName www.server2.com    ErrorLog "logs/server2.error_log"    CustomLog "logs/server2.access_log" common           Options Indexes FollowSymLinks       AllowOverride None       Require all granted    [root@mylinux extra]# apachectl configtestSyntax OK[root@mylinux extra]# apachectl restart[root@mylinux extra]# curl       #测试 www.server1.com[root@mylinux extra]# curl       #测试 www.server2.com
  • 防止网站图片盗链

为了防止其他网站非法盗取本网站中的图片文件,可以在Apache中进行配置,以禁止图片被非法盗用。假设本网站域名为www.myweb.com,用户可在配置文件中加入如下内容:

SetEnvIfNoCase Referer "^http://www.myweb.com/" local_ref=1  Order Allow,Deny  Allow from env=local_ref



文件 服务 配置 服务器 用户 认证 模块 目录 主机 支持 信息 日志 错误 虚拟主机 进程 验证 地址 网站 客户 页面 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司 新华互联网科技 中国互联网科技工作委员会毛军 重生之互联网科技帝国 重庆互联网络科技 关于新基建和互联网科技 合肥互联网科技公司 奇酷互联网络科技 苏州互联网科技贷款公司 北京禾田雨橡互联网科技 万达互联网科技 互联网科技公司有哪些 互联网科技公司名称 互联网科技公司需要的职位 玖二柒互联网科技公司待遇 华服互联网科技官网 武汉健翔互联网络科技有限公司 中国科技创新与世界互联网大会 广州燕处互联网科技有限公司 山东蛋芒互联网科技有限公司 互联网科技官网 互联网最新的科技 圆梦互联网科技 狮子座互联网科技有限公司 兼蜜互联网科技 阿里巴巴算科技企业还是互联网 中国互联网有什么科技含量 知域互联网科技有限公司大骗子 浙南科技城互联网大厦建设工程 西安左小互联网科技有限公司
0