Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1114469
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2010-08-25 15:28:08

RHCE学习笔记

 

下面是关于linux下面配置HTTP服务的讨论,

 

HTTP介绍

HTTP的全名为Hyper Text Transfer Protocol(超文本传输协议)

linux下面实现web服务,通常使用Apache来实现,Apache一直是Internet上面最流行的web服务器,web服务器的架设,在linux下面有个很著名的架构叫lamplinux+apache+mysql+php

 

下面是关于HTTP这个服务的属性

HTTP的相关软件包

Httpd 

HTTP的守护进程

/usr/sbin/httpd

HTTP的脚本

/etc/init.d/httpd

HTTP的端口

80(http)    443(https)

HTTP的配置文件

/etc/http/*    /var/www/*

 

下面来具体搭建HTTP服务

现在我们主要来实现虚拟主机的搭建,

虚拟主机可以实现在一台服务器上面运行多个站点,而且之间互不影响。可以大大的节约成本。虚拟主机技术可以通过三种方式来实现:

基于域名,基于IP,基于端口的虚拟主机。

 

下面主要讨论基于域名的虚拟主机的实现,

在实现基于域名的虚拟主机的之前,我们先要在DNS服务器里面做好解析。

我已经为192.168.0.254这个IP地址做了两个解析,一个是www1.example.com,还有一个station1.example.com

第一步,安装软件包

[root@localhost ~]#

[root@localhost ~]# yum -y install httpd

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Cluster                                                       | 1.3 kB     00:00    

ClusterStorage                                           | 1.3 kB     00:00    

Server                                                       | 1.3 kB     00:00    

VT                                                            | 1.3 kB     00:00    

Setting up Install Process 

Resolving Dependencies

There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.

The program yum-complete-transaction is found in the yum-utils package.

--> Running transaction check

---> Package httpd.i386 0:2.2.3-31.el5 set to be updated

--> Processing Dependency: libapr-1.so.0 for package: httpd

--> Processing Dependency: libaprutil-1.so.0 for package: httpd

--> Running transaction check

---> Package apr.i386 0:1.2.7-11.el5_3.1 set to be updated

---> Package apr-util.i386 0:1.2.7-7.el5_3.2 set to be updated

--> Processing Dependency: libpq.so.4 for package: apr-util

--> Running transaction check

---> Package postgresql-libs.i386 0:8.1.11-1.el5_1.1 set to be updated

--> Finished Dependency Resolution

 

Dependencies Resolved

 

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

 Package                Arch        Version                 Repository     Size

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

Installing:

 httpd                      i386        2.2.3-31.el5               Server        1.2 M

Installing for dependencies:

 apr                         i386        1.2.7-11.el5_3.1        Server        123 k

 apr-util                   i386        1.2.7-7.el5_3.2          Server         76 k

 postgresql-libs        i386        8.1.11-1.el5_1.1        Server        196 k

 

Transaction Summary

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

Install            4 Package(s)        

Update         0 Package(s)        

Remove        0 Package(s)        

 

Total size: 1.6 M

Total download size: 1.2 M

Downloading Packages:

httpd-2.2.3-31.el5.i386.rpm                              | 1.2 MB     00:00    

Running rpm_check_debug

Running Transaction Test

Finished Transaction Test

Transaction Test Succeeded

Running Transaction

  Installing     : apr                                                           1/4

  Installing     : postgresql-libs                                          2/4

  Installing     : apr-util                                                     3/4

  Installing     : httpd                                                        4/4

 

Installed:

  httpd.i386 0:2.2.3-31.el5                                                    

 

Dependency Installed:

  apr.i386 0:1.2.7-11.el5_3.1                apr-util.i386 0:1.2.7-7.el5_3.2  

  postgresql-libs.i386 0:8.1.11-1.el5_1.1  

 

Complete!

[root@localhost ~]#

HTTP软件包就安装成功了,

 

第二步,编辑http的配置文件

http服务的配置文件在/etc/httpd/conf/httpd.conf

vim  /etc/httpd/conf/httpd.conf

这个文件里面的语法很多,我们建议在定义虚拟站点的时候在这个文件的最后编辑,从这个文件的972行开始都是虚拟站点的模板文件,我们可以根据这些模板文件来定义虚拟站点。

NameVirtualHost 192.168.0.254:80

192.168.0.254:80>

DocumentRoot /var/www/virt1

ServerName    station1.example.com

ServerAlias     server1.example.com

192.168.0.254:80>

DocumentRoot  /var/www/virt2

ServerName     www1.example.com

OK,现在两个基于域名的虚拟站点基本就配置成功了。

现在,这两个基于域名的虚拟站点中根目录还没有建立,

[root@server1 ~]#

[root@server1 ~]# cd /var/www/

[root@server1 www]# mkdir virt1

[root@server1 www]# mkdir virt2

[root@server1 www]# cd virt1/

[root@server1 virt1]# vim index.html

[root@server1 virt1]# cat index.html

This is station1.example.com webstie!!!

[root@server1 virt1]#

[root@server1 virt1]# cd ../virt2/

[root@server1 virt2]#

[root@server1 virt2]# vim index.html

[root@server1 virt2]# cat index.html

This is www1.example.com website!!!

[root@server1 virt2]#

OK,这两个虚拟站点的根目录就建立,并且还分别建立了一个测试页面。

首先,重启下http服务,

[root@server1 ~]#

[root@server1 ~]# service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                             [  OK  ]

[root@server1 ~]#

OK,服务启动成功了,测试下。

现在我们到浏览器里面输入station1.example.com

OK,页面显示正常。

然后在浏览器里面输入www1.example.com

OK,显示也没有问题。

刚才我们给station1.example.conf做了一个别名为server1.example.com

现在我们来测试下,

最后在浏览器里面输入server1.example.com

OK,是和station1这个显示的相同的内容。

 

关于http服务的自身的访问控制

我们可以通过directory来做权限控制,

/etc/httpd/conf/httpd.conf文件的虚拟主机之间加入directory选项。

NameVirtualHost 192.168.0.254:80

192.168.0.254:80>

     DocumentRoot /var/www/virt1

     ServerName     station1.example.com

     ServerAlias       server1.example.com

      /var/www/virt1>

     options   -Indexes  -Followsymlinks

     order allow,deny

     allow  from   all

     deny  from   192.168.0.10

    

192.168.0.254:80>

     DocumentRoot /var/www/virt2

     ServerName      www1.example.com

      /var/www/virt2>

     options   -Indexes -Followsymlinks

     order  deny,allow

     deny   from  all

     allow  from  192.168.0.10

    

关于http服务语法参数的解释:

NameVirtualHost 192.168.0.254:80

通告虚拟主机在那个服务器上面以及那个端口,

第一个虚拟主机的站点,

DocumentRoot /var/www/virt1

虚拟主机站点的根目录,

ServerName station1.example.com

定义虚拟主机站点的域名,

ServerAlias server1.example.com

定义一个虚拟主机域名的别名,

定义虚拟主机站点的访问控制,

options -Indexes -Followsymlinks

去掉IndexesFollowsymlinks的功能,

order allow,deny

allow from all

deny from  192.168.0.10

允许所有,拒绝特定的主机。(这个是有顺序的)

order deny,allow 

deny from all

allow from 192.168.0.10

拒绝所有,允许特定的主机。(这个是有顺序的)

这个是语法,与对应,表示结束。

这个是语法,与对应,表示结束。

现在我们来测试一下,刚才我们定义了允许所有主机访问station1.example.com这个虚拟站点,拒绝192.168.0.10这台主机访问station1.example.com这个虚拟站点。还定义了拒绝所有主机访问www1.example.com这个虚拟站点,允许192.168.0.10这台主机访问www1.example.com这个虚拟站点。

首先,重启下http服务,

[root@server1 ~]#

[root@server1 ~]# service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                             [  OK  ]

[root@server1 ~]#

OK,服务启动成功了,测试下。

现在我们到192.168.0.10这台主机上面去测试。

我们到浏览器里面输入station1.example.com

可以看到,192.168.0.10的确不可以访问到station1.example.com这个虚拟站点。

现在我们在到浏览器里面输入www1.example.com

可以看到,192.168.0.10的确是可以访问到www1.example.com这个虚拟站点的。

现在我们在到192.168.0.254上面来测试一下,它应该可以访问station1.example.com这个虚拟站点,应该不可以访问www1.example.com

我们到浏览器里面输入station1.example.com

可以看到,192.168.0.254的确可以访问station1.example.com这个虚拟站点。

我们再到浏览器里面输入www1.example.com

可以看到,192.168.0.254的确不可以访问www1.example.com这个虚拟站点的。

这就说明我们刚才做的访问控制是生效的。

 

关于http服务的身份验证

一.生成身份认证的数据库

#Htpasswd  -cm  /etc/httpd/.htpasswd  bob

-c   创建      -m    md5的加密

#Htpasswd  -m  /etc/httpd/.htpasswd  alice

[root@server1 ~]#

[root@server1 ~]# htpasswd -cm /etc/httpd/.htpasswd bob

New password:

Re-type new password:

Adding password for user bob

[root@server1 ~]# htpasswd -m /etc/httpd/.htpasswd alice

New password:

Re-type new password:

Adding password for user alice

[root@server1 ~]#

现在我们打开这个文件来看一下,

Vim  /etc/httpd/.htpasswd

bob:$apr1$6o6Dh...$CbTynn8vJ.gVowkVGNacj0

alice:$apr1$whZJ0...$M1B8hxVv4Xc/MaNddkLRJ/

~ 

现在我们创建了两个用户,bobAlice。这两个用户本地可以不需要存在,只是用来登陆http服务的。

第一次创建用户的时候,需要加上-c的参数,代表创建身份认证的数据库。但是第二次创建用户的时候不要使用-c的参数,否则会覆盖前面的用户的。

 

二.在站点的根目录下面创建一个.htaccess文件

Vim  /var/www/virt1/.htaccess

AuthName       “website”

AuthType        basic

AuthUserFile     /etc/httpd/.htpasswd

Require user      bob  alice

Require vaid-user

关于.htacces文件的参数解释:

AuthName    “website”

认证时候的名字

AuthType      basic

认证的类型为basic

AuthUserFile   /etc/httpd/.htpasswd

调用刚才定义的认证数据库文件

Require user    bob  alice

允许bobAlice两个用户可以访问http服务,并且需要认证。

Require  vaid-user

所有在认证数据库里面的用户均访问http服务

 

三.启用认证

最后还需要在/etc/httpd/conf/httpd.conf这个文件中启用认证。

Allowoverride  Authconfig

 

首先,重启下http服务,

[root@server1 ~]#

[root@server1 ~]# service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                            [  OK  ]

[root@server1 ~]#

OK,服务启动成功了,测试下。

 

OK,当我们访问station1.example.com这个虚拟站点的时候的确是需要输入密码的。

 

关于http服务的语法检查工具

#service  httpd  configtest

[root@server1 ~]# service httpd configtest

Syntax OK

[root@server1 ~]#

#apachectl configtest

[root@server1 ~]# apachectl configtest

Syntax OK

[root@server1 ~]#

#httpd  -t

[root@server1 ~]# httpd -t

Syntax OK

[root@server1 ~]#

OK,这三种方式都是用来检查httpd.confssl.conf文件的语法的。

 

关于linux下面配置HTTP服务就到这里了。


阅读(614) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~