WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。

当Web浏览器(客户端)连接到服务器上并请求文件时,服务器将处理该请求并将文件发送到浏览器上。

http:超文本传输协议 hyper text transport protocol

html:超文本标记语言 Hyper text markup languade 是用于描述网页文档的一种标记语言。

网页文件本身是一种文本文件,通过在文本文件中添加标记符,可以告诉浏览器如何显示其中的内容

(如:文字如何处理,画面如何安排,图片如何显示等)。

www 万维网 WWW(WORLD WIDE WEB)

常见的web服务器

   APACHE

   IIS

   nginx

   Lighttpd

http://www.netcraft.com


使用端口:80

协议端口号用正整数标识,范围在0到65535之间。0-1023之间的端口为知名端口,这些端口一般固定分配给一些服务,比如21端口分配给FTP服务,23端口分配给TELNET,80端口分配给HTTP服务;而1024到65535之间的端口为动态分配端口,这些端口一般不固定分配给某个服务,也就是说许多服务都可以使用这些端口。只要运行的程序向系统提出访问网络的申请,那么系统就可以从这些端口号中分配一个共该程序使用,在关闭程序进程后,就会释放所占用的端口号。

APACHE 的优点:

1、免费

2、稳定性

3、扩展性 支持多操作系统

4、开放性 二次开发

5、支持多种语言,php,jsp,asp

Apache历史

1994 美国伊利诺大学国家超级计算中心 NCSA  httpd


具体实施:

1,装包

#yum install httpd httpd-manual –y

# rpm -ql httpd


/etc/httpd             配置文件和模块,日志的

/etc/httpd/conf           配置文件的目录

/etc/httpd/conf.d 包含的配置文件目录,子配置文件

/etc/httpd/conf.d/welcome.conf      子配置文件

/etc/httpd/conf/httpd.conf       配置文件

/etc/httpd/logs                   日志

/etc/httpd/modules            模块

/etc/rc.d/init.d/httpd          启动脚本

/var/log/httpd                    日志

/var/www                   apache的家目录

/var/www/cgi-bin              发布cgi脚本的目录

/var/www/error                  错误信息提示

/var/www/html                  页面发布的目录

/usr/bin/ab                   用来测速的

/usr/bin/htpasswd              生成访问用户和密码


2,配置

[root@wwwconf]#pwd

/etc/httpd/conf


57ServerRoot"/etc/httpd"     服务器配置的路径

63PidFilerun/httpd.pid  进程号

134Listen80                    监听端口

210Includeconf.d/*.conf       包含的子配置文件

231Userapache                用户

232Groupapache            

281DocumentRoot"/var/www/html"   对外提供的文档所在目录树的根。网页页面保存目录

291 <Directory />                 默认目录容器设置

292     OptionsFollowSymLinks

293    AllowOverride None

294 </Directory>


这个 / 是虚拟的,真实的路径是"/var/www/html"


306<Directory"/var/www/html">

320Options Indexes FollowSymLinks  选项

             indexes  支持索引

             FollowSymLinks 支持符号链接

327AllowOverride None  不使用验证

332Orderallow,deny 访问控制,allowdeny写到前面的优先

333Allow from all            具体的控制,all是所有的

                               ip地址allowfrom192.168.0.8

               网段      allowfrom192.168.0.0/24

               域   allowfrom.uplooking.com      

335</Directory>


实验:

1,发布页面:

[root@wwwhtml]#pwd

/var/www/html

[root@wwwhtml]#vimindex.html

[root@wwwhtml]#catindex.html

helloall!!!

结合DNS

为什么会有redhat的欢迎页面?

# cd /etc/httpd/conf.d/

# vim welcome.conf

#<LocationMatch"^/+$">

#   Options -Indexes

#   ErrorDocument 403 /error/noindex.html

#</LocationMatch>


2,修改端口

      80-->8080


http://www.up.com:8080/

3,自定义网络空间

[root@wwwhtml]#pwd

/var/www/html

[root@wwwhtml]#mkdirteacher

[root@wwwconf]#vimhttpd.conf


337<directory"/var/www/html/teacher">

338optionsindexes

339orderallow,deny

340allowfromall

341</directory>


[root@wwwhtml]#/etc/init.d/httpdrestart


[root@wwwhtml]#cdteacher/

[root@wwwteacher]#pwd

/var/www/html/teacher

[root@wwwteacher]#vimindex.html

helloteacher!!!


http://192.168.0.8:8080/teacher/


======================

optionsindexesFollowSymLinks

options-indexes-FollowSymLinks

添加-,表示取消该功能

符号链接:软连接,windows下的快捷方式

#ln -s /mnt/cdrom/Server /var/www/html/iso

配置一下yum

Baseurl=http://IP/iso    IP  写上本机的ip地址

======================


支持验证:

337<directory"/var/www/html/teacher">

338optionsindexesFollowSymLinks

339AllowOverride all  开启验证

340authname"test"   名字,任意

341authtype basic     类型,basic

342authuserfile "/var/www/html/teacher/.htpasswd"验证文件保存路径

343require valid-user       有效的用户-保存在文件中的用户

344order allow,deny

345allow from all

346</directory>


生成保存用户和密码的文件

[root@wwwteacher]#htpasswd -c .htpasswd u1

Newpassword:

Re-typenewpassword:

Addingpasswordforuseru1

第二次生成的不要加-c参数

[root@wwwteacher]#htpasswd .htpasswd u9

Newpassword:

Re-typenewpassword:

Addingpasswordforuseru9


[root@wwwteacher]#ls-A

abc boot group group1 .htpasswd passwd

[root@wwwteacher]#cat.htpasswd

u1:OGUc29lcJ255w

u9:Dh.vDCiu5dAkY


u1用户,系统里有;u9没有

实验的结果表明这里的用户可以不是系统用户


2)使用其他的文件实现验证


398AccessFileName .htaccess


#pwd

/var/www/html


#vim .htaccess

authname "hello"

authtype basic

authuserfile"/var/www/html/.htpasswd"

require valid-user


#htpasswd -c .htpasswd a1

Newpassword:

Re-typenew password:

Addingpassword for user a1


重启服务就可以了。

自定义一个目录容器:


<directory"/var/www/html/123">

       options indexes

       authname "456"

       authtype basic

       authuserfile"/var/www/html/123/.ht"

       require valid-user

       order allow,deny

       allow from all

</directory>


---------------------


访问控制:

       order allow,deny

       allow from all


allow允许

deny  拒绝


allow,deny:以allow指定的客户端能够访问,没有允许的全部deny-拒绝访问

deny,allow:以deny指定的客户端拒绝访问,没有拒绝的客户端allow-都能访问


黑名单:

      order deny,allow

      deny from 拒绝的客户端


拒绝的客户端:

       ip : 172.16.1..2

   网段:172.16.0.0/16

       FQDN: www.baidu.com

   区域:.baidu.com

      all

白名单:

      order allow,deny

      allow from 允许的客户端


允许的客户端 == 拒绝的客户端


例子:

1)黑名单:

       order deny,allow

       deny from 127.0.0.1 172.16.23.7

只有本机和172.16.23.7不可以访问


2)白名单

      order allow,deny

       allow from 127.0.0.1 172.16.23.7

只有本机和172.16.23.7可以访问


----------------------------------------------------------


个人空间:

359<IfModulemod_userdir.c>

365     #    UserDir disable            默认是关闭的

372           UserDir public_html

374</IfModule>

默认是关闭的,注释365,打开372,开启这个功能。


到一个系统用户的家目录:

[root@www uu]#pwd

/home/uu

[root@www uu]#mkdir public_html


[root@www conf]#vim httpd.conf

<directory"/home/uu/public_html">

           options indexes

           order allow,deny

           allowfrom all

</directory>


[root@www uu]#chmod 705 /home/uu/

[root@www uu]#cd public_html/

[root@www public_html]#vim index.html


访问:http://192.168.0.8/~uu/

---------------------------------------------------------------

定义默认首页的名字

406 Directory Index    [abc]    index.html    index.html.var


错误日志

486ErrorLoglogs/error_log

[root@www httpd]#ls

access_logaccess_log.2error_logerror_log.2

access_log.1access_log.3error_log.1error_log.3

[root@www httpd]#pwd

/var/log/httpd


--------------------------------------------------------------------


CGI(CommonGateway Interface)HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上。


发布cgi动态脚本,只要实现发布的功能就ok

578ScriptAlias/cgi-bin/"/var/www/cgi-bin/"


584<Directory"/var/www/cgi-bin">

585    AllowOverrideNone

586    OptionsNone

587    Orderallow,deny

588    Allowfromall

589</Directory>


[root@www cgi-bin]#pwd

/var/www/cgi-bin

[root@www cgi-bin]#vimt.cgi

#!/bin/bash

echo "content-type:text/html"

echo ""

echo "<html>"

echo "<h1>thisiscgi-bin!</center></h1>"

echo "<hr>"

echo "</html>"

[root@wwwcgi-bin]#chmod+xt.cgi

http://192.168.9.8/cgi-bin/t.cgi

-------------------------------------------------------------------


错误消息提示:

827#Someexamples:

828#ErrorDocument500"Theservermadeabooboo."

829#ErrorDocument404/missing.html

830#ErrorDocument404"/cgi-bin/missing_handler.pl"

831#ErrorDocument402http://www.example.com/subscription_info.html

851Alias/error/"/var/www/error/"

866#ErrorDocument400/error/HTTP_BAD_REQUEST.html.var

867#ErrorDocument401/error/HTTP_UNAUTHORIZED.html.var

868#ErrorDocument403/error/HTTP_FORBIDDEN.html.var

869#ErrorDocument404/error/HTTP_NOT_FOUND.html.var

870#ErrorDocument405/error/HTTP_METHOD_NOT_ALLOWED.html.var

871#ErrorDocument408/error/HTTP_REQUEST_TIME_OUT.html.var

872#ErrorDocument410/error/HTTP_GONE.html.var

873#ErrorDocument411/error/HTTP_LENGTH_REQUIRED.html.var

874#ErrorDocument412/error/HTTP_PRECONDITION_FAILED.html.var

875#ErrorDocument413/error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var

876#ErrorDocument414/error/HTTP_REQUEST_URI_TOO_LARGE.html.var

877#ErrorDocument415/error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var

878#ErrorDocument500/error/HTTP_INTERNAL_SERVER_ERROR.html.var

879#ErrorDocument501/error/HTTP_NOT_IMPLEMENTED.html.var

880#ErrorDocument502/error/HTTP_BAD_GATEWAY.html.var

881#ErrorDocument503/error/HTTP_SERVICE_UNAVAILABLE.html.var

882#ErrorDocument506/error/HTTP_VARIANT_ALSO_VARIES.html.var


853Alias/error/"/var/www/error/"


定义一个目录的错误提示信息:

[root@wwwconf]#vimhttpd.conf

337<directory"/var/www/html/teacher">

338optionsindexesFollowSymLinks

339errordocument403/error/403.html

340errordocument404/error/404.html

      ......

348</directory>


[root@wwwerror]#pwd

/var/www/error

[root@wwwerror]#vim403.html

[root@wwwerror]#vim404.html

--------------------------------------------------------------------------


虚拟主机:

虚拟主机是在网络服务器上划分出一定的磁盘空间供用户放置站点、应用组件等,提供必要的站点功能、数据存放和传输功能。所谓虚拟主机,也叫“网站空间”,就是把一台运行在互联网上的服务器划分成多个“虚拟”的服务器,每一个虚拟主机都具有独立的域名和完整的Internet服务器(支持WWWFTPE-mail等)功能。

      1,基于ip

单网卡多ip


# ifconfig eth0:0 192.168.9.3 netmask 255.255.0.0


# mkdir -pv /tmp/{aa,bb}

mkdir: created directory `/tmp/aa'

mkdir: created directory `/tmp/bb'

创建两个目录,作为虚拟主机的documentroot


<VirtualHost 192.168.9.8:80>

   ServerAdmin root@localhost 管理员邮箱

   DocumentRoot /tmp/aa                页面发布目录

   ServerName www.aa.com           FQDN

   ErrorLog logs/aa.com-error_log   错误日志

   CustomLog logs/aa.com-access_log common 访问日志

      <directory "/tmp/aa">

              options indexes

              order allow,deny

              allow from all

      </directory>

</VirtualHost>


<VirtualHost 192.168.9.3:80>

   ServerAdminroot@localhost

   DocumentRoot/tmp/bb

   ServerNamewww.bb.com

   ErrorLoglogs/bb.com-error_log

   CustomLoglogs/bb.com-access_log common

      <directory "/tmp/bb">

              options indexes

              order allow,deny

              allow from all

      </directory>

</VirtualHost>


# echo "hello aa" > /tmp/aa/index.html

# echo "hello bb" > /tmp/bb/index.html


编辑这个文件,代替DNS正向解析:

[root@t1 u1]# vim /etc/hosts

192.168.9.8      www.aa.com

192.168.9.3     www.bb.com


测试的结果是:

firefox http://www.aa.com

              helloaa


firefox http://www.bb.com

              hellobb


     2,基于端口

使用一个没有被占用的端口 vim /etc/service

# vim httpd.conf

Listen 80

listen 8080             需要声明


<VirtualHost 192.168.9.8:80>

内容同上

</VirtualHost>


<VirtualHost 192.168.9.8:8080>

内容同上

</VirtualHost>


修改DNS的配置:

[root@t1 u1]# cat /etc/hosts

192.168.9.8     www.aa.com

192.168.9.8     www.bb.com


测试的结果是:

firefox http://www.aa.com:80

              helloaa


firefox http://www.bb.com:8080

              hellobb




     3,基于域名

借助DNS

[root@www u1]#cd/var/named/chroot/etc/

[root@wwwetc]#ls

localtime named.conf rndc.key

[root@www etc]#vimnamed.conf

zone"a.com"{

typemaster;

file"a.com.zone";

};

zone"b.com"{

typemaster;

file"b.com.zone";

};


[root@www etc]#cd../var/named/


[root@www named]#cp cba.com.zonea.com.zone

[root@www named]#cpcba.com.zoneb.com.zone

[root@www named]#vima.com.zone

IN NS @

      www IN  A 192.168.9.8

[root@wwwnamed]#vimb.com.zone

IN NS @

      www IN A1 92.168.9.8

[root@www named]#/etc/init.d/namedrestart


测试,解析的结果都是自己的ipDNS任务完成。


[root@wwwconf]#pwd

/etc/httpd/conf

[root@wwwconf]#vimhttpd.conf

988NameVirtualHost*:80    打开这一行的注释,表示我要作基于域名的虚拟主机


1008<VirtualHost*:80>

1009ServerAdminroot@up.com

1010DocumentRoot/var/www/html/a

1011ServerNamewww.a.com

1012ErrorLoglogs/a.com-error_log

1013CustomLoglogs/a.com-access_logcommon

1014<directory"/var/www/html/a">

1015optionsindexes

1016orderallow,deny

1017allowfromall

1018</directory>

1019</VirtualHost>

1020<VirtualHost*:80>

1021ServerAdminroot@up.com

1022DocumentRoot/var/www/html/b

1023ServerNamewww.b.com

1024ErrorLoglogs/b.com-error_log

1025CustomLoglogs/b.com-access_logcommon

1026<directory"/var/www/html/b">

1027optionsindexes

1028orderallow,deny

1029allowfromall

1030</directory>

1031</VirtualHost>


[root@wwwhtml]#pwd

/var/www/html

[root@wwwhtml]#mkdirab

[root@wwwhtml]#cda

[root@wwwa]#vimindex.html

[root@wwwa]#cd../b

[root@wwwb]#vimindex.html



3,启动

[root@wwwhtml]#/etc/init.d/httpdrestart




4,测试


[root@wwwhtml]#vim/etc/hosts

      192.168.9.8www.a.com

  192.168.9.8    www.b.com

http://www.a.com/

http://www.b.com/


压力测试:

[root@www conf]#ab -n 1000 -c 100 http://192.168.9.8/


Timetakenfortests:    0.648163seconds  本次测试花费的时间

Totaltransferred:      287430 bytes   本次测试的总的网络流量

Requestspersecond:    1542.82[#/sec](mean)   吞吐量

Timeperrequest:    64.816[ms](mean)    客户端的等待时间

Timeperrequest:    0.648[ms]           服务器的处理时间

ransferrate:    431.99[Kbytes/sec]received


Percentageoftherequestsservedwithinacertaintime(ms)

50%    61

66%    63

75%    66

80%    68

90%    72

95%    74

98%    77

99%    84

100%    90

--------------------------------------------------------------