Web 网站服务

时间:2023-03-08 22:26:20
Web 网站服务

Apache 简介

Apache HTTP Server(简称Apache)是开放源码的网页服务器,基于标准的HTTP网站协议提供网页浏览服务,在Web服务器领域中长期保持着超过半数的份额。Apache服务器可以运行在Linux,UNIX,Windows等多种操作系统平台中

 

Apache的起源

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系统平台上)以及它的可移植性等方面。

 

Apache的主要特点

开放源代码、跨平台应用

支持多种网页编程语言:包括Perl,PHP,Python。Java等

模块化设计 、运行稳定、良好的安全性

软件版本

1.X

n   目前最高版本是1.3,运行稳定

n   向下兼容性较好,但缺乏一些较新的功能

2.X

n   目前主要包括2.0和2.2两个版本

n   具有更多的功能特性

n   与1.X相比,配置管理风格存在较大差异

安装httpd服务器

安装httpd服务器

环境描述:

操作系统

IP地址

主机名

软件包列表

CentOS 6.5_x84_64

192.168.6.10

Crushlinux

httpd-2.2.17.tar.gz

配置过程:

 

准备工作:

为了避免发生端口冲突,程序冲突等现象,建议卸载使用rpm方式安装的httpd

[root@crushlinux ~]# rpm -e httpd --nodeps

关闭防火墙和selinux

[root@crushlinux httpd-]# service iptables stop
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:                                   [确定]
[root@crushlinux httpd-]# setenforce 

源码编译及安装

解包

[root@crushlinux /]# tar zxf httpd-.tar.gz -C /usr/src/
[root@crushlinux /]# cd /usr/src/httpd-/

配置

[root@crushlinux httpd-]# ./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脚本程序支持

Web 网站服务

编译完成后,如上图,说明已经编译成功,如果出现以下错误

 Web 网站服务

则在编译时入加 --with-included-apr 即可解决

编译及安装

使用命令make将源代码转换为可执行的程序,然后执行make install完成最后的安装过程

[root@crushlinux httpd-]# make
[root@crushlinux httpd-]# make install

优化执行路径

[root@crushlinux httpd-]# ln -s /usr/local/httpd/bin/* /usr/local/bin/

添加httpd系统服务

[root@crushlinux httpd-]# cp /usr/local/httpd/bin/apachectl   /etc/init.d/httpd
[root@crushlinux httpd-]# vim /etc/init.d/httpd
.#!/bin/sh
.#chkconfig:
.#description:http apache

解释:

    345 66 88表示345运行级别是开启的,66为服务启动顺序,88服务为停止顺序

[root@crushlinux httpd-]# chkconfig --add httpd
//将httpd添加为系统服务
[root@crushlinux httpd-]# chkconfig --list httpd
//查看httpd服务的自启动状态
httpd              :关闭    :关闭    :关闭    :启用    :启用    :启用    :关闭
成功执行以上操作后,linux系统每次进入运行级别3,5时,httpd服务将会自动运行!

配置并启动服务

编辑主配置文件httpd.conf
#ServerName www.example.com:
 ServerName www.benet.com

检查语法错误

[root@crushlinux httpd-]# /usr/local/httpd/bin/apachectl -t
Syntax OK

启动httpd服务

[root@crushlinux httpd-]# /etc/init.d/httpd start
[root@crushlinux httpd-]# netstat -anpt | grep httpd
tcp               :::                       :::*                        LISTEN      /httpd    

默认监听TCP的80端口

赋权

[root@crushlinux httpd-]# chmod +x /etc/init.d/httpd

启动服务

[root@crushlinux httpd-]# 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
         #ServerName www.example.com:
         ServerName localhost:

在客户机中访问Web站点

Web 网站服务

httpd服务的访问控制

 

作用: 控制对网站资源的访问

为特定的网站目录添加访问授权

 

常用访问控制方式:

客户机地址限制

用户授权限制

客户机地址限制

 Order配置项,定义控制顺序    Order   用来定义访问控制顺序

Order deny,allow   #先拒绝,后允许

 Deny from all   #拒绝所有

Order allow,deny   #先允许,后拒绝

Allow from all   #允许所有

用户地址限制

Allow from  地址    #允许某些地址访
Deny  from  地址    #拒绝某些地址访问

    地址类型:IP地址 网络地址 主机名 域名 all(表示所有)  多个地址之间使用地址限制实例
vim /usr/local/httpd/conf/httpd.conf
     Order allow,deny
     Allow  ##只允许10.0网段访问网页

再次访问网页,权限拒绝。地址限制成功

Web 网站服务

用户授权限制

创建用户认证数据库文件

[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-]# cd /usr/local/httpd/conf/
[root@crushlinux conf]# cat .a
a:urPHogTpjMASI

添加用户授权

编辑httpd主配置文件

 

143行加入以下内容

         AuthName "http web"
         AuthType Basic
         AuthUserFile /usr/local/httpd/conf/.a
         require valid-user

再次访问网页,输入用户名和密码即可访问

Web 网站服务Web 网站服务

注:如果报错:httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName(找不到完整域名)编辑主配置文件将#ServerName www.benet.com80前的#号注释掉重启服务即可

构建虚拟Web主机

在同一台服务器中运行多个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地址
<VirtualHost 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
</VirtualHost>
<VirtualHost 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
</VirtualHost>

修改主配置文件

还原一下配置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.10 www.accp.com

测试虚拟用户配置

[root@crushlinux conf]# curl http://www.accp.com
<h1>accp</h1>
[root@crushlinux conf]# curl http://www.benet.com
<h1>benet</h1>

显示以上结果为虚拟用户配置成功

 

 

配置基于IP的虚拟主机

给Eth1网卡添加IP

[root@crushlinux 桌面]# ifconfig eth1: 192.168.6.11

Web 网站服务

<VirtualHost 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
<VirtualHost 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

验证结果:

Web 网站服务Web 网站服务

配置基于端口的虚拟主机
<VirtualHost >
……………
</VirtuealHost>
<VirtualHost >
……………….
</VirtuealHost>

在httpd.conf中加入以下配置
Include conf/extra/httpd-vhosts.conf
Listen
Listen