一、Apache项目简介
源于A Patchy Server,著名的开源Web服务,由ASF负责开发维护
官方站点:http://httpd.apache.org/
优点:开放源代码,跨平台应用
支持多种网页编程语言
模块化设计、运行稳定、良好的安全性
主要软件包:
httpd:Apache HTTP Server的服务程序包
httpd-manual:网页版手册文档(提供了版本说明、用户指南、配置向导等相关链接)
httpd-devel:开发工具
system-config-httpd:图形化配置工具
二、安装软件包
[root@localhost ~]# yum -y install httpd httpd-manual httpd-devel
直接启动服务即可访问默认站点
[root@localhost ~]# service httpd start
在客户端浏览器输入httpd服务器的域名或IP地址查看是否生效
当网站根目录下没有index.html等首页文件时,本来会出现代码为403的拒绝访问错误,但是RHEL 5 启用了welcome.conf配置,当遇到缺少索引文件的403错误时改成显示默认欢迎页
注意:若已有其他程序占用80端口,则启动httpd时可能报错“Address already in use: make_sock: could not bind to address [::]:80”,需要先禁用对应的程序后重新启动httpd服务。
比较常用的一些目录和文件位置如下所述
服务程序: /usr/sbin/httpd
系统服务脚本:/etc/rc.d/init.d/httpd
服务根目录:/etc/httpd/
主配置文件:/etc/httpd/conf/httpd.conf
配置目录:/etc/httpd/conf.d/
网站根目录:/var/www/html/
模块目录:/etc/httpd/modules/
访问日志:/var/log/httpd/access_log
错误日志:/var/log/httpd/error_log
主配置文件
/etc/httpd/conf/httpd.conf
常用全局设置参数
格式:配置参数 值
ServerName:本站点的FQDN名称 265行
DocumentRoot:网页文档的根目录 281行
DirectoryIndex:默认索引页/首页文件 391行
ErrorLog:错误日志文件的位置 472行
CustomLog:访问日志文件的位置 501行
Listen:监听服务的IP地址、端口号 134行
PidFile:保存httpd进程PID号的文件 63行
ServerRoot:服务目录 57行
ServerAdmin:管理员邮箱 251行
User:服务进程的属主,默认为apache 231行
Group:服务进程的属组,默认为apache 232行
Timeout:网络连接超时,单位秒 68行
KeepAlive:是否保持连接,可选On或Off 74行
MaxKeepAliveRequests:每次连接最多处理的请求数 81行
KeepAliveTimeout:保持连接的超时时限 87行
Include:可包含其他子配置文件 210行
若行数不准,可用关键字查找
建立测试网页,内容为“This is a test page !!!”
[root@localhost ~]# vim /var/www/html/test.html
<h1>This is a test page !!!</h1>
将首页文件更改为test.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex test.html
重启服务
[root@localhost ~]# service httpd restart
在客户端浏览器验证
输入域名,输出结果应为“This is a test page !!!”
在客户端访问可使用elinks浏览器访问结合“--dump”导出
[root@server1 ~]# elinks --dump http://192.168.10.10
This is a test page !!!
若无法访问,输入服务器IP,能访问则证明DNS有问题
若用IP也无法访问,则需查看网络
三、httpd服务的访问控制
为了更好地控制对网站资源的访问,可以为特定网站目录添加访问授权。无论是客户机地址限制,还是 基于用户名/密码的限制,都需要在<Directory 目录> ..</Directory>配置段内来指定。
1、基于客户机IP地址的访问控制
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Order allow,deny //顺序为先允许、后拒绝(未明确允许则拒绝)与“deny,allow”相反
Allow from 192.168.10.0/24 //允许访问的网段 也可用“Deny from”定义拒绝访问网段
</Directory>
注:正常情况下,允许和拒绝只写一个就可以,若都写则Order顺序在后的生效
在客户端访问可使用elinks浏览器访问结合“--dump”导出
[root@server1 ~]# elinks --dump http://192.168.10.10
This is a test page !!!
2、基于用户名/密码的访问控制
基于用户的访问控制包含认证和授权两个过程,认证(Authentication)是指识别用户身份的过程,授权(Authorization)是允许特定用户访问目录的过程
创建用户认证数据文件
使用htpasswd工具来建立用户数据文件,选项“-c”选项表示新建,无选项则用来修改账号的密码。以下操作将添加一个名为jack的网站用户,新建的数据文件存放到/etc/httpd/.htpasswd:
[root@localhost ~]# htpasswd -c /etc/httpd/.htpasswd jack
New password: 输入密码
Re-type new password: 重复输入密码
Adding password for user jack 创建成功
创建测试网页
[root@localhost ~]# echo "<h1>Hello World</h1>">/var/www/html/authdir/test.html
用户授权
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/authdir">
AuthName "Need Authorization!!" 认证领域名称,用于弹窗提示
AuthType basic 认证类型
AuthUserFile /etc/httpd/.htpasswd 用户数据文件路径
Require valid-user 指定授权用户或组
</Directory>
[root@www authdir]# service httpd restart
在客户端用浏览器验证,此时需要输入用户名及密码,输出结果应为“Hello World”
本文出自 “sjju” 博客,请务必保留此出处http://857398758.blog.51cto.com/8504117/1369552
Apache服务--Linux5,布布扣,bubuko.com
原文:http://857398758.blog.51cto.com/8504117/1369552