万维网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.so | mod_proxy的AJP支持模块 |
mod_proxy_balancer.so | mod_proxy的负载均衡模块 |
mod_proxy_ftp.so | mod_proxy的ftp支持模块 |
mod_proxy_http.so | mod_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 httpd
Loaded plugins: fastestmirror
Loading 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.cn
Available Packages
httpd.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.bz2
Resolving mirror.bit.edu.cn... 202.204.80.77, 2001:da8:204:2001:250:56ff:fea1:22
Connecting to mirror.bit.edu.cn|202.204.80.77|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6398218 (6.1M) [application/octet-stream]
Saving to: httpd-2.4.25.tar.bz2
100%[======================================================>] 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]# ls
bind-9.10.4-P8 httpd-2.4.25.tar.bz2 Python-2.7.13.tar.xz Python-3.6.1.tar.xz
bind-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.sh
export 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 mylinux
AH00558: 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 mylinux
AH00558: 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 start
daemon 39781 39780 0 07:31 ? 00:00:00 /usr/local/apache/bin/httpd -k start
daemon 39782 39780 0 07:31 ? 00:00:00 /usr/local/apache/bin/httpd -k start
daemon 39783 39780 0 07:31 ? 00:00:00 /usr/local/apache/bin/httpd -k start
root
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 <string> 设置用户代理发送给服务器 -basic 使用HTTP基本验证 --tcp-nodelay 使用TCP_NODELAY选项 -e/--referer <URL> 来源网址 --cacert <file> CA证书 (SSL) --compressed 要求返回是压缩的形势 -H/--header <line>自定义头信息传递给服务器 -I/--head 只显示响应报文首部信息 --limit-rate <rate> 设置传输速度 -u/--user <user[:password]>设置服务器的用户和密码 -0/--http1.0 使用HTTP 1.0 |
查看Apache软件的版本信息
[root@mylinux bin]# apachectl -V
Server version: Apache/2.4.25 (Unix)
Server built: May 26 2017 07:07:15
Server's Module Magic Number: 20120211:67
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server 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 -l
Compiled 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: 一个线程响应多个请求,事件驱动,主要目的在于实现单线程响应多个请求 |
<IfModule mpm_prefork_module>
StartServers: 默认启动的工作进程数;
MinSpareServers: 最少空闲进程数;
MaxSpareServers: 最大空闲进程数;
ServerLimit: 最大活动进程数;
MaxClients: 并发请求的最大数;
MaxRequestsPerChild: 每个子进程在生命周期内所能够服务的最多请求个数;
</IfModule>
<IfModule mpm_worker_module>
StartServers:启动的子进程的个数
MaxClients: 并发请求的最大数;
MinSpareThreads:最小空闲线程数;
MaxSpareThreads:最大空闲线程数;
ThreadsPerChild:每个子进程可生成的线程数;
MaxRequestsPerChild:每个子进程在生命周期内所能够服务的最多请求个数
</IfModule>
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
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文件和目录权限相关选项:
<Directory /> #设置根目录权限
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/usr/local/apache/htdocs" #设置网站根目录
<Directory "/usr/local/apache/htdocs"> #设置/usr/local/apache/htdocs目录权限
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#最终匹配结果:二者都匹配或二者都无匹配项时,则以后者为准;否则,则以匹配到的为准
<IfModule dir_module> #设置index.html为首页
DirectoryIndex index.html
</IfModule>
<Files ".ht*"> #.ht*为后缀文件的访问权限
Require all denied
</Files>
常见可用选项: Indexes: 缺少指定的默认页面时,允许将目录中所有文件以列表形式返回用户 FollowSymLinks: 允许跟随符号链接所指向的原始文件; None: 所有都不启用; All: 所有都启用; ExecCGI: 允许使用mod_cgi模块执行CGI脚本; Includes: 允许使用mod_include模块实现服务器端包含(SSI); MultiViews: 允许使用mod_negotiation实现内容协商; SymLinksIfOwnerMatch:在链接文件属主属组与原始文件的属主属组相同时,允许跟随符号链接所指向的原始文件; |
与Apache日志相关的选项:
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" common #设置访问日志的位置和格式
</IfModule>
<IfModule alias_module> #别名设置
ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/usr/local/apache/cgi-bin"> #设置/usr/local/apache/cgi-bin目录权限
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module> #mime模块的相关配置
TypesConfig /etc/httpd24/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
</IfModule>
<IfModule proxy_html_module>
Include /etc/httpd24/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module> #ssl模块的相关设置
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
配置文件的修改
用户可以直接通过图形界面中文件编辑器或者通过vi对配置文件进行修改,修改完成后必须重启Apache服务器才能使更改生效。如果配置文件中添加了错误的参数或者设置了错误的参数值,Apache将无法启动,为了方便用户验证httpd.conf配置文件的参数是否配置正确,Apache提供了命令自动完成配置文件的检查工作。
[root@mylinux httpd24]# apachectl configtest
Syntax 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
...
<IfModule alias_module>
#
# 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" #需要添加的信息
<Directory "/var/log">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#
# 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 <Directory> 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/"
</IfModule>
...
[root@mylinux htdocs]# apachectl configtest
Syntax 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 #打开配置文件
...
<IfModule alias_module>
#
# 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 configtest
Syntax 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]# ls
access_log error_log httpd.pid
访问日志
Apache的访问日志就是记录Web服务器的所有访问活动,每一行记录了一条访问记录,由7个部分组成,其格式为:
客户端地址 访问者的标识 访问者的验证名字 请求时间 请求类型 请求的HTTP代码 发送给客户端的字节数
客户端地址: 表明访问网站的客户端IP地址 访问者的标识: 一般为空白,'-'代替 访问者的验证名字: 用于记录访问者进行身份验证时提供的名字,一般也为空白 请求时间: 记录访问操作的发生时间 请求类型: 记录服务器收到的是一个什么类型请求 请求的HTTP代码: 该信息可判断请求是否成功 发送给客户端的字节数:发送给客户端的总字节数 |
<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" common #访问日志的格式
</IfModule>
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 45
192.168.191.1 - - [26/May/2017:07:32:23 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET / HTTP/1.1" 200 45
192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.191.1 - - [02/Jun/2017:04:17:02 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.191.1 - - [02/Jun/2017:04:20:12 +0800] "GET /doc HTTP/1.1" 301 235
192.168.191.1 - - [02/Jun/2017:04:20:12 +0800] "GET /doc/ HTTP/1.1" 200 332
192.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 #错误日志级别
错误日志级别有以下几种:
严重程度 | 等级 | 说明 |
1 | emerg | 系统不可用 |
2 | alert | 需要立即引起注意的情况 |
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段用于设置与目录相关的参数和指令,包括访问控制和认证。
<Directory 目录的路径>
目录相关的配置参数和指令
</Directory>
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 #添加配置信息
<Directory "/usr/local/apache/htdocs/fin"> #需要认证的目录
Options None
AllowOverride AuthConfig
AuthType Basic #认证类型
AuthName "Private Area"
AuthUserFile /etc/httpd24/.htpasswd #指明认证密码文件的位置
Require valid-user
</Directory>
[root@mylinux httpd24]# apachectl configtest
Syntax 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
虚拟主机服务就是指将一台物理主机服务器虚拟成多态Web服务器,可以有效节省硬件资源且方便管理。Apache支撑3种不同的虚拟主机方式:
基于不同的IP实现不同的虚拟主机:变化IP 基于不同的port实现不同的虚拟主机:变化port 基于不同的FQDN实现不同的虚拟主机:变化ServerName的值 |
基于IP的虚拟主机
基于IP的虚拟主机服务商必须同时设置有多个IP,服务器根据用户请求目的的IP地址来判定用户请求的那个虚拟主机。在使用虚拟主机时,必须要关闭中心主机。
[root@mylinux fin]# ifconfig #当前IP
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: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:1
eth0: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:1
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: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 #到专用虚拟主机配置文件进行配置
<VirtualHost 192.168.191.100>
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
</VirtualHost>
<VirtualHost 192.168.191.101>
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
</VirtualHost>
[root@mylinux httpd24]# apachectl configtest
Syntax 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.localdomain6
192.168.191.128 www.server1.com
192.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 #到专用虚拟主机配置文件进行配置
<VirtualHost 192.168.191.128>
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
<Directory "/usr/local/apache/docs/server1">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.191.128>
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
<Directory "/usr/local/apache/docs/server2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@mylinux extra]# apachectl configtest
Syntax 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
<FilesMatch ".(gif|jpg|bmp)">
Order Allow,Deny
Allow from env=local_ref
</FileMatch>
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。