Web网站服务
- 【技能展示】
-
- 【Apache简介】
-
- 【安装httpd服务器——编译安装】
-
- 【 httpd服务器的基本配置】
-
- 【全局配置项】
-
- 【区域配置项】
【技能展示】
【技能展示】
1.学会编译安装httpd服务器
2.熟悉httpd服务的部署过程及常见配置
3.学会构建AWStats日志分析系统
【Apache简介】
【Apache简介】
【Apache起源】
1.源于 A Patchy Server,著名的开源Web服务软件
2.1995年时,发布Apache服务程序的1.0版本
3.由Apache软件基金会(ASF)负责维护
4.最新的名称为 “Apache HTTP Server”
5.官方站点:http://httpd.apache.org/
【主要特点】
1.开放源代码、跨平台应用
2.支持多种网页编程语言
3.模块化设计 、运行稳定、良好的安全性
【软件版本】
1.1.X
*目前最高版本是1.3,运行稳定
- 向下兼容性较好,但缺乏一些较新的功能
2.2.X
-
目前主要包括2.0和2.2两个版本
*具有更多的功能特性
*与1.X相比,配置管理风格存在较大差异
【编译安装的优点】
1.具有较大的自由度,功能可定制
2.可及时获得最新的软件版本
3.普遍适用于大多数Linux版本,便于移植使用
【获得Apache服务器的源码包 】
1.参考地址:http://httpd.apache.org/download.cgi
【安装httpd服务器——编译安装】
【安装httpd服务器——编译安装】
1.准备工作
卸载httpd及相关依赖包
[[email protected] ~]# rpm -e httpd --nodeps
[[email protected] ~]# yum -y install apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre
2.源码编译及安装
解包
[[email protected] ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/httpd-2.4.25/
配置
[[email protected] httpd-2.4.25]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
–prefix:指定将httpd服务程序安装到哪个目录,如/usr/local/httpd。
–enable-so:启用动态加载模块支持,使httpd具备进一步扩展功能的能力。
–enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。
–enable-charset-lite:启动字符集支持,以便支持使用各种字符集编码的网页。
–enable-cgi:启用CGI脚本程序支持,便于扩展网站的应用访问能力。
Httpd的安装目录是由前面的—prefix指定的路径,
默认安装在/usr/local/apache2目录下
编译及安装
[[email protected] httpd-2.4.25]# make
[[email protected] httpd-2.4.25]# make install
3.确认安装结果
[[email protected] ~]# ls /usr/local/httpd/
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
主要目录和文件:
服务目录:/usr/local/httpd/
主配置文件:/usr/local/httpd/conf/httpd.conf
网页目录:/usr/local/httpd/htdocs/
服务脚本:/usr/local/httpd/bin/apachectl
执行程序:/usr/local/httpd/bin/httpd
访问日志: /usr/local/httpd/log/access_log
错误日志: /usr/local/httpd/log/error_log
模块目录:/usr/local/httpd/modules
4.优化执行路径
[[email protected] ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[[email protected] ~]# ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
lrwxrwxrwx 1 root root 30 04-06 13:08 /usr/local/bin/apachectl -> /usr/local/httpd/bin/apachectl
lrwxrwxrwx 1 root root 26 04-06 13:08 /usr/local/bin/httpd -> /usr/local/httpd/bin/httpd
[[email protected] ~]# httpd -v
5.添加httpd系统服务
*以便通过chkconfig进行管理
[[email protected] ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[[email protected] ~]# vi /etc/init.d/httpd
编辑和添加
#!/bin/bash
#chkconfig: 35 85 15
description: Startup script for the Apache HTTP Server
注释:chkconfig识别配置
chkconfig— 服务识别参数,在运行级别3,5中启动。服务启动和关闭顺序分别为85、15.
description----服务描述信息
[[email protected] ~]# chkconfig --add httpd
[[email protected] ~]# chkconfig --list httpd
httpd 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
[[email protected] ~]# ll /etc/rc.d/rc3.d
【 httpd服务器的基本配置】
(一)Web站点部署过程
1.确定网站名称、IP地址
若要向internet中发布一个web站点,需要申请一个合法的互联网公网ip地址,并向dns服务提供商注册一个完整的网站名称。
[[email protected] ~]# cat /etc/sysconfig/network
#Created by anaconda
NETWORKING=yes
HOSTNAME=www.bdqn.com
[[email protected] ~]# cat /etc/hostname
www.bdqn.com
[[email protected] ~]# reboot
2.配置并启动httpd服务
[[email protected] ~]# vi /usr/local/httpd/conf/httpd.conf
200 #ServerName www.example.com:80
201 ServerName www.bdqn.com
[[email protected] ~]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# netstat -anput | grep httpd
3.部署网页文档
[[email protected] ~]# cat /usr/local/httpd/htdocs/index.html
< html >< body >< h1 > It works! < /h1 > < /body > < /html >
4.在客户机中访问Web站点
5.查看Web站点的访问情况
查看访问日志
[[email protected] ~]# tail /usr/local/httpd/logs/access_log
192.168.4.110 - - [06/Apr/2011:14:24:06 +0800] “GET / HTTP/1.1” 200 44
192.168.4.110 - - [06/Apr/2011:14:24:06 +0800] “GET /favicon.ico HTTP/1.1” 404 209
记录客户机的ip地址、访问服务器的日期和时间、请求的网页对象等
查看错误日志(排查服务器运行故障)
[[email protected] ~]# tail /usr/local/httpd/logs/error_log
记录了发生错误的日期和时间、错误事件类型、错误事件的内容描述
若要对web站点进行更加具体、更加强大的配置,需要做进一步配置。
[[email protected] ~]# vim /usr/local/httpd/conf/httpd.conf
【全局配置项】
ServerRoot “/usr/local/httpd”
Listen 80
User daemon
Group daemon
ServerAdmin [email protected]
ServerName www.benet.com
DocumentRoot “/usr/local/httpd/htdocs”
DirectoryIndex index.html index.php
ErrorLog logs/error_log
LogLevel warn
CustomLog logs/access_log common
PidFile logs/httpd.pid
CharsetDefault UTF-8
Include conf/extra/httpd-default.conf
常用的全局配置参数
如果配置文件中指定目录或文件位置不使用绝对路径,则认为在服务器根目录下(apache安装目录/usr/local/httpd)
ServerRoot:服务器根目录。要与DocumentRoot区分开
ServerAdmin:管理员邮箱
User:运行服务的用户身份
Group:运行服务的组身份
ServerName:网站服务器的FQDN(主机名+域名)
DocumentRoot:网页文档的根目录
Listen:监听的IP地址、端口号
PidFile:保存httpd进程PID号的文件
DirectoryIndex:默认的索引页文件
ErrorLog:错误日志文件的位置
CustomLog:访问日志文件的位置
LogLevel:记录日志的级别,默认为warn以上级别
AddDefaultCharset:设置站点中的网页默认使用的字符集编码
Timeout:网络连接超时,默认为300秒
KeepAlive:是否保持连接,可选On或Off
MaxKeepAliveRequests:每次连接最多请求文件数
KeepAliveTimeout:保持连接状态时的超时时间
Include:需要包含进来的其他配置文件
【区域配置项】
使用一对组合标记,限定了配置项的作用范围
定义‘/’目录区域的开始
Options FollowSymLinks 控制选项,允许使用符号连接
AllowOverride none 不允许隐含控制文件中的覆盖位置
Require all denied 禁止任何人访问此区域
定义/’目录区域的结束
Options FollowSymLinks 控制选项,允许使用符号连接
就是允许你的网页文件夹下的链接文件链接到首页目录以外的文件。举例来说,如果你把首页目录设置为/var/www/html,那么你的网页程序最多只能访问到/var/www/html目录,上层目录是不可见的。但是你可以通过链接把文件链接到/var/www/html目录以外的文件以访问该文件,如果FollowSymLinks被设置的话
从安全性考虑,根目录的AllowOverride属性一般都配置成不允许任何Override
Apache Options Indexes FollowSymLinks详解
禁止显示Apache目录列表-Indexes FollowSymLinks
如何修改目录的配置以禁止显示 Apache 目录列表。
缺省情况下如果你在浏览器输入地址:
http://localhost:8080/
如果你的文件根目录里有 index.html,浏览器就会显示 index.html的内容,如果没有 index.html,浏览器就会显示文件根目录的目录列表,目录列表包括文件根目录下的文件和子目录。
同样你输入一个虚拟目录的地址:
http://localhost:8080/b/
如果该虚拟目录下没有 index.html,浏览器也会显示该虚拟目录的目录结构,列出该虚拟目录下的文件和子目录。
如何禁止 Apache 显示目录列表呢?
要禁止 Apache 显示目录结构列表,只需将 Option 中的 Indexes 去掉即可。
比如我们看看一个目录的目录配置:
<Directory “D:/Apa/blabla”>
Options Indexes FollowSymLinks #---------->Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
你只需要将上面代码中的 Indexes 去掉,就可以禁止 Apache 显示该目录结构。用户就不会看到该目录下的文件和子目录列表了。
Indexes 的作用就是当该目录下没有 index.html 文件时,就显示目录结构,去掉 Indexes,Apache 就不会显示该目录的列表了。
第二种方法
解决办法:
1、编辑httpd.conf文件
vi ./conf/httpd.conf
找到如下内容:
?BR> <Directory “C:/Program Files/Apache2.2/htdocs”>
#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named explicitly — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
……
在Options Indexes FollowSymLinks在Indexes前面加上 – 符号。
即: Options -Indexes FollowSymLinks
【备注:在Indexes前,加 + 代表允许目录浏览;加 – 代表禁止目录浏览。】
这样的话就属于整个Apache禁止目录浏览了。
如果是在虚拟主机中,只要增加如下信息就行:
<Directory “D:test”>
Options -Indexes FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
这样的话就禁止在test工程下进行目录浏览。
备注: 切记莫把“Allow from all”改成 “Deny from all”,否则,整个网站都不能被打开。
还有一种方法:
可以在根目录的 .htaccess 文件中输入
<Files *>
Options -Indexes
就可以阻止Apache 将目录结构列表出来。
AllowOverride从字面上解释是允许覆盖的意思,即Apache允许另一配置文件覆盖现有配置文件。
我们通常利用Apache的rewrite模块对URL进行重写,rewrite规则会写在 .htaccess 文件里。但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对.htaccess 所在目录进行配置。
从安全性考虑,根目录的AllowOverride属性一般都配置成不允许任何Override,即:
< Directory />
AllowOverride None
< /Directory>
在 AllowOverride 设置为 None 时, .htaccess 文件将被完全忽略。当此指令设置为 All 时,所有具有 “.htaccess” 作用域的指令都允许出现在 .htaccess 文件中。
而对于 URL rewrite 来说,至少需要把目录设置为:
< Directory /myblogroot/>
AllowOverride FileInfo
< /Directory>
以下是AllowOverride的详细参数:
AuthConfig
允许使用与认证授权相关的指令(AuthDBMGroupFile, AuthDBMUserFile, AuthGroupFile, AuthName, AuthType, AuthUserFile, Require, 等)。
FileInfo
允许使用控制文档类型的指令(DefaultType, ErrorDocument, ForceType, LanguagePriority, SetHandler, SetInputFilter, SetOutputFilter, mod_mime中的 Add* 和 Remove* 指令等等)、控制文档元数据的指令(Header, RequestHeader, SetEnvIf, SetEnvIfNoCase, BrowserMatch, CookieExpires, CookieDomain, CookieStyle, CookieTracking, CookieName)、mod_rewrite中的指令(RewriteEngine, RewriteOptions, RewriteBase, RewriteCond, RewriteRule)和mod_actions中的Action指令。
Indexes
允许使用控制目录索引的指令(AddDescription, AddIcon, AddIconByEncoding, AddIconByType, DefaultIcon, DirectoryIndex, FancyIndexing, HeaderName, IndexIgnore, IndexOptions, ReadmeName, 等)。
Limit
允许使用控制主机访问的指令(Allow, Deny, Order)。
Options[=Option,…]
允许使用控制指定目录功能的指令(Options和XBitHack)。可以在等号后面附加一个逗号分隔的(无空格的)Options选项列表,用来控制允许Options指令使用哪些选项。