Apache HTTPServer(简称 Apache)是开放源码的网页服务器,基于标准的 HTTP 网站协议提供网页浏览服务,在 web 服务器领域中长期保持着超过半数的份额。Apache 服务器可以运行在 Linux,UNIX,Windows 等多种操作系统平台中
Apache 源于 NCSAhttpd 服务器,经过多次修改,成为世界上最流行的 Web 服务器软件之一。Apache 取自 "a patchy server" 的读音,意思是充满补丁的服务器,因为它是自由软件,所以不断有人来为它开发新的功能、新的特性、修改原来的缺陷。Apache 的特点是简 单、速度快、性能稳定,并可做代理服务器来使用。 本来它只用于小型或试验 Internet 网络,后来逐步扩充到各种 Unix 系统中, 尤其对 Linux 的支持相当完美。Apache 有多种产品,可以支持 SSL 技术,支持多个虚拟主机。Apache 是以进程为基础的结构,进程要比线程消耗 更多的系统开支,不太适合于多处理器环境,因此,在一个 Apache Web 站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。到目前为止 Apache 仍然是世界上用的最多的 Web 服务器,市场占有率达 60% 左 右。世界上很多著名的网站如 Amazon.com、Yahoo!、W3 Consortium、Financial Times 等都是 Apache 的产物,它的成功之处主要在于它的源代码开放、有一支开放的开发队伍、支持跨平台的应用(可以运行在几乎所有的 Unix、 Windows、Linux 系统平台上)以及它的可移植性等方面。
开放源代码、跨平台应用
支持多种网页编程语言:包括 Perl,PHP,Python。Java 等
模块化设计 、运行稳定、良好的安全性
1.X
n 目前最高版本是 1.3,运行稳定
n 向下兼容性较好,但缺乏一些较新的功能
2.X
n 目前主要包括 2.0 和 2.2 两个版本
n 具有更多的功能特性
n 与 1.X 相比,配置管理风格存在较大差异
操作系统 |
IP 地址 |
主机名 |
软件包列表 |
CentOS 6.5_x84_64 |
192.168.6.10 |
Crushlinux |
httpd-2.2.17.tar.gz |
- [root@crushlinux ~]# rpm -e httpd --nodeps
- [root@crushlinux httpd-2.2.17]# service iptables stop
- iptables:将链设置为政策 ACCEPT:filter [确定]
- iptables:清除防火墙规则: [确定]
- iptables:正在卸载模块: [确定]
- [root@crushlinux httpd-2.2.17]# setenforce0
- [root@crushlinux /]# tar zxf httpd-2.2.17.tar.gz -C /usr/src/
- [root@crushlinux /]# cd /usr/src/httpd-2.2.17/
- [root@crushlinux httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
--prefix:指定将程序安装到哪个目录下
--enable-so:启用动态加载模块支持
--enable-rewrite:启用网页地址重写功能
--enable-charset-lite:启动字符集支持
--enable-cgi:启用 CGI 脚本程序支持
编译完成后,如上图,说明已经编译成功,如果出现以下错误
则在编译时入加 --with-included-apr 即可解决。
使用命令 make 将源代码转换为可执行的程序,然后执行 make install 完成最后的安装过程
- [root@crushlinux httpd-2.2.17]# make
- [root@crushlinux httpd-2.2.17]# make install
- [root@crushlinux httpd - 2.2.17]#ln - s / usr / local / httpd / bin
- /* /usr/local/bin/*/
- [root@crushlinux httpd-2.2.17]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
- [root@crushlinux httpd-2.2.17]# vim /etc/init.d/httpd
- 1.#!/bin/sh
- 2.#chkconfig:345 66 88
- 3.#description:http apache
解释:
345 66 88 表示:345 运行级别是开启的,66 为服务启动顺序,88 服务为停止顺序
- [root@crushlinux httpd-2.2.17]# chkconfig --add httpd
- //将httpd添加为系统服务[root@crushlinux httpd-2.2.17]# chkconfig --list httpd
- //查看httpd服务的自启动状态httpd0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭
- 成功执行以上操作后,linux系统每次进入运行级别3,5时,httpd服务将会自动运行!
- 编辑主配置文件httpd.conf
- #ServerName www.example.com:80
- ServerName www.benet.com
- [root@crushlinux httpd-2.2.17]# /usr/local/httpd/bin/apachectl -t
- Syntax OK
- [root@crushlinux httpd-2.2.17]# /etc/init.d/httpd start
- [root@crushlinux httpd-2.2.17]# netstat -anpt | grep httpd
- tcp 0 0:::80:::* LISTEN39980/httpd
- [root@crushlinux httpd-2.2.17]# chmod +x /etc/init.d/httpd
- [root@crushlinux httpd-2.2.17]# service httpd start
- 报错1:httpd: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName解决:vim /usr/local/httpd/conf/httpd.conf
- 97#ServerName www.example.com:80
- 98ServerName localhost:80
作用: 控制对网站资源的访问
为特定的网站目录添加访问授权
常用访问控制方式:
客户机地址限制
客户机地址限制
Order 配置项,定义控制顺序 Order 用来定义访问控制顺序
Order deny,allow #先拒绝,后允许
Deny from all #拒绝所有
Order allow,deny #先允许,后拒绝
Allow from all #允许所有
- Allowfrom 地址 #允许某些地址访
- Deny from 地址 #拒绝某些地址访问
- 地址类型:IP地址 网络地址 主机名 域名 all(表示所有) 多个地址之间使用地址限制实例
- vim /usr/local/httpd/conf/httpd.conf
- 156 Order allow,deny
- 157Allowfrom 192.168.6.0#将ALL改为192.168.6.0##只允许10.0网段访问网页
再次访问网页,权限拒绝。地址限制成功
创建用户认证数据库文件
[root@crushlinux httpd-2.2.17]# /usr/local/httpd/bin/htpasswd -c /usr/local/httpd/conf/.a a
New password:
Re-type new password:
Adding password for user a
.a 认证的用户数据文件(是隐藏文件)a 创建的用户
- [root@crushlinux httpd-2.2.17]# cd /usr/local/httpd/conf/
- [root@crushlinux conf]# cat .a
- a:urPHogTpjMASI
添加用户授权
编辑 httpd 主配置文件
在 143 行加入以下内容
- 144AuthName"http web"
- 145 AuthType Basic
- 146AuthUserFile /usr/local/httpd/conf/.a
- 147require valid-user
再次访问网页,输入用户名和密码即可访问
注:如果报错:httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName(找不到完整域名)编辑主配置文件将 #ServerNamewww.benet.com:80 前的# 号注释掉重启服务即可
在同一台服务器中运行多个 Web 站点,其中每一个站点并不独立占用一台真正的计算机
httpd 支持的虚拟主机类型:
基于域名的虚拟主机
基于 IP 地址的虚拟主机
基于端口的虚拟主机
创建虚拟用户的网页根目录
- [root@crushlinux /]# cd /usr/local/httpd/htdocs/
- [root@crushlinux /]# mkdir benetcom
- [root@crushlinux /]# cd benetcom
- [root@crushlinux /]# echo"<h1>benet</h1>"> index.html
- [root@crushlinux /]# cd ..
- [root@crushlinux /]# mkdir accpcom
- [root@crushlinux /]# cd accptcom
- [root@crushlinux /]# echo"<h1>accp</h1>"> index.html
注:如果报错且无法实现则将
AuthName "http web"
AuthType Basic
AuthUserFile /usr/local/httpd/conf/.a
require valid-user
用 #注释掉即可
修改虚拟用户的配置文件
- [root@crushlinux /]# cd /usr/local/httpd/conf/extra/
- vim httpd-vhosts.conf[root@crushlinux /]# vim httpd-vhosts.conf #更改配置文件,修改红色部分内容
- NameVirtualHost 192.168.6.10 #更改19行 更改为web服务器(本机)的IP地址
- 192.168.6.10>
- DocumentRoot /usr/local/httpd/htdocs/benetcom/
- ServerName www.benet.com
- ErrorLog "logs/benet-error_log"
- CustomLog "logs/benet-access_log" common
- 192.168.6.10>
- DocumentRoot /usr/local/httpd/htdocs/accpcom/
- ServerName www.accp.com
- ErrorLog "logs/accp-error_log"
- CustomLog "logs/accp-access_log" common
修改主配置文件
还原一下配置 httpd 的原有配置。
- [root@crushlinux /]# vim /usr/local/httpd/conf/httpd.conf
#在最后一行添加以下内容,加载外部的虚拟用户的配置文件
Include conf/extra/httpd-vhosts.conf
重启服务
- [root@crushlinux /]# service httpd restart
配置完虚拟主机后 重启报错:
[warn] NameVirtualHost *:80 has no VirtualHosts
解决:
vim httpd-vhosts.conf
将 19 行注释掉
#NameVirtualHost *:80
修改 hosts 文件
- vim /etc/hosts #添加以下内容
- 192.168.6.10 www.benet.com
- 192.168.6.10www.accp.com
测试虚拟用户配置
- [root@crushlinux conf]# curl http://www.accp.com
- accp
- [root@crushlinux conf]# curl http://www.benet.com
- benet
显示以上结果为虚拟用户配置成功
给 Eth1 网卡添加 IP
- [root@crushlinux 桌面]# ifconfig eth1:1 192.168.6.11
- 192.168.6.10>
- DocumentRoot /usr/local/httpd/htdocs/benetcom/
- ServerName www.benet.com
- ErrorLog "logs/benet-error_log"
- CustomLog "logs/benet-access_log" common
- 192.168.6.11>
- DocumentRoot /usr/local/httpd/htdocs/benetcom/
- ServerName www.accp.com
- ErrorLog "logs/benet-error_log"
- CustomLog "logs/benet-access_log"common
验证结果:
- 192.168.6.10:81>
- ……………
- 192.168.6.11:81>
- ……………….
- 在httpd.conf中加入以下配置
- Include conf/extra/httpd-vhosts.conf
- Listen 192.168.6.10:81
- Listen 192.168.6.11:82
来源: http://www.cnblogs.com/crushlinux/p/6749071.html